温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.bun.com/reference/node/test/default/MockTracker/module
点击访问原文链接

MockTracker.module method | Node.js node:test module | Bun

MockTracker.module method | Node.js node:test module | BunBuildDocsReferenceGuidesBlogDiscord/node:test/default/MockTracker/moduleMmodule

Search the reference...

/

BuildDocsReferenceGuidesBlogDiscord/node:test/default/MockTracker/moduleMmodule

method

test.default.MockTracker.module { // Create a mock of 'node:readline' with a named export named 'foo', which // does not exist in the original 'node:readline' module. const mock = t.mock.module('node:readline', { exports: { foo: () => 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); }); ```" data-algolia-static="false" data-algolia-merged="false" data-type="Method">module(specifier: string | URL,options?: MockModuleOptions): MockModuleContext;

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.

Note: module customization hooks registered via the synchronous API effect resolution of the specifier provided to mock.module. Customization hooks registered via the asynchronous API are currently ignored (because the test runner's loader is synchronous, and node does not support multi-chain / cross-chain loading).

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 'foo', which
// does not exist in the original 'node:readline' module.
const mock = t.mock.module('node:readline', {
exports: { foo: () => 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);
});
@param specifier

A string identifying the module to mock.

@param options

Optional configuration options for the mock module.

Referenced typesinterface URLhash: stringhost: stringhostname: stringhref: stringreadonly origin: stringpassword: stringpathname: stringport: stringprotocol: stringsearch: stringreadonly searchParams: URLSearchParamsusername: stringtoJSON(): string;

Resources

ReferenceDocsGuidesDiscordMerch StoreGitHubBlog 

Toolkit

RuntimePackage managerTest runnerBundlerPackage runner

Project

Bun 1.0Bun 1.1Bun 1.2Bun 1.3RoadmapContributingLicense

Baked with ❤️ in San Francisco

We're hiring →

MockTracker.module method | Node.js node:test module | Bun,AI智能索引,全网链接索引,智能导航,网页索引

    API documentation for method node:test.default.MockTracker.module | Bun