Jest testing patterns — test structure, mocking, async testing, snapshot
99
99%
Does it follow best practices?
Impact
99%
1.26xAverage score across 6 eval scenarios
Passed
No known issues
{
"instruction": "Follow Jest best practices for test structure, mocking, async testing, and configuration",
"relevant_when": "Agent writes or modifies Jest test files",
"context": "Jest tests must use jest.mock() for external dependencies with MockedFunction casting, beforeEach with clearAllMocks, mockResolvedValueOnce for per-test values, toHaveBeenCalledWith plus toHaveBeenCalledTimes for mock verification, expect.assertions for conditional assertions, async/await with rejects.toThrow for error testing, and fake timers with proper cleanup.",
"sources": [
{
"type": "file",
"filename": "skills/jest-testing/SKILL.md",
"tile": "tessl-labs/jest-testing@0.1.0"
}
],
"checklist": [
{
"name": "jest-mock-for-dependencies",
"rule": "Agent uses jest.mock() at module level to mock external dependencies rather than manual mock objects or constructor injection",
"relevant_when": "Agent writes tests that need to mock imported modules"
},
{
"name": "mocked-function-casting",
"rule": "Agent casts mocked functions using 'as jest.MockedFunction<typeof fn>' for type-safe mock access in TypeScript",
"relevant_when": "Agent writes TypeScript Jest tests with mocked modules"
},
{
"name": "clear-all-mocks-in-before-each",
"rule": "Agent calls jest.clearAllMocks() inside beforeEach (not afterEach) to guarantee fresh mock state before every test",
"relevant_when": "Agent writes Jest tests that use mocks"
},
{
"name": "mock-resolved-value-once",
"rule": "Agent uses mockResolvedValueOnce or mockReturnValueOnce for per-test mock setup instead of persistent mockResolvedValue",
"relevant_when": "Agent sets up mock return values in individual tests"
},
{
"name": "verify-call-count",
"rule": "Agent verifies mock call counts with toHaveBeenCalledTimes in addition to toHaveBeenCalledWith for important mocks",
"relevant_when": "Agent verifies that mocked functions were called correctly"
},
{
"name": "expect-assertions",
"rule": "Agent uses expect.assertions(n) in tests where assertions are inside callbacks, catch blocks, or conditional branches",
"relevant_when": "Agent writes tests with conditional or callback-based assertions"
},
{
"name": "async-await-pattern",
"rule": "Agent uses async/await syntax in async tests rather than .then() chaining or done callbacks",
"relevant_when": "Agent writes tests for async code"
},
{
"name": "rejects-to-throw",
"rule": "Agent uses 'await expect(fn()).rejects.toThrow(...)' for testing rejected promises, not try/catch",
"relevant_when": "Agent writes tests for code that should throw or reject"
},
{
"name": "fake-timers-with-cleanup",
"rule": "Agent uses jest.useFakeTimers() before timer tests, jest.advanceTimersByTime() to advance, and jest.useRealTimers() in afterEach to restore",
"relevant_when": "Agent writes tests for timer-based or scheduled code"
},
{
"name": "describe-it-structure",
"rule": "Agent uses one top-level describe block per module/class with nested describe blocks per method, and it() descriptions starting with verbs",
"relevant_when": "Agent writes Jest test files"
},
{
"name": "no-only-calls",
"rule": "Agent does not include test.only, it.only, or describe.only in committed test files",
"relevant_when": "Agent writes Jest test files"
},
{
"name": "do-not-mock-subject",
"rule": "Agent does not mock the module under test — only external dependencies are mocked",
"relevant_when": "Agent writes Jest tests with mocks"
}
]
}