-
-
Notifications
You must be signed in to change notification settings - Fork 32k
esm: refactor globalPreload
tests
#49493
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
Closed
GeoffreyBooth
wants to merge
2
commits into
nodejs:main
from
GeoffreyBooth:refactor-globalpreload-tests
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,46 @@ | ||
// Flags: --loader ./test/fixtures/es-module-loaders/hook-resolve-type.mjs | ||
import { allowGlobals } from '../common/index.mjs'; | ||
import { spawnPromisified } from '../common/index.mjs'; | ||
import * as tmpdir from '../common/tmpdir.js'; | ||
import * as fixtures from '../common/fixtures.mjs'; | ||
import { strict as assert } from 'assert'; | ||
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); | ||
|
||
const rel = (file) => new URL(file, basePath); | ||
const createDir = (path) => { | ||
if (!fs.existsSync(path)) { | ||
fs.mkdirSync(path); | ||
} | ||
}; | ||
import { deepStrictEqual } from 'node:assert'; | ||
import { mkdir, rm, cp } from 'node:fs/promises'; | ||
import { execPath } from 'node:process'; | ||
|
||
const base = tmpdir.fileURL(`test-esm-loader-resolve-type-${(Math.random() * Date.now()).toFixed(0)}`); | ||
const moduleName = 'module-counter-by-type'; | ||
const moduleDir = rel(`${moduleName}`); | ||
const moduleURL = new URL(`${base}/node_modules/${moduleName}`); | ||
try { | ||
createDir(basePath); | ||
createDir(moduleDir); | ||
fs.cpSync( | ||
fixtures.path('es-modules', moduleName), | ||
moduleDir, | ||
await mkdir(moduleURL, { recursive: true }); | ||
await cp( | ||
fixtures.path('es-modules', 'module-counter-by-type'), | ||
moduleURL, | ||
{ recursive: true } | ||
); | ||
|
||
const output = await spawnPromisified( | ||
execPath, | ||
[ | ||
'--no-warnings', | ||
'--input-type=module', | ||
'--eval', | ||
`import { getModuleTypeStats } from ${JSON.stringify(fixtures.fileURL('es-module-loaders', 'hook-resolve-type.mjs'))}; | ||
const before = getModuleTypeStats(); | ||
await import(${JSON.stringify(moduleName)}); | ||
const after = getModuleTypeStats(); | ||
console.log(JSON.stringify({ before, after }));`, | ||
], | ||
{ cwd: base }, | ||
); | ||
|
||
await import(`${moduleName}`); | ||
deepStrictEqual(output, { | ||
code: 0, | ||
signal: null, | ||
stderr: '', | ||
stdout: JSON.stringify({ | ||
before: { importedESM: 0, importedCJS: 0 }, | ||
// Dynamic import in the eval script should increment ESM counter but not CJS counter | ||
after: { importedESM: 1, importedCJS: 0 }, | ||
}) + '\n', | ||
}); | ||
} finally { | ||
fs.rmSync(basePath, { recursive: true, force: true }); | ||
await rm(base, { recursive: true, force: true }); | ||
} | ||
|
||
const { importedESM: importedESMAfter, | ||
importedCJS: importedCJSAfter } = await global.getModuleTypeStats(); | ||
|
||
// Dynamic import above should increment ESM counter but not CJS counter | ||
assert.strictEqual(importedESMBefore + 1, importedESMAfter); | ||
assert.strictEqual(importedCJSBefore, importedCJSAfter); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import * as fixtures from '../../common/fixtures.mjs'; | ||
import { createRequire, register } from 'node:module'; | ||
|
||
const require = createRequire(import.meta.url); | ||
|
||
const GET_BUILTIN = `$__get_builtin_hole_${Date.now()}`; | ||
Object.defineProperty(globalThis, GET_BUILTIN, { | ||
value: builtinName => require(builtinName), | ||
enumerable: false, | ||
configurable: false, | ||
}); | ||
|
||
register(fixtures.fileURL('es-module-loaders/builtin-named-exports-loader.mjs'), { | ||
data: { | ||
GET_BUILTIN, | ||
}, | ||
}); |
18 changes: 18 additions & 0 deletions
18
test/fixtures/es-module-loaders/hook-resolve-type-loader.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** @type {MessagePort} */ | ||
let port; | ||
export function initialize(data) { | ||
port = data.port; | ||
} | ||
|
||
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' }); | ||
} else if (format == null || format === 'commonjs') { | ||
port.postMessage({ type: 'commonjs' }); | ||
} | ||
|
||
return nextResult; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,30 @@ | ||
import * as fixtures from '../../common/fixtures.mjs'; | ||
import { register } from 'node:module'; | ||
import { MessageChannel } from 'node:worker_threads'; | ||
|
||
let importedESM = 0; | ||
let importedCJS = 0; | ||
export function 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(() => { throw new Error('timeout'); }, 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); | ||
} | ||
const { port1, port2 } = new MessageChannel(); | ||
|
||
export async function resolve(specifier, context, next) { | ||
const nextResult = await next(specifier, context); | ||
const { format } = nextResult; | ||
register(fixtures.fileURL('es-module-loaders/hook-resolve-type-loader.mjs'), { | ||
data: { port: port2 }, | ||
transferList: [port2], | ||
}); | ||
|
||
if (format === 'module' || specifier.endsWith('.mjs')) { | ||
importedESM++; | ||
} else if (format == null || format === 'commonjs') { | ||
importedCJS++; | ||
port1.on('message', ({ type }) => { | ||
switch (type) { | ||
case 'module': | ||
importedESM++; | ||
break; | ||
case 'commonjs': | ||
importedCJS++; | ||
break; | ||
} | ||
}); | ||
|
||
return nextResult; | ||
} | ||
|
||
port1.unref(); | ||
port2.unref(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.