Test runner

mock.fn()

Creates a mock function and records calls. It can wrap an implementation while allowing assertions on inputs and outputs. A mock checks a controlled interaction, not the behavior of a real external service. Tested on Node.js 22.16.0. Example output (one line per entry): ["6 1 3"].

Formal Syntax & Structure

nodejs syntaxnodejs
mock.fn([original[, implementation]][, options])

Parameters & Attributes

NameTypeDescription
originalFunctionOriginal function.
implementationFunctionMock implementation.
optionsobjectMock options.

Return Value

Function

Instrumented mock function.

Working Example

Example Snippetnodejs
import { mock } from 'node:test';const fn = mock.fn(x => x * 2);try {    console.log(fn(3), fn.mock.callCount(), fn.mock.calls[0].arguments[0]);}finally {    mock.reset();}
Node.js Reference