Skip to content

Commit 52901a7

Browse files
aduh95targos
authored andcommitted
test: deflake test-esm-loader-resolve-type
PR-URL: nodejs#50273 Fixes: nodejs#50040 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Richard Lau <rlau@redhat.com>
1 parent 5699370 commit 52901a7

File tree

2 files changed

+23
-28
lines changed

2 files changed

+23
-28
lines changed

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

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1-
/** @type {MessagePort} */
2-
let port;
3-
export function initialize(data) {
4-
port = data.port;
1+
/** @type {Uint8Array} */
2+
let data;
3+
/** @type {number} */
4+
let ESM_MODULE_INDEX;
5+
/** @type {number} */
6+
let CJS_MODULE_INDEX;
7+
8+
export function initialize({ sab, ESM_MODULE_INDEX:e, CJS_MODULE_INDEX:c }) {
9+
data = new Uint8Array(sab);
10+
ESM_MODULE_INDEX = e;
11+
CJS_MODULE_INDEX = c;
512
}
613

714
export async function resolve(specifier, context, next) {
815
const nextResult = await next(specifier, context);
916
const { format } = nextResult;
1017

1118
if (format === 'module' || specifier.endsWith('.mjs')) {
12-
port.postMessage({ type: 'module' });
19+
Atomics.add(data, ESM_MODULE_INDEX, 1);
1320
} else if (format == null || format === 'commonjs') {
14-
port.postMessage({ type: 'commonjs' });
21+
Atomics.add(data, CJS_MODULE_INDEX, 1);
1522
}
1623

1724
return nextResult;
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,18 @@
11
import * as fixtures from '../../common/fixtures.mjs';
22
import { register } from 'node:module';
3-
import { MessageChannel } from 'node:worker_threads';
43

5-
let importedESM = 0;
6-
let importedCJS = 0;
4+
const sab = new SharedArrayBuffer(2);
5+
const data = new Uint8Array(sab);
6+
7+
const ESM_MODULE_INDEX = 0
8+
const CJS_MODULE_INDEX = 1
9+
710
export function getModuleTypeStats() {
11+
const importedESM = Atomics.load(data, ESM_MODULE_INDEX);
12+
const importedCJS = Atomics.load(data, CJS_MODULE_INDEX);
813
return { importedESM, importedCJS };
9-
};
10-
11-
const { port1, port2 } = new MessageChannel();
14+
}
1215

1316
register(fixtures.fileURL('es-module-loaders/hook-resolve-type-loader.mjs'), {
14-
data: { port: port2 },
15-
transferList: [port2],
17+
data: { sab, ESM_MODULE_INDEX, CJS_MODULE_INDEX },
1618
});
17-
18-
port1.on('message', ({ type }) => {
19-
switch (type) {
20-
case 'module':
21-
importedESM++;
22-
break;
23-
case 'commonjs':
24-
importedCJS++;
25-
break;
26-
}
27-
});
28-
29-
port1.unref();
30-
port2.unref();

0 commit comments

Comments
 (0)