mock.module(specifier[, options])


稳定性: 1.0 - 早期开发

¥Stability: 1.0 - Early development

  • specifier <string> | <URL> 用于标识要模拟的模块的字符串。

    ¥specifier <string> | <URL> A string identifying the module to mock.

  • options <Object> 模拟模块的可选配置选项。支持以下属性:

    ¥options <Object> Optional configuration options for the mock module. The following properties are supported:

    • cache <boolean> 如果 false,则每次调用 require()import() 都会生成一个新的模拟模块。如果是 true,后续调用将返回相同的模块模拟​​,并且模拟模块将插入到 CommonJS 缓存中。默认值:false。

      ¥cache <boolean> If false, each call to require() or import() generates a new mock module. If true, subsequent calls will return the same module mock, and the mock module is inserted into the CommonJS cache. Default: false.

    • defaultExport <any> 用作模拟模块默认导出的可选值。如果未提供此值,则 ESM 模拟不包含默认导出。如果模拟是 CommonJS 或内置模块,则此设置用作 module.exports 的值。如果没有提供此值,CJS 和内置模拟将使用空对象作为 module.exports 的值。

      ¥defaultExport <any> An optional value used as the mocked module's default export. If this value is not provided, ESM mocks do not include a default export. If the mock is a CommonJS or builtin module, this setting is used as the value of module.exports. If this value is not provided, CJS and builtin mocks use an empty object as the value of module.exports.

    • namedExports <Object> 可选对象,其键和值用于创建模拟模块的命名导出。如果模拟是 CommonJS 或内置模块,则这些值将复制到 module.exports 上。因此,如果使用命名导出和非对象默认导出创建模拟,则模拟在用作 CJS 或内置模块时将引发异常。

      ¥namedExports <Object> An optional object whose keys and values are used to create the named exports of the mock module. If the mock is a CommonJS or builtin module, these values are copied onto module.exports. Therefore, if a mock is created with both named exports and a non-object default export, the mock will throw an exception when used as a CJS or builtin module.

  • 返回:<MockModuleContext> 可用于操作模拟的对象。

    ¥Returns: <MockModuleContext> An object that can be used to manipulate the mock.

此函数用于模拟 ECMAScript 模块、CommonJS 模块和 Node.js 内置模块的导出。模拟之前对原始模块的任何引用均不受影响。为了启用模块模拟,必须使用 --experimental-test-module-mocks 命令行标志启动 Node.js。

¥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. 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);
});