### Bad Example: Redundant Jest Test Structure Source: https://github.com/thealgorithms/typescript/blob/master/CONTRIBUTING.md This snippet shows an example of a poorly structured Jest test suite for a `Factorial` function. It uses multiple `it` blocks with redundant descriptions, adding no value and making the test suite verbose and less concise. It also demonstrates bad practice of not inlining simple variables. ```ts describe("Factorial", () => { // Bad: it-blocks add absolutely no value it("works", () => { expect(factorial(0)).toBe(1); }); it("works #2", () => { expect(factorial(1)).toBe(1); }); // Bad: Redundant it-block description it("3! is 6", () => { expect(factorial(3)).toBe(6); }); // Bad: Redundant it-block description it("4! is 24", () => { // Bad: Naming these variables adds no value; they should have been inlined const input = 4; const expected = 24; expect(factorial(input)).toBe(expected); }); }); ``` -------------------------------- ### Example Function for Summation in TypeScript Source: https://github.com/thealgorithms/typescript/blob/master/CONTRIBUTING.md This snippet demonstrates a simple TypeScript function `sum` that calculates the total of numbers in an array. It adheres to the coding style guidelines, using `lowerCamelCase` for the function name and `const` for the loop variable. It shows basic iteration and accumulation. ```ts function sum(arr: number[]): number { let total = 0; for (const elem of arr) total += elem; return total; } ``` -------------------------------- ### Optimized Example: Data-Driven Jest Tests with `test.each` Source: https://github.com/thealgorithms/typescript/blob/master/CONTRIBUTING.md This snippet demonstrates an even better approach to writing Jest tests using `test.each`. This method allows for data-driven testing, where multiple test cases are defined as an array of inputs and expected outputs, making the test suite highly concise, readable, and easily extensible for various scenarios. ```ts describe("Factorial", () => { test.each([[0, 1], [1, 1], [3, 6], [4, 24]])('%i! = %i', (n, expected) => { expect(factorial(n)).toBe(expected); }); }); ``` -------------------------------- ### Good Example: Concise Jest Test Structure Source: https://github.com/thealgorithms/typescript/blob/master/CONTRIBUTING.md This snippet presents a more concise and effective way to write Jest tests for the `Factorial` function. By consolidating multiple assertions into a single `test` block, it maintains the same test coverage while significantly reducing verbosity and improving readability, especially for trivial algorithms. ```ts test("Factorial", () => { expect(factorial(0)).toBe(1); expect(factorial(1)).toBe(1); expect(factorial(3)).toBe(6); expect(factorial(4)).toBe(24); }); ``` -------------------------------- ### Good Example: Optional Field Usage in TypeScript Source: https://github.com/thealgorithms/typescript/blob/master/CONTRIBUTING.md This snippet demonstrates the preferred way to define and use optional fields in TypeScript. By marking `y` as `y?: number`, it correctly indicates that the property might be absent, and the object can be initialized without it, improving type safety and clarity. ```ts let foo: { x: number, y?: number } = { x: 123 }; ``` -------------------------------- ### Bad Example: Optional Field Usage in TypeScript Source: https://github.com/thealgorithms/typescript/blob/master/CONTRIBUTING.md This snippet illustrates an incorrect way to handle optional fields in TypeScript. Using `undefined` explicitly for an optional property is discouraged; instead, the property should simply be omitted if it's not present, and its type should be marked as optional. ```ts let foo = { x: 123, y: undefined }; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.