Skip to main content

class MockTracker

The MockTracker class is used to manage mocking functionality. The test runner module provides a top level mock export which is a MockTracker instance. Each test also provides its own MockTracker instance via the test context's mock property.

Properties #

Methods #

#fn<F extends Function = NoOpFunction>(
original?: F,
): Mock<F>

This function is used to create a mock function.

The following example creates a mock function that increments a counter by one on each invocation. The times option is used to modify the mock behavior such that the first two invocations add two to the counter instead of one.

test('mocks a counting function', (t) => {
  let cnt = 0;

  function addOne() {
    cnt++;
    return cnt;
  }

  function addTwo() {
    cnt += 2;
    return cnt;
  }

  const fn = t.mock.fn(addOne, addTwo, { times: 2 });

  assert.strictEqual(fn(), 2);
  assert.strictEqual(fn(), 4);
  assert.strictEqual(fn(), 5);
  assert.strictEqual(fn(), 6);
});
#fn<
F extends Function = NoOpFunction,
Implementation extends Function = F,
>
(
original?: F,
implementation?: Implementation,
): Mock<F | Implementation>
#getter<
MockedObject extends object,
MethodName extends keyof MockedObject,
>
(
object: MockedObject,
methodName: MethodName,
): Mock<() => MockedObject[MethodName]>

This function is syntax sugar for MockTracker.method with options.getter set to true.

#getter<
MockedObject extends object,
MethodName extends keyof MockedObject,
Implementation extends Function,
>
(
object: MockedObject,
methodName: MethodName,
implementation?: Implementation,
): Mock<(() => MockedObject[MethodName]) | Implementation>
#method<
MockedObject extends object,
MethodName extends FunctionPropertyNames<MockedObject>,
>
(
object: MockedObject,
methodName: MethodName,
): MockedObject[MethodName] extends Function ? Mock<MockedObject[MethodName]> : never

This function is used to create a mock on an existing object method. The following example demonstrates how a mock is created on an existing object method.

test('spies on an object method', (t) => {
  const number = {
    value: 5,
    subtract(a) {
      return this.value - a;
    },
  };

  t.mock.method(number, 'subtract');
  assert.strictEqual(number.subtract.mock.calls.length, 0);
  assert.strictEqual(number.subtract(3), 2);
  assert.strictEqual(number.subtract.mock.calls.length, 1);

  const call = number.subtract.mock.calls[0];

  assert.deepStrictEqual(call.arguments, [3]);
  assert.strictEqual(call.result, 2);
  assert.strictEqual(call.error, undefined);
  assert.strictEqual(call.target, undefined);
  assert.strictEqual(call.this, number);
});
#method<
MockedObject extends object,
MethodName extends FunctionPropertyNames<MockedObject>,
Implementation extends Function,
>
(
object: MockedObject,
methodName: MethodName,
implementation: Implementation,
): MockedObject[MethodName] extends Function ? Mock<MockedObject[MethodName] | Implementation> : never
#method<MockedObject extends object>(
object: MockedObject,
methodName: keyof MockedObject,
): Mock<Function>
#method<MockedObject extends object>(
object: MockedObject,
methodName: keyof MockedObject,
implementation: Function,
): Mock<Function>
#module(
specifier: string,
): MockModuleContext

This function is used to mock the exports of ECMAScript modules, CommonJS modules, and Node.js builtin modules. Any references to the original module prior to mocking are not impacted.

Only available through the --experimental-test-module-mocks flag.

#reset(): void

This function restores the default behavior of all mocks that were previously created by this MockTracker and disassociates the mocks from the MockTracker instance. Once disassociated, the mocks can still be used, but the MockTracker instance can no longer be used to reset their behavior or otherwise interact with them.

After each test completes, this function is called on the test context's MockTracker. If the global MockTracker is used extensively, calling this function manually is recommended.

#restoreAll(): void

This function restores the default behavior of all mocks that were previously created by this MockTracker. Unlike mock.reset(), mock.restoreAll() does not disassociate the mocks from the MockTracker instance.

#setter<
MockedObject extends object,
MethodName extends keyof MockedObject,
>
(
object: MockedObject,
methodName: MethodName,
): Mock<(value: MockedObject[MethodName]) => void>

This function is syntax sugar for MockTracker.method with options.setter set to true.

#setter<
MockedObject extends object,
MethodName extends keyof MockedObject,
Implementation extends Function,
>
(
object: MockedObject,
methodName: MethodName,
implementation?: Implementation,
): Mock<((value: MockedObject[MethodName]) => void) | Implementation>

Did you find what you needed?

Privacy policy