|
1 |
| -// Flags: --loader ./test/fixtures/es-module-loaders/hook-resolve-type.mjs |
2 |
| -import { allowGlobals } from '../common/index.mjs'; |
| 1 | +import { spawnPromisified } from '../common/index.mjs'; |
3 | 2 | import * as fixtures from '../common/fixtures.mjs';
|
4 |
| -import { strict as assert } from 'assert'; |
5 |
| -import * as fs from 'fs'; |
6 |
| - |
7 |
| -allowGlobals(global.getModuleTypeStats); |
8 |
| - |
9 |
| -const { importedESM: importedESMBefore, |
10 |
| - importedCJS: importedCJSBefore } = await global.getModuleTypeStats(); |
11 |
| - |
12 |
| -const basePath = |
13 |
| - new URL('./node_modules/', import.meta.url); |
14 |
| - |
15 |
| -const rel = (file) => new URL(file, basePath); |
16 |
| -const createDir = (path) => { |
17 |
| - if (!fs.existsSync(path)) { |
18 |
| - fs.mkdirSync(path); |
19 |
| - } |
20 |
| -}; |
| 3 | +import { match } from 'node:assert'; |
| 4 | +import { mkdir, rm, cp } from 'node:fs/promises'; |
| 5 | +import { tmpdir } from 'node:os'; |
| 6 | +import { execPath } from 'node:process'; |
| 7 | +import { fileURLToPath, pathToFileURL } from 'node:url'; |
21 | 8 |
|
| 9 | +const base = new URL(`test-esm-loader-resolve-type-${(Math.random() * Date.now()).toFixed(0)}`, pathToFileURL(tmpdir())); |
22 | 10 | const moduleName = 'module-counter-by-type';
|
23 |
| -const moduleDir = rel(`${moduleName}`); |
| 11 | +const moduleURL = new URL(`${base}/node_modules/${moduleName}`); |
24 | 12 | try {
|
25 |
| - createDir(basePath); |
26 |
| - createDir(moduleDir); |
27 |
| - fs.cpSync( |
28 |
| - fixtures.path('es-modules', moduleName), |
29 |
| - moduleDir, |
| 13 | + await mkdir(moduleURL, { recursive: true }); |
| 14 | + await cp( |
| 15 | + fixtures.path('es-modules', 'module-counter-by-type'), |
| 16 | + moduleURL, |
30 | 17 | { recursive: true }
|
31 | 18 | );
|
32 | 19 |
|
| 20 | + const { stdout } = await spawnPromisified( |
| 21 | + execPath, |
| 22 | + [ |
| 23 | + '--input-type=module', |
| 24 | + '--eval', |
| 25 | + `import { getModuleTypeStats } from ${JSON.stringify(fixtures.fileURL('es-module-loaders', 'hook-resolve-type.mjs'))}; |
| 26 | + const before = getModuleTypeStats(); |
| 27 | + await import('${moduleName}'); |
| 28 | + const after = getModuleTypeStats(); |
| 29 | + console.log(JSON.stringify({ before, after }));` |
| 30 | + ], |
| 31 | + { cwd: fileURLToPath(base) }, |
| 32 | + ); |
33 | 33 |
|
34 |
| - await import(`${moduleName}`); |
| 34 | + // Dynamic import in the eval script should increment ESM counter but not CJS counter |
| 35 | + match(stdout, /{"before":{"importedESM":0,"importedCJS":0},"after":{"importedESM":1,"importedCJS":0}}/); |
35 | 36 | } finally {
|
36 |
| - fs.rmSync(basePath, { recursive: true, force: true }); |
| 37 | + await rm(base, { recursive: true, force: true }); |
37 | 38 | }
|
38 |
| - |
39 |
| -const { importedESM: importedESMAfter, |
40 |
| - importedCJS: importedCJSAfter } = await global.getModuleTypeStats(); |
41 |
| - |
42 |
| -// Dynamic import above should increment ESM counter but not CJS counter |
43 |
| -assert.strictEqual(importedESMBefore + 1, importedESMAfter); |
44 |
| -assert.strictEqual(importedCJSBefore, importedCJSAfter); |
0 commit comments