From 88e666f21026596dfad9dbb7703c58e94de3e68c Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Mon, 16 Mar 2026 14:51:18 -0400 Subject: [PATCH] test: fix esm issue in node-spec-runner (#50296) Chromium added a top-level package.json in CL:7485999 that sets the type to module and breaks commonjs tests run via node-spec-runner.js. This commit temporarily changes the type to commonjs while running the tests, then changes it back to module when done. Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Shelley Vohr --- script/node-spec-runner.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/script/node-spec-runner.js b/script/node-spec-runner.js index 9896ecfe043..1a947961aff 100644 --- a/script/node-spec-runner.js +++ b/script/node-spec-runner.js @@ -14,6 +14,7 @@ const args = minimist(process.argv.slice(2), { const BASE = path.resolve(__dirname, '../..'); +const ROOT_PACKAGE_JSON = path.resolve(BASE, 'package.json'); const NODE_DIR = path.resolve(BASE, 'third_party', 'electron_node'); const JUNIT_DIR = args.jUnitDir ? path.resolve(args.jUnitDir) : null; const TAP_FILE_NAME = 'test.tap'; @@ -38,6 +39,18 @@ const defaultOptions = [ '-J' ]; +// The root package.json is ESM, which breaks the test runner. +// Temporarily change it to CommonJS while running the tests, then +// change it back when done. +const resetPackageJson = ({ useESM }) => { + // This won't always exist in CI. + if (!fs.existsSync(ROOT_PACKAGE_JSON)) { return; } + + const packageJson = JSON.parse(fs.readFileSync(ROOT_PACKAGE_JSON, 'utf-8')); + packageJson.type = useESM ? 'module' : 'commonjs'; + fs.writeFileSync(ROOT_PACKAGE_JSON, JSON.stringify(packageJson, null, 2) + '\n'); +}; + const getCustomOptions = () => { let customOptions = ['tools/test.py']; @@ -79,6 +92,8 @@ async function main () { const options = args.default ? defaultOptions : getCustomOptions(); + resetPackageJson({ useESM: false }); + const testChild = cp.spawn('python3', options, { env: { ...process.env, @@ -88,7 +103,10 @@ async function main () { cwd: NODE_DIR, stdio: 'inherit' }); + testChild.on('exit', (testCode) => { + resetPackageJson({ useESM: true }); + if (JUNIT_DIR) { fs.mkdirSync(JUNIT_DIR); const converterStream = require('tap-xunit')();