Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

esm: refactor mocking test #49465

Merged
merged 6 commits into from
Sep 5, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Review notes
GeoffreyBooth authored and aduh95 committed Sep 3, 2023
commit 844fe455701928518ffb3212b42fec18a99221f8
2 changes: 1 addition & 1 deletion test/es-module/test-esm-loader-mock.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Flags: --no-warnings --import ./test/fixtures/es-module-loaders/mock.mjs
// Flags: --import ./test/fixtures/es-module-loaders/mock.mjs
import assert from 'node:assert/strict';
import { mock } from '../fixtures/es-module-loaders/mock.mjs';

11 changes: 6 additions & 5 deletions test/fixtures/es-module-loaders/mock-loader.mjs
Original file line number Diff line number Diff line change
@@ -33,9 +33,11 @@ let currentMockVersion = 0;
// assert(namespace1 === namespace2);
// ```


export async function initialize({ port }) {
port.on('message', ({ mockVersion, resolved, exports }) => {
/** @type {string} */
let mainImportURL;
export async function initialize(data) {
mainImportURL = data.mainImportURL;
data.port.on('message', ({ mockVersion, resolved, exports }) => {
currentMockVersion = mockVersion;
mockedModuleExports.set(resolved, exports);
});
@@ -75,7 +77,6 @@ export async function load(url, context, defaultLoad) {
return defaultLoad(url, context);
}

const mainImportURL = new URL('./mock.mjs', import.meta.url);
/**
* Generate the source code for a mocked module.
* @param {string} encodedTargetURL the module being mocked
@@ -86,7 +87,7 @@ function generateModule(encodedTargetURL) {
decodeURIComponent(encodedTargetURL)
);
let body = [
`import { mockedModules } from '${mainImportURL}';`,
`import { mockedModules } from ${JSON.stringify(mainImportURL)};`,
'export {};',
'let mapping = {__proto__: null};'
];
5 changes: 4 additions & 1 deletion test/fixtures/es-module-loaders/mock.mjs
Original file line number Diff line number Diff line change
@@ -6,7 +6,10 @@ import { MessageChannel } from 'node:worker_threads';
const { port1, port2 } = new MessageChannel();

register(fixtures.fileURL('es-module-loaders/mock-loader.mjs'), {
data: { port: port2 },
data: {
port: port2,
mainImportURL: import.meta.url,
},
transferList: [port2],
});