Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท util, assert and the Test Runner

Node.js mock.method() method (node:test)

Reference for mock.method() in node:test: spy on or replace an object method, mock getters and setters, limit calls with times, and restore the original.

mock.method(object, methodName[, implementation][, options]) swaps a method on an object for a mock function and returns that mock. With no implementation it becomes a spy: the original method still runs and every call is recorded. With an implementation it becomes a stub that replaces the behavior.

This is how you stop a function from reaching the real world (Math.random, console.log, a client object's send) while a test runs, and how you check that your code called a dependency in the expected way. mock.getter() and mock.setter() do the same for accessor properties.

NODE
import { mock } from 'node:test';

const spy = mock.method(console, 'log');
console.log('hello');
console.log(spy.mock.callCount()); // 1

Try it

Node.js Spy on a method and replace it

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Getters, setters, times and a fixed Math.random

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
mock.method(object, methodName[, implementation][, options])
mock.getter(object, methodName[, implementation][, options])
mock.setter(object, methodName[, implementation][, options])
t.mock.method(...)
Name Description
object The object that owns the method (or a class prototype).
methodName The property name, a string or symbol.
implementation The replacement function. Omit it to spy on the original.
options.getter true to mock the getter of an accessor property. mock.getter() sets this for you.
options.setter true to mock the setter. mock.setter() sets this for you.
options.times Number of calls that use implementation; later calls use the original.

Returns: the mock function, which is also the new value of object[methodName]. It has the same .mock context as mock.fn(): calls, callCount(), mockImplementation(), mockImplementationOnce(), resetCalls() and restore().

Errors: a property that does not exist or is not a function throws ERR_INVALID_ARG_VALUE. Setting both getter and setter to true throws ERR_INVALID_ARG_VALUE too.

Usage notes

  • this is preserved. The original method and the implementation both receive the receiver of the call, so spies on methods that use this keep working.
  • Restoring. mock.restore() on the returned mock restores that one method. mock.restoreAll() restores every mock created through the global mock, and mock.reset() also forgets them. Mocks made with t.mock are restored when the test finishes.
  • Mocking class instances. Mock Class.prototype to affect all instances, or mock a single instance to affect only that object.
  • Mock the dependency, not the code under test. If the test replaces the very method it claims to check, it proves nothing. Spy when you can.
  • Frozen objects cannot be mocked. Mocking a method of a frozen object throws a TypeError. Built-ins such as Math.random and console.log can be mocked, as shown above.
  • Instances stay separate. Mocking a method on one instance puts the mock on that object only, so other instances of the same class keep the real method.
  • Do not forget to restore global mocks in an afterEach() hook when you use the global mock, or prefer t.mock so it happens automatically.