QA Jest Writer
Purpose
Write Jest unit and integration tests from test case specifications (Phase 2 output). Transform structured test cases into executable TypeScript/JavaScript tests using Jest's describe/it patterns, mocking, snapshots, and coverage tooling.
Trigger Phrases
- "Write Jest tests for [module/function]"
- "Generate Jest unit tests from test cases"
- "Create Jest integration tests for [service]"
- "Add Jest tests with mocks for [dependency]"
- "Jest snapshot tests for [component]"
- "Jest test coverage for [file]"
- "Parameterized Jest tests for [function]"
- "Jest async tests for [API handler]"
Test Types
| Type |
Scope |
Focus |
| Unit |
Isolated function/class testing |
Single unit, mocked dependencies |
| Integration |
Module interaction, service layer |
Real or partial integration, fewer mocks |
Workflow
- Read test cases — From Phase 2 (qa-testcase-from-docs, qa-testcase-from-ui, qa-manual-test-designer)
- Analyze target code — Identify module under test, dependencies, exports
- Generate test files — Create
{module}.test.ts or {module}.spec.ts
- Add mocks/fixtures — jest.mock, jest.spyOn, factory functions, test data
- Verify coverage — Ensure assertions match test case steps, run coverage report
Context7 MCP
Uses Context7 MCP to fetch current Jest documentation when needed. Query for Jest API, matchers, or configuration to ensure generated tests align with latest Jest behavior.
Key Patterns
| Pattern |
Use Case |
describe / it / test |
Group and define test cases |
beforeEach / afterEach |
Setup and teardown per test |
jest.mock() |
Mock entire modules (hoisted) |
jest.spyOn() |
Spy on object methods |
| Snapshot testing |
UI components, serializable output |
test.each |
Parameterized / table-driven tests |
async/await, done |
Async testing |
| Custom matchers |
Domain-specific assertions |
Assertion Patterns
| Assertion |
Use |
toBe |
Strict equality (===) |
toEqual |
Deep equality |
toMatchObject |
Partial object match |
toThrow |
Error thrown |
toHaveBeenCalledWith |
Mock call arguments |
resolves / rejects |
Promise outcomes |
See references/assertions.md for full reference.
Configuration
- jest.config.ts — TypeScript config with
moduleNameMapper, transform, coverage
- tsconfig paths — Map aliases (e.g.,
@/ → src/)
- setupFiles — Global setup (e.g., env vars, polyfills)
See references/config.md for configuration guide.
File Naming
{module}.test.ts — Preferred (aligns with Jest default)
{module}.spec.ts — Alternative (common in Angular/Vue)
Place tests next to source (__tests__/ or colocated) or in tests/ per project convention.
Scope
Can do (autonomous):
- Generate Jest unit and integration tests from test case specs
- Add jest.mock, jest.spyOn, manual mocks
- Create snapshot tests for components/output
- Write parameterized tests (test.each)
- Handle async tests (async/await, resolves/rejects)
- Configure jest.config.ts patterns (moduleNameMapper, coverage)
- Use Context7 MCP for current Jest docs
Cannot do (requires confirmation):
- Change production code to make it testable
- Add tests for modules not in test case scope
- Override project Jest configuration without approval
Will not do (out of scope):
- E2E tests (use Playwright/Cypress skills)
- Run tests or interpret coverage reports
- Modify CI/CD pipelines
References
references/patterns.md — Unit structure, mocking strategies, async, snapshots, error testing
references/assertions.md — Complete Jest assertion reference by category
references/config.md — jest.config, tsconfig paths, coverage, setupFiles
references/best-practices.md — Anti-patterns, AAA, isolation, descriptive names
Quality Checklist
Troubleshooting
| Symptom |
Likely Cause |
Fix |
| Module not found |
Path/alias mismatch |
Check moduleNameMapper in jest.config, tsconfig paths |
| Mock not working |
Hoisting / import order |
Use jest.mock() at top of file, before imports |
| Timeout |
Async not resolved |
Increase jest.setTimeout(), ensure promises resolve |
| Snapshot drift |
Intentional change |
Run jest -u to update; verify change is correct |
| Coverage gaps |
Missing branches |
Add tests for error paths, edge cases |
| Spy not called |
Wrong target or timing |
Verify spy on correct object, call after setup |
1---2name: qa-jest-writer3description: Generate Jest unit and integration tests for TypeScript/JavaScript projects with jest.mock, snapshots, code coverage, and describe/it patterns.4---56# QA Jest Writer78## Purpose910Write Jest unit and integration tests from test case specifications (Phase 2 output). Transform structured test cases into executable TypeScript/JavaScript tests using Jest's describe/it patterns, mocking, snapshots, and coverage tooling.1112## Trigger Phrases1314- "Write Jest tests for [module/function]"15- "Generate Jest unit tests from test cases"16- "Create Jest integration tests for [service]"17- "Add Jest tests with mocks for [dependency]"18- "Jest snapshot tests for [component]"19- "Jest test coverage for [file]"20- "Parameterized Jest tests for [function]"21- "Jest async tests for [API handler]"2223## Test Types2425| Type | Scope | Focus |26| ---- | ----- | ----- |27| **Unit** | Isolated function/class testing | Single unit, mocked dependencies |28| **Integration** | Module interaction, service layer | Real or partial integration, fewer mocks |2930## Workflow31321. **Read test cases** — From Phase 2 (qa-testcase-from-docs, qa-testcase-from-ui, qa-manual-test-designer)332. **Analyze target code** — Identify module under test, dependencies, exports343. **Generate test files** — Create `{module}.test.ts` or `{module}.spec.ts`354. **Add mocks/fixtures** — jest.mock, jest.spyOn, factory functions, test data365. **Verify coverage** — Ensure assertions match test case steps, run coverage report3738## Context7 MCP3940Uses **Context7 MCP** to fetch current Jest documentation when needed. Query for Jest API, matchers, or configuration to ensure generated tests align with latest Jest behavior.4142## Key Patterns4344| Pattern | Use Case |45| ------- | -------- |46| `describe` / `it` / `test` | Group and define test cases |47| `beforeEach` / `afterEach` | Setup and teardown per test |48| `jest.mock()` | Mock entire modules (hoisted) |49| `jest.spyOn()` | Spy on object methods |50| Snapshot testing | UI components, serializable output |51| `test.each` | Parameterized / table-driven tests |52| `async`/`await`, `done` | Async testing |53| Custom matchers | Domain-specific assertions |5455## Assertion Patterns5657| Assertion | Use |58| --------- | --- |59| `toBe` | Strict equality (===) |60| `toEqual` | Deep equality |61| `toMatchObject` | Partial object match |62| `toThrow` | Error thrown |63| `toHaveBeenCalledWith` | Mock call arguments |64| `resolves` / `rejects` | Promise outcomes |6566See `references/assertions.md` for full reference.6768## Configuration6970- **jest.config.ts** — TypeScript config with `moduleNameMapper`, `transform`, `coverage`71- **tsconfig paths** — Map aliases (e.g., `@/` → `src/`)72- **setupFiles** — Global setup (e.g., env vars, polyfills)7374See `references/config.md` for configuration guide.7576## File Naming7778- `{module}.test.ts` — Preferred (aligns with Jest default)79- `{module}.spec.ts` — Alternative (common in Angular/Vue)8081Place tests next to source (`__tests__/` or colocated) or in `tests/` per project convention.8283## Scope8485**Can do (autonomous):**86- Generate Jest unit and integration tests from test case specs87- Add jest.mock, jest.spyOn, manual mocks88- Create snapshot tests for components/output89- Write parameterized tests (test.each)90- Handle async tests (async/await, resolves/rejects)91- Configure jest.config.ts patterns (moduleNameMapper, coverage)92- Use Context7 MCP for current Jest docs9394**Cannot do (requires confirmation):**95- Change production code to make it testable96- Add tests for modules not in test case scope97- Override project Jest configuration without approval9899**Will not do (out of scope):**100- E2E tests (use Playwright/Cypress skills)101- Run tests or interpret coverage reports102- Modify CI/CD pipelines103104## References105106- `references/patterns.md` — Unit structure, mocking strategies, async, snapshots, error testing107- `references/assertions.md` — Complete Jest assertion reference by category108- `references/config.md` — jest.config, tsconfig paths, coverage, setupFiles109- `references/best-practices.md` — Anti-patterns, AAA, isolation, descriptive names110111## Quality Checklist112113- [ ] No hardcoded sensitive data (use fixtures/env)114- [ ] Proper mocking (dependencies isolated, no real I/O)115- [ ] Assertions specific (no vague toBeTruthy for complex objects)116- [ ] Tests independent (order-independent, no shared mutable state)117- [ ] Descriptive test names (behavior/outcome, not implementation)118- [ ] Each test case step has corresponding assertion(s)119- [ ] Async tests use async/await or proper done/resolves120121## Troubleshooting122123| Symptom | Likely Cause | Fix |124| ------- | ------------ | --- |125| Module not found | Path/alias mismatch | Check `moduleNameMapper` in jest.config, tsconfig paths |126| Mock not working | Hoisting / import order | Use `jest.mock()` at top of file, before imports |127| Timeout | Async not resolved | Increase `jest.setTimeout()`, ensure promises resolve |128| Snapshot drift | Intentional change | Run `jest -u` to update; verify change is correct |129| Coverage gaps | Missing branches | Add tests for error paths, edge cases |130| Spy not called | Wrong target or timing | Verify spy on correct object, call after setup |