Skip to content

Commit

Permalink
test: deflake test-esm-loader-resolve-type
Browse files Browse the repository at this point in the history
PR-URL: nodejs#50273
Fixes: nodejs#50040
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Marco Ippolito <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
  • Loading branch information
aduh95 authored and targos committed Nov 11, 2023
1 parent 24c0d60 commit ccaa0e2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 28 deletions.
19 changes: 13 additions & 6 deletions test/fixtures/es-module-loaders/hook-resolve-type-loader.mjs
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
/** @type {MessagePort} */
let port;
export function initialize(data) {
port = data.port;
/** @type {Uint8Array} */
let data;
/** @type {number} */
let ESM_MODULE_INDEX;
/** @type {number} */
let CJS_MODULE_INDEX;

export function initialize({ sab, ESM_MODULE_INDEX:e, CJS_MODULE_INDEX:c }) {
data = new Uint8Array(sab);
ESM_MODULE_INDEX = e;
CJS_MODULE_INDEX = c;
}

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

if (format === 'module' || specifier.endsWith('.mjs')) {
port.postMessage({ type: 'module' });
Atomics.add(data, ESM_MODULE_INDEX, 1);
} else if (format == null || format === 'commonjs') {
port.postMessage({ type: 'commonjs' });
Atomics.add(data, CJS_MODULE_INDEX, 1);
}

return nextResult;
Expand Down
32 changes: 10 additions & 22 deletions test/fixtures/es-module-loaders/hook-resolve-type.mjs
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
import * as fixtures from '../../common/fixtures.mjs';
import { register } from 'node:module';
import { MessageChannel } from 'node:worker_threads';

let importedESM = 0;
let importedCJS = 0;
const sab = new SharedArrayBuffer(2);
const data = new Uint8Array(sab);

const ESM_MODULE_INDEX = 0
const CJS_MODULE_INDEX = 1

export function getModuleTypeStats() {
const importedESM = Atomics.load(data, ESM_MODULE_INDEX);
const importedCJS = Atomics.load(data, CJS_MODULE_INDEX);
return { importedESM, importedCJS };
};

const { port1, port2 } = new MessageChannel();
}

register(fixtures.fileURL('es-module-loaders/hook-resolve-type-loader.mjs'), {
data: { port: port2 },
transferList: [port2],
data: { sab, ESM_MODULE_INDEX, CJS_MODULE_INDEX },
});

port1.on('message', ({ type }) => {
switch (type) {
case 'module':
importedESM++;
break;
case 'commonjs':
importedCJS++;
break;
}
});

port1.unref();
port2.unref();

0 comments on commit ccaa0e2

Please sign in to comment.