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

Jest testing choices
NeedMechanismCaution
Related input matrixtest.eachIndependent expected values
Promise failureAwaited rejects assertionDo not exit early
Controlled collaboratorjest.fn or spyPreserve target behavior
Timer boundaryFake timersRestore environment
Mock cleanupClear, reset, or restoreDifferent semantics
Download this worksheet (CSV)

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.

Make the next change verifiable.

Getzilla is an open source workflow for changes to existing repositories. Inspect the project, define acceptance checks, and keep independent review in the loop.

See the repository workflow See current access and pricing