qa
jestjavascripttestingmockingcoveragetypescriptunit-tests
jest
Write reliable JavaScript tests with Jest: unit tests, mocking, snapshots, coverage, and CI integration. Use for JS/TS testing.
Models: sonnetopusgpt-6gemini-3glm-5
Jest
Testing JavaScript and TypeScript with Jest.
Quick Start
npm i -D jest @types/jest ts-jest
npx jestWhen to Use
- Unit tests for JS/TS code
- React/Node component logic
- API and utility function testing
- Coverage and regression suites
Best Practices
Test Structure
- Name tests by behavior: `test('adds numbers')`
- Arrange, act, assert per test
- One behavior per test
- Colocate or use `__tests__`
Matchers & Assertions
- Use natural matchers (`toBe`, `toEqual`, `toContain`)
- Use `expect` with descriptive messages
- Test edge cases and errors
- Prefer `toEqual` for objects
Mocking
- Mock external modules and side effects
- Use `jest.fn()` and `jest.spyOn()`
- Reset mocks between tests (`clearMocks`)
- Avoid over-mocking your own code
Coverage & CI
- Set coverage thresholds
- Run in CI on every PR
- Use `--ci` and `--coverage`
- Keep tests fast and isolated
Dependencies
npm i -D jest @types/jest ts-jest
# React: @testing-library/reactExamples
// Basic test
function add(a: number, b: number) {
return a + b;
}
test("adds two numbers", () => {
expect(add(2, 3)).toBe(5);
});// Mocking a module
import { fetchUsers } from "./api";
jest.mock("./api");
test("renders users", async () => {
fetchUsers.mockResolvedValue([{ id: 1, name: "Alice" }]);
const users = await loadUsers();
expect(users).toHaveLength(1);
});// Parameterized tests
test.each([
[1, 1, 2],
[2, 3, 5],
[-1, 1, 0],
])("adds %i + %i = %i", (a, b, expected) => {
expect(add(a, b)).toBe(expected);
});// Spy and error test
const spy = jest.spyOn(console, "error").mockImplementation(() => {});
expect(() => risky()).toThrow();
spy.mockRestore();Step-by-Step
1. Install Jest and configure for your stack.
2. Add the test script and coverage thresholds.
3. Write focused unit tests.
4. Mock external dependencies.
5. Add edge case and error tests.
6. Run with `--coverage` and review gaps.
7. Wire Jest into CI.
8. Keep tests deterministic and fast.
Validation
1. All tests pass locally and in CI
2. Coverage meets the threshold
3. Mocks are reset between tests
4. No tests depend on network or timers
5. Failures pinpoint the behavior
Troubleshooting
- Flaky tests: remove shared state and timing.
- Over-mocked: mock only boundaries, not internals.
- Slow suite: use `--maxWorkers` and isolate heavy tests.