@exodus/test
    Preparing search index...

    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.

    v19.1.0, v18.13.0

    interface MockTracker {
        timers: MockTimers;
        fn<F extends Function = (...args: any[]) => undefined>(
            original?: F,
            options?: MockFunctionOptions,
        ): test.Mock<F>;
        fn<
            F extends Function = (...args: any[]) => undefined,
            Implementation extends Function = F,
        >(
            original?: F,
            implementation?: Implementation,
            options?: MockFunctionOptions,
        ): test.Mock<F | Implementation>;
        getter<
            MockedObject extends object,
            MethodName extends string | number | symbol,
        >(
            object: MockedObject,
            methodName: MethodName,
            options?: MockFunctionOptions,
        ): test.Mock<() => MockedObject[MethodName]>;
        getter<
            MockedObject extends object,
            MethodName extends string | number | symbol,
            Implementation extends Function,
        >(
            object: MockedObject,
            methodName: MethodName,
            implementation?: Implementation,
            options?: MockFunctionOptions,
        ): test.Mock<Implementation | (() => MockedObject[MethodName])>;
        method<
            MockedObject extends object,
            MethodName extends string | number | symbol,
        >(
            object: MockedObject,
            methodName: MethodName,
            options?: MockFunctionOptions,
        ): MockedObject[MethodName] extends Function ? test.Mock<any[any]> : never;
        method<
            MockedObject extends object,
            MethodName extends string | number | symbol,
            Implementation extends Function,
        >(
            object: MockedObject,
            methodName: MethodName,
            implementation: Implementation,
            options?: MockFunctionOptions,
        ): MockedObject[MethodName] extends Function
            ? test.Mock<Implementation | any[any]>
            : never;
        method<MockedObject extends object>(
            object: MockedObject,
            methodName: keyof MockedObject,
            options: MockMethodOptions,
        ): test.Mock<Function>;
        method<MockedObject extends object>(
            object: MockedObject,
            methodName: keyof MockedObject,
            implementation: Function,
            options: MockMethodOptions,
        ): test.Mock<Function>;
        module(specifier: string, options?: MockModuleOptions): MockModuleContext;
        reset(): void;
        restoreAll(): void;
        setter<
            MockedObject extends object,
            MethodName extends string | number | symbol,
        >(
            object: MockedObject,
            methodName: MethodName,
            options?: MockFunctionOptions,
        ): test.Mock<(value: MockedObject[MethodName]) => void>;
        setter<
            MockedObject extends object,
            MethodName extends string | number | symbol,
            Implementation extends Function,
        >(
            object: MockedObject,
            methodName: MethodName,
            implementation?: Implementation,
            options?: MockFunctionOptions,
        ): test.Mock<Implementation | ((value: MockedObject[MethodName]) => void)>;
    }
    Index

    Properties

    timers: MockTimers

    Methods

    • 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);
      });

      Type Parameters

      • F extends Function = (...args: any[]) => undefined

      Parameters

      • Optionaloriginal: F

        An optional function to create a mock on.

      • Optionaloptions: MockFunctionOptions

        Optional configuration options for the mock function.

      Returns test.Mock<F>

      The mocked function. The mocked function contains a special mock property, which is an instance of MockFunctionContext, and can be used for inspecting and changing the behavior of the mocked function.

      v19.1.0, v18.13.0

    • Type Parameters

      • F extends Function = (...args: any[]) => undefined
      • Implementation extends Function = F

      Parameters

      Returns test.Mock<F | Implementation>

    • 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);
      });

      Type Parameters

      • MockedObject extends object
      • MethodName extends string | number | symbol

      Parameters

      • object: MockedObject

        The object whose method is being mocked.

      • methodName: MethodName

        The identifier of the method on object to mock. If object[methodName] is not a function, an error is thrown.

      • Optionaloptions: MockFunctionOptions

        Optional configuration options for the mock method.

      Returns MockedObject[MethodName] extends Function ? test.Mock<any[any]> : never

      The mocked method. The mocked method contains a special mock property, which is an instance of MockFunctionContext, and can be used for inspecting and changing the behavior of the mocked method.

      v19.1.0, v18.13.0

    • Type Parameters

      • MockedObject extends object
      • MethodName extends string | number | symbol
      • Implementation extends Function

      Parameters

      Returns MockedObject[MethodName] extends Function
          ? test.Mock<Implementation | any[any]>
          : never

    • Type Parameters

      • MockedObject extends object

      Parameters

      Returns test.Mock<Function>

    • Type Parameters

      • MockedObject extends object

      Parameters

      Returns test.Mock<Function>

    • Experimental

      This function is used to mock the exports of ECMAScript modules, CommonJS modules, JSON modules, and Node.js builtin modules. Any references to the original module prior to mocking are not impacted. In order to enable module mocking, Node.js must be started with the --experimental-test-module-mocks command-line flag.

      The following example demonstrates how a mock is created for a module.

      test('mocks a builtin module in both module systems', async (t) => {
      // Create a mock of 'node:readline' with a named export named 'fn', which
      // does not exist in the original 'node:readline' module.
      const mock = t.mock.module('node:readline', {
      namedExports: { fn() { return 42; } },
      });

      let esmImpl = await import('node:readline');
      let cjsImpl = require('node:readline');

      // cursorTo() is an export of the original 'node:readline' module.
      assert.strictEqual(esmImpl.cursorTo, undefined);
      assert.strictEqual(cjsImpl.cursorTo, undefined);
      assert.strictEqual(esmImpl.fn(), 42);
      assert.strictEqual(cjsImpl.fn(), 42);

      mock.restore();

      // The mock is restored, so the original builtin module is returned.
      esmImpl = await import('node:readline');
      cjsImpl = require('node:readline');

      assert.strictEqual(typeof esmImpl.cursorTo, 'function');
      assert.strictEqual(typeof cjsImpl.cursorTo, 'function');
      assert.strictEqual(esmImpl.fn, undefined);
      assert.strictEqual(cjsImpl.fn, undefined);
      });

      Parameters

      • specifier: string

        A string identifying the module to mock.

      • Optionaloptions: MockModuleOptions

        Optional configuration options for the mock module.

      Returns MockModuleContext

      v22.3.0

    • 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.

      Returns void

      v19.1.0, v18.13.0

    • 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.

      Returns void

      v19.1.0, v18.13.0