Skip to content

Commit 4ac5c71

Browse files
targosGeoffreyBooth
authored andcommitted
test: refactor to avoid mutation of global by a loader
This makes the test compatible with off-thread loaders. Co-Authored-By: Geoffrey Booth <webadmin@geoffreybooth.com> PR-URL: #46220 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
1 parent e482d5e commit 4ac5c71

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

test/es-module/test-esm-loader-resolve-type.mjs

+17-15
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import * as fs from 'fs';
66

77
allowGlobals(global.getModuleTypeStats);
88

9+
const { importedESM: importedESMBefore,
10+
importedCJS: importedCJSBefore } = await global.getModuleTypeStats();
11+
912
const basePath =
1013
new URL('./node_modules/', import.meta.url);
1114

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

1922
const moduleName = 'module-counter-by-type';
20-
2123
const moduleDir = rel(`${moduleName}`);
22-
createDir(basePath);
23-
createDir(moduleDir);
24-
fs.cpSync(
25-
fixtures.path('es-modules', moduleName),
26-
moduleDir,
27-
{ recursive: true }
28-
);
29-
30-
const { importedESM: importedESMBefore,
31-
importedCJS: importedCJSBefore } = global.getModuleTypeStats();
32-
33-
await import(`${moduleName}`).finally(() => {
24+
try {
25+
createDir(basePath);
26+
createDir(moduleDir);
27+
fs.cpSync(
28+
fixtures.path('es-modules', moduleName),
29+
moduleDir,
30+
{ recursive: true }
31+
);
32+
33+
34+
await import(`${moduleName}`);
35+
} finally {
3436
fs.rmSync(basePath, { recursive: true, force: true });
35-
});
37+
}
3638

3739
const { importedESM: importedESMAfter,
38-
importedCJS: importedCJSAfter } = global.getModuleTypeStats();
40+
importedCJS: importedCJSAfter } = await global.getModuleTypeStats();
3941

4042
// Dynamic import above should increment ESM counter but not CJS counter
4143
assert.strictEqual(importedESMBefore + 1, importedESMAfter);

test/fixtures/es-module-loaders/hook-resolve-type.mjs

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
let importedESM = 0;
22
let importedCJS = 0;
3-
global.getModuleTypeStats = () => { return {importedESM, importedCJS} };
3+
4+
export function globalPreload({ port }) {
5+
port.on('message', (int32) => {
6+
port.postMessage({ importedESM, importedCJS });
7+
Atomics.store(int32, 0, 1);
8+
Atomics.notify(int32, 0);
9+
});
10+
port.unref();
11+
return `
12+
const { receiveMessageOnPort } = getBuiltin('worker_threads');
13+
global.getModuleTypeStats = async function getModuleTypeStats() {
14+
const sab = new SharedArrayBuffer(4);
15+
const int32 = new Int32Array(sab);
16+
port.postMessage(int32);
17+
// Artificial timeout to keep the event loop alive.
18+
// https://bugs.chromium.org/p/v8/issues/detail?id=13238
19+
// TODO(targos) Remove when V8 issue is resolved.
20+
const timeout = setTimeout(() => { throw new Error('timeout'); }, 1_000);
21+
await Atomics.waitAsync(int32, 0, 0).value;
22+
clearTimeout(timeout);
23+
return receiveMessageOnPort(port).message;
24+
};
25+
`;
26+
}
427

528
export async function load(url, context, next) {
629
return next(url);

0 commit comments

Comments
 (0)