11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst { spawnPromisified } = require('../common'); 41cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures.js'); 51cb0ef41Sopenharmony_ciconst assert = require('node:assert'); 61cb0ef41Sopenharmony_ciconst path = require('node:path'); 71cb0ef41Sopenharmony_ciconst { execPath } = require('node:process'); 81cb0ef41Sopenharmony_ciconst { describe, it } = require('node:test'); 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ciconst requiringCjsAsEsm = path.resolve(fixtures.path('/es-modules/cjs-esm.js')); 121cb0ef41Sopenharmony_ciconst requiringEsm = path.resolve(fixtures.path('/es-modules/cjs-esm-esm.js')); 131cb0ef41Sopenharmony_ciconst pjson = path.resolve( 141cb0ef41Sopenharmony_ci fixtures.path('/es-modules/package-type-module/package.json') 151cb0ef41Sopenharmony_ci); 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_cidescribe('CJS ↔︎ ESM interop warnings', { concurrency: true }, () => { 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ci it(async () => { 211cb0ef41Sopenharmony_ci const required = path.resolve( 221cb0ef41Sopenharmony_ci fixtures.path('/es-modules/package-type-module/cjs.js') 231cb0ef41Sopenharmony_ci ); 241cb0ef41Sopenharmony_ci const basename = 'cjs.js'; 251cb0ef41Sopenharmony_ci const { code, signal, stderr } = await spawnPromisified(execPath, [requiringCjsAsEsm]); 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci assert.ok( 281cb0ef41Sopenharmony_ci stderr.replaceAll('\r', '').includes( 291cb0ef41Sopenharmony_ci `Error [ERR_REQUIRE_ESM]: require() of ES Module ${required} from ${requiringCjsAsEsm} not supported.\n` 301cb0ef41Sopenharmony_ci ) 311cb0ef41Sopenharmony_ci ); 321cb0ef41Sopenharmony_ci assert.ok( 331cb0ef41Sopenharmony_ci stderr.replaceAll('\r', '').includes( 341cb0ef41Sopenharmony_ci `Instead either rename ${basename} to end in .cjs, change the requiring ` + 351cb0ef41Sopenharmony_ci 'code to use dynamic import() which is available in all CommonJS ' + 361cb0ef41Sopenharmony_ci `modules, or change "type": "module" to "type": "commonjs" in ${pjson} to ` + 371cb0ef41Sopenharmony_ci 'treat all .js files as CommonJS (using .mjs for all ES modules ' + 381cb0ef41Sopenharmony_ci 'instead).\n' 391cb0ef41Sopenharmony_ci ) 401cb0ef41Sopenharmony_ci ); 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci assert.strictEqual(code, 1); 431cb0ef41Sopenharmony_ci assert.strictEqual(signal, null); 441cb0ef41Sopenharmony_ci }); 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci it(async () => { 471cb0ef41Sopenharmony_ci const required = path.resolve( 481cb0ef41Sopenharmony_ci fixtures.path('/es-modules/package-type-module/esm.js') 491cb0ef41Sopenharmony_ci ); 501cb0ef41Sopenharmony_ci const basename = 'esm.js'; 511cb0ef41Sopenharmony_ci const { code, signal, stderr } = await spawnPromisified(execPath, [requiringEsm]); 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ci assert.ok( 541cb0ef41Sopenharmony_ci stderr.replace(/\r/g, '').includes( 551cb0ef41Sopenharmony_ci `Error [ERR_REQUIRE_ESM]: require() of ES Module ${required} from ${requiringEsm} not supported.\n` 561cb0ef41Sopenharmony_ci ) 571cb0ef41Sopenharmony_ci ); 581cb0ef41Sopenharmony_ci assert.ok( 591cb0ef41Sopenharmony_ci stderr.replace(/\r/g, '').includes( 601cb0ef41Sopenharmony_ci `Instead change the require of ${basename} in ${requiringEsm} to` + 611cb0ef41Sopenharmony_ci ' a dynamic import() which is available in all CommonJS modules.\n' 621cb0ef41Sopenharmony_ci ) 631cb0ef41Sopenharmony_ci ); 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_ci assert.strictEqual(code, 1); 661cb0ef41Sopenharmony_ci assert.strictEqual(signal, null); 671cb0ef41Sopenharmony_ci }); 681cb0ef41Sopenharmony_ci}); 69