jest

Jest

Used in 1621 typescript projects (avg ★339)

TypeScript
Testing
Default
Used by 1621 projects

Details

Language / Topic
typescriptTypeScript
Category
Testing

Rules

balanced
- Use `describe` blocks to group tests by unit (e.g., controller or function) and `it` for individual cases.
- Set up repeatable fixtures in `beforeEach`, like instantiating subjects with mocks.
- Mock request/response objects and dependencies for isolation.
- Assert on mock interactions with `toHaveBeenCalledWith` and matchers like `expect.any()`.
- Mark async tests as `async` in `it` blocks.
- Group tests logically in `describe` to mirror production code structure and aid debugging.
- Use `beforeEach` over `beforeAll` for state reset, preventing test interference from shared setup.
- Create mocks with `jest.fn()` to return values and verify exact arguments in calls.
- Prefer `expect(res.status).toHaveBeenCalledWith(200)` for HTTP response validation in controller tests.
- Leverage `expect.any(Type)` for flexible assertions on dynamic data like arrays.