1import { spawnPromisified } from '../common/index.mjs'; 2import * as tmpdir from '../common/tmpdir.js'; 3import * as fixtures from '../common/fixtures.mjs'; 4import { deepStrictEqual } from 'node:assert'; 5import { mkdir, rm, cp } from 'node:fs/promises'; 6import { execPath } from 'node:process'; 7 8const base = tmpdir.fileURL(`test-esm-loader-resolve-type-${(Math.random() * Date.now()).toFixed(0)}`); 9const moduleName = 'module-counter-by-type'; 10const moduleURL = new URL(`${base}/node_modules/${moduleName}`); 11try { 12 await mkdir(moduleURL, { recursive: true }); 13 await cp( 14 fixtures.path('es-modules', 'module-counter-by-type'), 15 moduleURL, 16 { recursive: true } 17 ); 18 19 const output = await spawnPromisified( 20 execPath, 21 [ 22 '--no-warnings', 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(${JSON.stringify(moduleName)}); 28 const after = getModuleTypeStats(); 29 console.log(JSON.stringify({ before, after }));`, 30 ], 31 { cwd: base }, 32 ); 33 34 deepStrictEqual(output, { 35 code: 0, 36 signal: null, 37 stderr: '', 38 stdout: JSON.stringify({ 39 before: { importedESM: 0, importedCJS: 0 }, 40 // Dynamic import in the eval script should increment ESM counter but not CJS counter 41 after: { importedESM: 1, importedCJS: 0 }, 42 }) + '\n', 43 }); 44} finally { 45 await rm(base, { recursive: true, force: true }); 46} 47