Skip to content

Commit

Permalink
test: refactor to avoid mutation of global by a loader
Browse files Browse the repository at this point in the history
This makes the test compatible with off-thread loaders.

Co-Authored-By: Geoffrey Booth <webadmin@geoffreybooth.com>
  • Loading branch information
targos and GeoffreyBooth committed Jan 16, 2023
1 parent 67a9ed3 commit 825261e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
32 changes: 17 additions & 15 deletions test/es-module/test-esm-loader-resolve-type.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import * as fs from 'fs';

allowGlobals(global.getModuleTypeStats);

const { importedESM: importedESMBefore,
importedCJS: importedCJSBefore } = await global.getModuleTypeStats();

const basePath =
new URL('./node_modules/', import.meta.url);

Expand All @@ -17,25 +20,24 @@ const createDir = (path) => {
};

const moduleName = 'module-counter-by-type';

const moduleDir = rel(`${moduleName}`);
createDir(basePath);
createDir(moduleDir);
fs.cpSync(
fixtures.path('es-modules', moduleName),
moduleDir,
{ recursive: true }
);

const { importedESM: importedESMBefore,
importedCJS: importedCJSBefore } = global.getModuleTypeStats();

await import(`${moduleName}`).finally(() => {
try {
createDir(basePath);
createDir(moduleDir);
fs.cpSync(
fixtures.path('es-modules', moduleName),
moduleDir,
{ recursive: true }
);


await import(`${moduleName}`);
} finally {
fs.rmSync(basePath, { recursive: true, force: true });
});
}

const { importedESM: importedESMAfter,
importedCJS: importedCJSAfter } = global.getModuleTypeStats();
importedCJS: importedCJSAfter } = await global.getModuleTypeStats();

// Dynamic import above should increment ESM counter but not CJS counter
assert.strictEqual(importedESMBefore + 1, importedESMAfter);
Expand Down
25 changes: 24 additions & 1 deletion test/fixtures/es-module-loaders/hook-resolve-type.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
let importedESM = 0;
let importedCJS = 0;
global.getModuleTypeStats = () => { return {importedESM, importedCJS} };

export function globalPreload({ port }) {
port.on('message', (int32) => {
port.postMessage({ importedESM, importedCJS });
Atomics.store(int32, 0, 1);
Atomics.notify(int32, 0);
});
port.unref();
return `
const { receiveMessageOnPort } = getBuiltin('worker_threads');
global.getModuleTypeStats = async function getModuleTypeStats() {
const sab = new SharedArrayBuffer(4);
const int32 = new Int32Array(sab);
port.postMessage(int32);
// Artificial timeout to keep the event loop alive.
// https://bugs.chromium.org/p/v8/issues/detail?id=13238
// TODO(targos) Remove when V8 issue is resolved.
const timeout = setTimeout(() => {}, 1_000);
await Atomics.waitAsync(int32, 0, 0).value;
clearTimeout(timeout);
return receiveMessageOnPort(port).message;
};
`;
}

export async function load(url, context, next) {
return next(url);
Expand Down

0 comments on commit 825261e

Please sign in to comment.