node/test/fixtures/test-runner/mock-nm.js
Colin Ihrig a619789ef0
test_runner: support module mocking
This commit adds experimental module mocking to the test runner.

PR-URL: https://github.com/nodejs/node/pull/52848
Fixes: https://github.com/nodejs/node/issues/51164
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
2024-05-19 05:09:09 +00:00

21 lines
598 B
JavaScript

'use strict';
const assert = require('node:assert');
const { test } = require('node:test');
const fixture = 'reporter-cjs';
test('mock node_modules dependency', async (t) => {
const mock = t.mock.module(fixture, {
namedExports: { fn() { return 42; } },
});
let cjsImpl = require(fixture);
let esmImpl = await import(fixture);
assert.strictEqual(cjsImpl.fn(), 42);
assert.strictEqual(esmImpl.fn(), 42);
mock.restore();
cjsImpl = require(fixture);
esmImpl = await import(fixture);
assert.strictEqual(cjsImpl.fn, undefined);
assert.strictEqual(esmImpl.fn, undefined);
});