Skip to content

Commit

Permalink
Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Aug 16, 2024
1 parent db0e8e2 commit 64babf7
Showing 1 changed file with 52 additions and 40 deletions.
92 changes: 52 additions & 40 deletions test/parallel/test-runner-module-mocking.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ test('CJS mocking with namedExports option', async (t) => {
assert.strictEqual(original.string, 'original cjs string');
assert.strictEqual(original.fn, undefined);

t.mock.module(fixture, {
t.mock.module(`${pathToFileURL(fixture)}`, {
namedExports: { fn() { return 42; } },
});
const mocked = require(fixture);
Expand All @@ -174,7 +174,7 @@ test('CJS mocking with namedExports option', async (t) => {
assert.strictEqual(original.string, 'original cjs string');
assert.strictEqual(original.fn, undefined);

t.mock.module(fixture, {
t.mock.module(`${pathToFileURL(fixture)}`, {
namedExports: { fn() { return 42; } },
cache: true,
});
Expand All @@ -195,7 +195,7 @@ test('CJS mocking with namedExports option', async (t) => {
assert.strictEqual(original.string, 'original cjs string');
assert.strictEqual(original.fn, undefined);

t.mock.module(fixture, {
t.mock.module(`${pathToFileURL(fixture)}`, {
namedExports: { fn() { return 42; } },
cache: false,
});
Expand All @@ -219,7 +219,7 @@ test('CJS mocking with namedExports option', async (t) => {

const defaultExport = { val1: 5, val2: 3 };

t.mock.module(fixture, {
t.mock.module(`${pathToFileURL(fixture)}`, {
defaultExport,
namedExports: { val1: 'mock value' },
});
Expand All @@ -242,7 +242,7 @@ test('CJS mocking with namedExports option', async (t) => {

const defaultExport = null;

t.mock.module(fixture, {
t.mock.module(`${pathToFileURL(fixture)}`, {
defaultExport,
namedExports: { val1: 'mock value' },
});
Expand All @@ -256,13 +256,13 @@ test('CJS mocking with namedExports option', async (t) => {

test('ESM mocking with namedExports option', async (t) => {
await t.test('does not cache by default', async (t) => {
const fixture = fixtures.path('module-mocking', 'basic-esm.mjs');
const fixture = fixtures.fileURL('module-mocking', 'basic-esm.mjs');
const original = await import(fixture);

assert.strictEqual(original.string, 'original esm string');
assert.strictEqual(original.fn, undefined);

t.mock.module(fixture, {
t.mock.module(`${fixture}`, {
namedExports: { fn() { return 42; } },
});
const mocked = await import(fixture);
Expand All @@ -276,13 +276,13 @@ test('ESM mocking with namedExports option', async (t) => {
});

await t.test('explicitly enables caching', async (t) => {
const fixture = fixtures.path('module-mocking', 'basic-esm.mjs');
const fixture = fixtures.fileURL('module-mocking', 'basic-esm.mjs');
const original = await import(fixture);

assert.strictEqual(original.string, 'original esm string');
assert.strictEqual(original.fn, undefined);

t.mock.module(fixture, {
t.mock.module(`${fixture}`, {
namedExports: { fn() { return 42; } },
cache: true,
});
Expand All @@ -297,13 +297,13 @@ test('ESM mocking with namedExports option', async (t) => {
});

await t.test('explicitly disables caching', async (t) => {
const fixture = fixtures.path('module-mocking', 'basic-esm.mjs');
const fixture = fixtures.fileURL('module-mocking', 'basic-esm.mjs');
const original = await import(fixture);

assert.strictEqual(original.string, 'original esm string');
assert.strictEqual(original.fn, undefined);

t.mock.module(fixture, {
t.mock.module(`${fixture}`, {
namedExports: { fn() { return 42; } },
cache: false,
});
Expand All @@ -318,7 +318,8 @@ test('ESM mocking with namedExports option', async (t) => {
});

await t.test('named exports are not applied to defaultExport', async (t) => {
const fixture = fixtures.path('module-mocking', 'basic-esm.mjs');
const fixturePath = fixtures.path('module-mocking', 'basic-esm.mjs');
const fixture = pathToFileURL(fixturePath);
const original = await import(fixture);

assert.strictEqual(original.string, 'original esm string');
Expand All @@ -327,7 +328,7 @@ test('ESM mocking with namedExports option', async (t) => {

const defaultExport = 'mock default';

t.mock.module(fixture, {
t.mock.module(`${fixture}`, {
defaultExport,
namedExports: { val1: 'mock value' },
});
Expand All @@ -338,19 +339,19 @@ test('ESM mocking with namedExports option', async (t) => {
assert.strictEqual(mocked.default, 'mock default');
assert.strictEqual(mocked.val1, 'mock value');
t.mock.reset();
common.expectRequiredModule(require(fixture), original);
common.expectRequiredModule(require(fixturePath), original);
});

await t.test('throws if named exports cannot be applied to defaultExport as CJS', async (t) => {
const fixture = fixtures.path('module-mocking', 'basic-cjs.js');
const fixture = fixtures.fileURL('module-mocking', 'basic-cjs.js');
const original = await import(fixture);

assert.strictEqual(original.default.string, 'original cjs string');
assert.strictEqual(original.default.val1, undefined);

const defaultExport = null;

t.mock.module(fixture, {
t.mock.module(`${fixture}`, {
defaultExport,
namedExports: { val1: 'mock value' },
});
Expand All @@ -366,13 +367,14 @@ test('ESM mocking with namedExports option', async (t) => {
test('modules cannot be mocked multiple times at once', async (t) => {
await t.test('CJS', async (t) => {
const fixture = fixtures.path('module-mocking', 'basic-cjs.js');
const fixtureURL = pathToFileURL(fixture).href;

t.mock.module(fixture, {
t.mock.module(fixtureURL, {
namedExports: { fn() { return 42; } },
});

assert.throws(() => {
t.mock.module(fixture, {
t.mock.module(fixtureURL, {
namedExports: { fn() { return 55; } },
});
}, {
Expand All @@ -386,7 +388,7 @@ test('modules cannot be mocked multiple times at once', async (t) => {
});

await t.test('ESM', async (t) => {
const fixture = fixtures.path('module-mocking', 'basic-esm.mjs');
const fixture = fixtures.fileURL('module-mocking', 'basic-esm.mjs').href;

t.mock.module(fixture, {
namedExports: { fn() { return 42; } },
Expand All @@ -405,14 +407,20 @@ test('modules cannot be mocked multiple times at once', async (t) => {

assert.strictEqual(mocked.fn(), 42);
});

await t.test('Importing a Windows path should fail', { skip: common.isWindows }, async (t) => {

Check failure on line 411 in test/parallel/test-runner-module-mocking.js

View workflow job for this annotation

GitHub Actions / test-linux

--- stderr --- (node:169593) ExperimentalWarning: Support for loading ES Module in require() is an experimental feature and might change at any time (Use `node --trace-warnings ...` to show where the warning was created) (node:169593) ExperimentalWarning: Module mocking is an experimental feature and might change at any time (node:169593) ExperimentalWarning: Support for loading ES Module in require() is an experimental feature and might change at any time (Use `node --trace-warnings ...` to show where the warning was created) --- stdout --- ▶ input validation ✔ throws if specifier is not a string (3.38569ms) ✔ throws if options is not an object (0.306679ms) ::debug::starting to run input validation ::debug::starting to run throws if specifier is not a string ::debug::completed running throws if specifier is not a string ::debug::starting to run throws if options is not an object ::debug::completed running throws if options is not an object ✔ throws if cache is not a boolean (0.306349ms) ✔ throws if namedExports is not an object (0.327327ms) ▶ input validation (6.414307ms) ::debug::starting to run throws if cache is not a boolean ::debug::completed running throws if cache is not a boolean ::debug::starting to run throws if namedExports is not an object ::debug::completed running throws if namedExports is not an object ::debug::completed running input validation ::debug::starting to run core module mocking with namedExports option ▶ core module mocking with namedExports option ✔ does not cache by default (49.502755ms) ✔ explicitly enables caching (1.188234ms) ✔ explicitly disables caching (1.251681ms) ::debug::starting to run does not cache by default ::debug::completed running does not cache by default ::debug::starting to run explicitly enables caching ::debug::completed running explicitly enables caching ::debug::starting to run explicitly disables caching ::debug::completed running explicitly disables caching ::debug::starting to run named exports are applied to defaultExport ✔ named exports are applied to defaultExport (1.146758ms) ✔ throws if named exports cannot be applied to defaultExport (1.47726ms) ▶ core module mocking with namedExports option (55.792802ms) ▶ CJS mocking with namedExports option ✔ does not cache by default (2.589313ms) ::debug::completed running named exports are applied to defaultExport ::debug::starting to run throws if named exports cannot be applied to defaultExport ::debug::completed running throws if named exports cannot be applied to defaultExport ::debug::completed running core module mocking with namedExports option ::debug::starting to run CJS mocking with namedExports option ::debug::starting to run does not cache by default ::debug::completed running does not cache by default ✔ explicitly enables caching (4.392959ms) ✔ explicitly disables caching (3.893943ms) ✔ named exports are applied to defaultExport (8.718512ms) ::debug::starting to run explicitly enables caching ::debug::completed running explicitly enables caching ::debug::starting to run explicitly disables caching ::debug::completed running explicitly disables caching ::debug::starting to run named exports are applied to defaultExport ::debug::completed running named exports are applied to defaultExport ✔ throws if named exports cannot be applied to defaultExport (4.732148ms) ▶ CJS mocking with namedExports option (25.190916ms) ::debug::starting to run throws if named exports cannot be applied to defaultExport ::debug::completed running throws if named exports cannot be applied to defaultExport ::debug::completed running CJS mocking with namedExports option ▶ ESM mocking with namedExports option ✔ does not cache by default (99.038155ms) ::debug::starting to run ESM mocking with namedExports option ::debug::starting to run does not cache by default ::debug::completed running does not cache by default ✔ explicitly enables caching (35.767715ms) ::debug::starting to run explicitly enables caching ::debug::completed running explicitly enables caching ✔ explicitly disables caching (44.980774ms) ::

Check failure on line 411 in test/parallel/test-runner-module-mocking.js

View workflow job for this annotation

GitHub Actions / test-macOS

--- stderr --- (node:71735) ExperimentalWarning: Support for loading ES Module in require() is an experimental feature and might change at any time (Use `node --trace-warnings ...` to show where the warning was created) (node:71735) ExperimentalWarning: Module mocking is an experimental feature and might change at any time (node:71735) ExperimentalWarning: Support for loading ES Module in require() is an experimental feature and might change at any time (Use `node --trace-warnings ...` to show where the warning was created) --- stdout --- ▶ input validation ✔ throws if specifier is not a string (1.148583ms) ✔ throws if options is not an object (0.085042ms) ::debug::starting to run input validation ::debug::starting to run throws if specifier is not a string ::debug::completed running throws if specifier is not a string ::debug::starting to run throws if options is not an object ::debug::completed running throws if options is not an object ✔ throws if cache is not a boolean (0.089792ms) ✔ throws if namedExports is not an object (0.079542ms) ▶ input validation (2.104625ms) ::debug::starting to run throws if cache is not a boolean ::debug::completed running throws if cache is not a boolean ::debug::starting to run throws if namedExports is not an object ::debug::completed running throws if namedExports is not an object ::debug::completed running input validation ::debug::starting to run core module mocking with namedExports option ▶ core module mocking with namedExports option ✔ does not cache by default (17.751417ms) ✔ explicitly enables caching (0.599917ms) ✔ explicitly disables caching (0.739292ms) ::debug::starting to run does not cache by default ::debug::completed running does not cache by default ::debug::starting to run explicitly enables caching ::debug::completed running explicitly enables caching ::debug::starting to run explicitly disables caching ::debug::completed running explicitly disables caching ::debug::starting to run named exports are applied to defaultExport ✔ named exports are applied to defaultExport (0.877583ms) ✔ throws if named exports cannot be applied to defaultExport (1.058625ms) ▶ core module mocking with namedExports option (21.529584ms) ▶ CJS mocking with namedExports option ✔ does not cache by default (8.208458ms) ::debug::completed running named exports are applied to defaultExport ::debug::starting to run throws if named exports cannot be applied to defaultExport ::debug::completed running throws if named exports cannot be applied to defaultExport ::debug::completed running core module mocking with namedExports option ::debug::starting to run CJS mocking with namedExports option ::debug::starting to run does not cache by default ::debug::completed running does not cache by default ✔ explicitly enables caching (6.337833ms) ✔ explicitly disables caching (2.594875ms) ✔ named exports are applied to defaultExport (1.296792ms) ::debug::starting to run explicitly enables caching ::debug::completed running explicitly enables caching ::debug::starting to run explicitly disables caching ::debug::completed running explicitly disables caching ::debug::starting to run named exports are applied to defaultExport ::debug::completed running named exports are applied to defaultExport ✔ throws if named exports cannot be applied to defaultExport (0.488666ms) ▶ CJS mocking with namedExports option (19.276167ms) ::debug::starting to run throws if named exports cannot be applied to defaultExport ::debug::completed running throws if named exports cannot be applied to defaultExport ::debug::completed running CJS mocking with namedExports option ▶ ESM mocking with namedExports option ✔ does not cache by default (9.716291ms) ::debug::starting to run ESM mocking with namedExports option ::debug::starting to run does not cache by default ::debug::completed running does not cache by default ✔ explicitly enables caching (1.751792ms) ::debug::starting to run explicitly enables caching ::debug::completed running explicitly enables caching ✔ explicitly disables caching (1.964333ms) ::debu
const fixture = fixtures.path('module-mocking', 'wrong-path.js');
t.mock.module(fixture, { namedExports: { fn() { return 42; } } });
await assert.rejects(import(fixture), { code: 'ERR_UNSUPPORTED_ESM_URL_SCHEME' });
})

Check failure on line 415 in test/parallel/test-runner-module-mocking.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing semicolon
});

test('mocks are automatically restored', async (t) => {
const cjsFixture = fixtures.path('module-mocking', 'basic-cjs.js');
const esmFixture = fixtures.path('module-mocking', 'basic-esm.mjs');
const esmFixture = fixtures.fileURL('module-mocking', 'basic-esm.mjs');

await t.test('CJS', async (t) => {
t.mock.module(cjsFixture, {
t.mock.module(`${pathToFileURL(cjsFixture)}`, {
namedExports: { fn() { return 42; } },
});

Expand All @@ -422,7 +430,7 @@ test('mocks are automatically restored', async (t) => {
});

await t.test('ESM', async (t) => {
t.mock.module(esmFixture, {
t.mock.module(`${esmFixture}`, {
namedExports: { fn() { return 43; } },
});

Expand All @@ -442,13 +450,13 @@ test('mocks are automatically restored', async (t) => {

test('mocks can be restored independently', async (t) => {
const cjsFixture = fixtures.path('module-mocking', 'basic-cjs.js');
const esmFixture = fixtures.path('module-mocking', 'basic-esm.mjs');
const esmFixture = fixtures.fileURL('module-mocking', 'basic-esm.mjs');

const cjsMock = t.mock.module(cjsFixture, {
const cjsMock = t.mock.module(`${pathToFileURL(cjsFixture)}`, {
namedExports: { fn() { return 42; } },
});

const esmMock = t.mock.module(esmFixture, {
const esmMock = t.mock.module(`${esmFixture}`, {
namedExports: { fn() { return 43; } },
});

Expand Down Expand Up @@ -511,18 +519,19 @@ test('node:- core module mocks can be used by both module systems', async (t) =>

test('CJS mocks can be used by both module systems', async (t) => {
const cjsFixture = fixtures.path('module-mocking', 'basic-cjs.js');
const cjsMock = t.mock.module(cjsFixture, {
const cjsFixtureURL = pathToFileURL(cjsFixture)

Check failure on line 522 in test/parallel/test-runner-module-mocking.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing semicolon
const cjsMock = t.mock.module(`${cjsFixtureURL}`, {
namedExports: { fn() { return 42; } },
});
let esmImpl = await import(cjsFixture);
let esmImpl = await import(cjsFixtureURL);
let cjsImpl = require(cjsFixture);

assert.strictEqual(esmImpl.fn(), 42);
assert.strictEqual(cjsImpl.fn(), 42);

cjsMock.restore();

esmImpl = await import(cjsFixture);
esmImpl = await import(cjsFixtureURL);
cjsImpl = require(cjsFixture);

assert.strictEqual(esmImpl.default.string, 'original cjs string');
Expand All @@ -531,19 +540,20 @@ test('CJS mocks can be used by both module systems', async (t) => {

test('ESM mocks can be used by both module systems', async (t) => {
const esmFixture = fixtures.path('module-mocking', 'basic-esm.mjs');
const esmMock = t.mock.module(esmFixture, {
const esmFixtureURL = pathToFileURL(esmFixture)

Check failure on line 543 in test/parallel/test-runner-module-mocking.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing semicolon
const esmMock = t.mock.module(`${esmFixtureURL}`, {
namedExports: { fn() { return 42; } },
});

let cjsImpl = require(esmFixture);
let esmImpl = await import(esmFixture);
let esmImpl = await import(esmFixtureURL);

assert.strictEqual(cjsImpl.fn(), 42);
assert.strictEqual(esmImpl.fn(), 42);

esmMock.restore();
cjsImpl = require(esmFixture);
esmImpl = await import(esmFixture);
esmImpl = await import(esmFixtureURL);

assert.strictEqual(esmImpl.string, 'original esm string');
assert.strictEqual(cjsImpl.string, 'original esm string');
Expand All @@ -552,7 +562,7 @@ test('ESM mocks can be used by both module systems', async (t) => {
test('relative paths can be used by both module systems', async (t) => {
const fixture = relative(
__dirname, fixtures.path('module-mocking', 'basic-esm.mjs')
);
).replaceAll('\\', '/');
const mock = t.mock.module(fixture, {
namedExports: { fn() { return 42; } },
});
Expand Down Expand Up @@ -604,9 +614,9 @@ test('file:// imports are supported in ESM only', async (t) => {
});

test('mocked modules do not impact unmocked modules', async (t) => {
const mockedFixture = fixtures.path('module-mocking', 'basic-cjs.js');
const unmockedFixture = fixtures.path('module-mocking', 'basic-esm.mjs');
t.mock.module(mockedFixture, {
const mockedFixture = fixtures.fileURL('module-mocking', 'basic-cjs.js');
const unmockedFixture = fixtures.fileURL('module-mocking', 'basic-esm.mjs');
t.mock.module(`${mockedFixture}`, {
namedExports: { fn() { return 42; } },
});
const mockedImpl = await import(mockedFixture);
Expand All @@ -619,24 +629,26 @@ test('mocked modules do not impact unmocked modules', async (t) => {

test('defaultExports work with CJS mocks in both module systems', async (t) => {
const fixture = fixtures.path('module-mocking', 'basic-cjs.js');
const fixtureURL = pathToFileURL(fixture);
const original = require(fixture);
const defaultExport = Symbol('default');

assert.strictEqual(original.string, 'original cjs string');
t.mock.module(fixture, { defaultExport });
t.mock.module(`${fixtureURL}`, { defaultExport });
assert.strictEqual(require(fixture), defaultExport);
assert.strictEqual((await import(fixture)).default, defaultExport);
assert.strictEqual((await import(fixtureURL)).default, defaultExport);
});

test('defaultExports work with ESM mocks in both module systems', async (t) => {
const fixture = fixtures.path('module-mocking', 'basic-esm.mjs');
const fixturePath = fixtures.path('module-mocking', 'basic-esm.mjs');
const fixture = pathToFileURL(fixturePath);
const original = await import(fixture);
const defaultExport = Symbol('default');

assert.strictEqual(original.string, 'original esm string');
t.mock.module(fixture, { defaultExport });
t.mock.module(`${fixture}`, { defaultExport });
assert.strictEqual((await import(fixture)).default, defaultExport);
assert.strictEqual(require(fixture), defaultExport);
assert.strictEqual(require(fixturePath), defaultExport);
});

test('wrong import syntax should throw error after module mocking.', async () => {
Expand Down

0 comments on commit 64babf7

Please sign in to comment.