TE

TypeScript Testing

TypeScript testing conventions

Details

Language / Topic
typescriptTypeScript
Category
Testing

Rules

balanced
- Write tests alongside source files (`*.test.ts`) or in a `__tests__/` directory.
- Use the Arrange-Act-Assert pattern: set up data, perform the action, verify the result.
- Use `describe()` for grouping, `it()` for cases — use type-safe mocking with `vi.fn()` (Vitest) or `jest.fn()` and `Partial<T>` for mock types.
- Mock external dependencies (APIs, databases) — test your logic, not the network.
- Use `describe` blocks to group related tests. Use `it` with descriptive names: `it("should return 404 when user not found")`.
- Use typed test fixtures and factories for consistent, type-safe test data.
- Use `beforeEach` for setup and `afterEach` for cleanup — avoid shared mutable state between tests.