Fixtures and mocking
A fixture is controlled test data or a resource created for a test. A mock substitutes a dependency so a test can exercise a defined outcome without relying on a real external service. Inject a clock or repository through a function parameter. This makes the dependency explicit and avoids changing global time. Use fresh fixtures for each test. The built-in mock tracker can record calls; a context-owned mock is restored at test completion. A mock verifies interactions with its simulated contract, not the real service. Hooks can set up and clean up resources; use try/finally when the resource is acquired inside a test. Do not mock the function under test or assert every incidental call. Global mocks shared by concurrent tests can interfere with each other. Sources: https://nodejs.org/download/release/v22.16.0/docs/api/test.html.
Formal Syntax & Structure
mock.fn(implementation); test(name, fn)Working Example
import assert from 'node:assert/strict';function valid(session, now){return now()<session.expiresAt;}const session={expiresAt:100};for(const t of [99,100,101]) console.log(t,valid(session,()=>t));assert.equal(valid(session,()=>100),false);