-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
esm: refactor test-esm-named-exports
PR-URL: #49493 Backport-PR-URL: #50669 Reviewed-By: Jacob Smith <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
- Loading branch information
1 parent
65058d9
commit 9b7c9d9
Showing
4 changed files
with
59 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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,44 @@ | ||
// 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 } | ||
); | ||
|
||
|
||
await import(`${moduleName}`); | ||
deepStrictEqual(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 }, | ||
), { | ||
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', | ||
code: 0, | ||
signal: null, | ||
}); | ||
} 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 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
19 changes: 6 additions & 13 deletions
19
test/fixtures/es-module-loaders/builtin-named-exports-loader.mjs
This file contains 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 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, | ||
}, | ||
}); |