mock

A test double that stands in for a real dependency and checks that the code under test calls it in the expected way.

In more detail

A mock records or expects specific calls, so the test depends on which methods are called and with which arguments. People often say mock for any test double, but strictly a mock is the kind that verifies interactions, so use one when the interaction really is the behaviour. Never mock the code under test, and pair mocks with contract or integration tests, because a mock is only an assumption about the real dependency.

Example

node:test provides mock.fn(), mock.method() and mock.timers; mocks created through the test context t.mock are restored automatically when that test ends.

Related terms

Taught in

References