From 17823b3533741c72f550cdc037a69070f169cc23 Mon Sep 17 00:00:00 2001 From: Geoffrey Booth Date: Mon, 4 Sep 2023 12:00:11 -0700 Subject: [PATCH] esm: refactor test-esm-named-exports PR-URL: https://github.com/nodejs/node/pull/49493 Reviewed-By: Jacob Smith Reviewed-By: Antoine du Hamel --- .../test-esm-loader-resolve-type.mjs | 70 +++++++++---------- test/es-module/test-esm-named-exports.js | 2 +- test/es-module/test-esm-named-exports.mjs | 2 +- .../builtin-named-exports-loader.mjs | 19 ++--- .../builtin-named-exports.mjs | 17 +++++ 5 files changed, 60 insertions(+), 50 deletions(-) create mode 100644 test/fixtures/es-module-loaders/builtin-named-exports.mjs diff --git a/test/es-module/test-esm-loader-resolve-type.mjs b/test/es-module/test-esm-loader-resolve-type.mjs index 482320c664c5d8..05a41dd5158ed4 100644 --- a/test/es-module/test-esm-loader-resolve-type.mjs +++ b/test/es-module/test-esm-loader-resolve-type.mjs @@ -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); diff --git a/test/es-module/test-esm-named-exports.js b/test/es-module/test-esm-named-exports.js index 4e10aaca635539..2c6f67288aa57c 100644 --- a/test/es-module/test-esm-named-exports.js +++ b/test/es-module/test-esm-named-exports.js @@ -1,4 +1,4 @@ -// Flags: --experimental-loader ./test/fixtures/es-module-loaders/builtin-named-exports-loader.mjs +// Flags: --import ./test/fixtures/es-module-loaders/builtin-named-exports.mjs 'use strict'; require('../common'); diff --git a/test/es-module/test-esm-named-exports.mjs b/test/es-module/test-esm-named-exports.mjs index ce8599e68b1bf5..bbe9c96b92d9b8 100644 --- a/test/es-module/test-esm-named-exports.mjs +++ b/test/es-module/test-esm-named-exports.mjs @@ -1,4 +1,4 @@ -// Flags: --experimental-loader ./test/fixtures/es-module-loaders/builtin-named-exports-loader.mjs +// Flags: --import ./test/fixtures/es-module-loaders/builtin-named-exports.mjs import '../common/index.mjs'; import { readFile, __fromLoader } from 'fs'; import assert from 'assert'; diff --git a/test/fixtures/es-module-loaders/builtin-named-exports-loader.mjs b/test/fixtures/es-module-loaders/builtin-named-exports-loader.mjs index 7d462a53ada9d1..38fa0b3a7488aa 100644 --- a/test/fixtures/es-module-loaders/builtin-named-exports-loader.mjs +++ b/test/fixtures/es-module-loaders/builtin-named-exports-loader.mjs @@ -1,17 +1,10 @@ -import module from 'module'; +import module from 'node:module'; import { readFileSync } from 'node:fs'; -const GET_BUILTIN = `$__get_builtin_hole_${Date.now()}`; - -export function globalPreload() { - return `Object.defineProperty(globalThis, ${JSON.stringify(GET_BUILTIN)}, { - value: (builtinName) => { - return getBuiltin(builtinName); - }, - enumerable: false, - configurable: false, -}); -`; +/** @type {string} */ +let GET_BUILTIN; +export function initialize(data) { + GET_BUILTIN = data.GET_BUILTIN; } export async function resolve(specifier, context, next) { @@ -56,7 +49,7 @@ const $builtinInstance = ${GET_BUILTIN}(${JSON.stringify(builtinName)}); module.exports = $builtinInstance; module.exports.__fromLoader = true; -// We need this for CJS-module-lexer can parse the exported names. +// We need this for CJS-module-lexer can parse the exported names. ${ builtinExports .map(name => `exports.${name} = $builtinInstance.${name};`) diff --git a/test/fixtures/es-module-loaders/builtin-named-exports.mjs b/test/fixtures/es-module-loaders/builtin-named-exports.mjs new file mode 100644 index 00000000000000..123b12c26bf0c9 --- /dev/null +++ b/test/fixtures/es-module-loaders/builtin-named-exports.mjs @@ -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, + }, +});