Start with the existing module setup
Inspect the repository's Jest configuration, package module format, and transforms before adding examples. TypeScript and ES modules may need project-specific setup. Use the normal test command and verify discovery so a passing result reflects executed cases rather than a file pattern that silently excluded the new test from the run.
Choose a matcher that expresses the requirement. Use a deep comparison for an expected object and an exact value comparison for a scalar where appropriate. Avoid broad truthiness when the content matters, because a wrong nonempty object or nonzero amount can satisfy that assertion while violating the business rule the test names.
Await asynchronous behavior
Await a returned promise or use an awaited resolves or rejects assertion so Jest observes the outcome. For callbacks, use the supported completion pattern and ensure failures reach the runner. A test that launches work and exits early may pass before the relevant assertion executes, producing no useful evidence about the asynchronous result.
Use fake timers for timer-driven behavior only when the scenario needs them. Advance the relevant work deliberately and restore the timer environment afterward. Be careful with code that mixes timers and promises, because advancing one queue does not automatically establish that every asynchronous application effect has completed in the order your test assumes.
Distinguish mock cleanup operations
Use jest.fn for controlled callable behavior and spies where observing or replacing an existing method fits the boundary. Understand the difference between clearing call history, resetting mock behavior, and restoring the original implementation. Choose the operation that matches the shared setup rather than treating every cleanup method as equivalent across all mocks.
Keep assertions about calls only when the interaction is the contract, such as sending a single notification after a valid transition. Prefer returned values or observable state for ordinary logic. Test real adapters separately, since a mocked module can make local cases deterministic while hiding a mismatch in the actual service or database integration.
Working sheet
| Need | Mechanism | Caution |
|---|---|---|
| Related input matrix | test.each | Independent expected values |
| Promise failure | Awaited rejects assertion | Do not exit early |
| Controlled collaborator | jest.fn or spy | Preserve target behavior |
| Timer boundary | Fake timers | Restore environment |
| Mock cleanup | Clear, reset, or restore | Different semantics |
Questions about jest unit testing: assertions, isolation and mocks
Why did an asynchronous Jest test pass too early?
The test may not have returned or awaited its asynchronous work. Ensure the runner observes completion.
Are clearing and restoring mocks equivalent?
No. Clearing removes call history; restoring returns a spied implementation to its original behavior. Resetting has another purpose.
How should I use the working sheet?
Download the CSV and replace the sample rows with evidence from your repository. Record a clear acceptance check and an owner for each item. Keep unresolved assumptions visible rather than treating the example as a completed assessment.
Sources and further reading
Examples are illustrative. They are not customer results, security guarantees, or a substitute for checking the documentation for your installed version.
