11cb0ef41Sopenharmony_ciimport { spawnPromisified } from '../common/index.mjs'; 21cb0ef41Sopenharmony_ciimport * as fixtures from '../common/fixtures.mjs'; 31cb0ef41Sopenharmony_ciimport { match, strictEqual } from 'node:assert'; 41cb0ef41Sopenharmony_ciimport { execPath } from 'node:process'; 51cb0ef41Sopenharmony_ciimport { describe, it } from 'node:test'; 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_cidescribe('ESM: specifier-resolution=node', { concurrency: true }, () => { 91cb0ef41Sopenharmony_ci it(async () => { 101cb0ef41Sopenharmony_ci const { code, stderr, stdout } = await spawnPromisified(execPath, [ 111cb0ef41Sopenharmony_ci '--no-warnings', 121cb0ef41Sopenharmony_ci '--experimental-specifier-resolution=node', 131cb0ef41Sopenharmony_ci '--input-type=module', 141cb0ef41Sopenharmony_ci '--eval', 151cb0ef41Sopenharmony_ci [ 161cb0ef41Sopenharmony_ci 'import { strictEqual } from "node:assert";', 171cb0ef41Sopenharmony_ci // CommonJS index.js 181cb0ef41Sopenharmony_ci `import commonjs from ${JSON.stringify(fixtures.fileURL('es-module-specifiers/package-type-commonjs'))};`, 191cb0ef41Sopenharmony_ci // ESM index.js 201cb0ef41Sopenharmony_ci `import module from ${JSON.stringify(fixtures.fileURL('es-module-specifiers/package-type-module'))};`, 211cb0ef41Sopenharmony_ci // Directory entry with main.js 221cb0ef41Sopenharmony_ci `import main from ${JSON.stringify(fixtures.fileURL('es-module-specifiers/dir-with-main'))};`, 231cb0ef41Sopenharmony_ci // Notice the trailing slash 241cb0ef41Sopenharmony_ci `import success, { explicit, implicit, implicitModule } from ${JSON.stringify(fixtures.fileURL('es-module-specifiers/'))};`, 251cb0ef41Sopenharmony_ci 'strictEqual(commonjs, "commonjs");', 261cb0ef41Sopenharmony_ci 'strictEqual(module, "module");', 271cb0ef41Sopenharmony_ci 'strictEqual(main, "main");', 281cb0ef41Sopenharmony_ci 'strictEqual(success, "success");', 291cb0ef41Sopenharmony_ci 'strictEqual(explicit, "esm");', 301cb0ef41Sopenharmony_ci 'strictEqual(implicit, "cjs");', 311cb0ef41Sopenharmony_ci 'strictEqual(implicitModule, "cjs");', 321cb0ef41Sopenharmony_ci ].join('\n'), 331cb0ef41Sopenharmony_ci ]); 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci strictEqual(stderr, ''); 361cb0ef41Sopenharmony_ci strictEqual(stdout, ''); 371cb0ef41Sopenharmony_ci strictEqual(code, 0); 381cb0ef41Sopenharmony_ci }); 391cb0ef41Sopenharmony_ci it('should work with --preserve-symlinks', async () => { 401cb0ef41Sopenharmony_ci const { code, stderr, stdout } = await spawnPromisified(execPath, [ 411cb0ef41Sopenharmony_ci '--no-warnings', 421cb0ef41Sopenharmony_ci '--experimental-specifier-resolution=node', 431cb0ef41Sopenharmony_ci '--preserve-symlinks', 441cb0ef41Sopenharmony_ci '--input-type=module', 451cb0ef41Sopenharmony_ci '--eval', 461cb0ef41Sopenharmony_ci [ 471cb0ef41Sopenharmony_ci 'import { strictEqual } from "node:assert";', 481cb0ef41Sopenharmony_ci // CommonJS index.js 491cb0ef41Sopenharmony_ci `import commonjs from ${JSON.stringify(fixtures.fileURL('es-module-specifiers/package-type-commonjs'))};`, 501cb0ef41Sopenharmony_ci // ESM index.js 511cb0ef41Sopenharmony_ci `import module from ${JSON.stringify(fixtures.fileURL('es-module-specifiers/package-type-module'))};`, 521cb0ef41Sopenharmony_ci // Directory entry with main.js 531cb0ef41Sopenharmony_ci `import main from ${JSON.stringify(fixtures.fileURL('es-module-specifiers/dir-with-main'))};`, 541cb0ef41Sopenharmony_ci // Notice the trailing slash 551cb0ef41Sopenharmony_ci `import success, { explicit, implicit, implicitModule } from ${JSON.stringify(fixtures.fileURL('es-module-specifiers/'))};`, 561cb0ef41Sopenharmony_ci 'strictEqual(commonjs, "commonjs");', 571cb0ef41Sopenharmony_ci 'strictEqual(module, "module");', 581cb0ef41Sopenharmony_ci 'strictEqual(main, "main");', 591cb0ef41Sopenharmony_ci 'strictEqual(success, "success");', 601cb0ef41Sopenharmony_ci 'strictEqual(explicit, "esm");', 611cb0ef41Sopenharmony_ci 'strictEqual(implicit, "cjs");', 621cb0ef41Sopenharmony_ci 'strictEqual(implicitModule, "cjs");', 631cb0ef41Sopenharmony_ci ].join('\n'), 641cb0ef41Sopenharmony_ci ]); 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci strictEqual(stderr, ''); 671cb0ef41Sopenharmony_ci strictEqual(stdout, ''); 681cb0ef41Sopenharmony_ci strictEqual(code, 0); 691cb0ef41Sopenharmony_ci }); 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ci it('should throw when the file doesn\'t exist', async () => { 721cb0ef41Sopenharmony_ci const { code, stderr, stdout } = await spawnPromisified(execPath, [ 731cb0ef41Sopenharmony_ci '--no-warnings', 741cb0ef41Sopenharmony_ci fixtures.path('es-module-specifiers/do-not-exist.js'), 751cb0ef41Sopenharmony_ci ]); 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci match(stderr, /Cannot find module/); 781cb0ef41Sopenharmony_ci strictEqual(stdout, ''); 791cb0ef41Sopenharmony_ci strictEqual(code, 1); 801cb0ef41Sopenharmony_ci }); 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ci it('should throw when the omitted file extension is .mjs (legacy loader doesn\'t support it)', async () => { 831cb0ef41Sopenharmony_ci const { code, stderr, stdout } = await spawnPromisified(execPath, [ 841cb0ef41Sopenharmony_ci '--no-warnings', 851cb0ef41Sopenharmony_ci '--experimental-specifier-resolution=node', 861cb0ef41Sopenharmony_ci '--input-type=module', 871cb0ef41Sopenharmony_ci '--eval', 881cb0ef41Sopenharmony_ci `import whatever from ${JSON.stringify(fixtures.fileURL('es-module-specifiers/implicit-main-type-commonjs'))};`, 891cb0ef41Sopenharmony_ci ]); 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci match(stderr, /ERR_MODULE_NOT_FOUND/); 921cb0ef41Sopenharmony_ci strictEqual(stdout, ''); 931cb0ef41Sopenharmony_ci strictEqual(code, 1); 941cb0ef41Sopenharmony_ci }); 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci for ( 971cb0ef41Sopenharmony_ci const item of [ 981cb0ef41Sopenharmony_ci 'package-type-commonjs', 991cb0ef41Sopenharmony_ci 'package-type-module', 1001cb0ef41Sopenharmony_ci '/', 1011cb0ef41Sopenharmony_ci '/index', 1021cb0ef41Sopenharmony_ci ] 1031cb0ef41Sopenharmony_ci ) it('should ', async () => { 1041cb0ef41Sopenharmony_ci const { code } = await spawnPromisified(execPath, [ 1051cb0ef41Sopenharmony_ci '--no-warnings', 1061cb0ef41Sopenharmony_ci '--experimental-specifier-resolution=node', 1071cb0ef41Sopenharmony_ci '--es-module-specifier-resolution=node', 1081cb0ef41Sopenharmony_ci fixtures.path('es-module-specifiers', item), 1091cb0ef41Sopenharmony_ci ]); 1101cb0ef41Sopenharmony_ci 1111cb0ef41Sopenharmony_ci strictEqual(code, 0); 1121cb0ef41Sopenharmony_ci }); 1131cb0ef41Sopenharmony_ci}); 114