11cb0ef41Sopenharmony_ciimport { spawnPromisified } from '../common/index.mjs';
21cb0ef41Sopenharmony_ciimport fixtures from '../common/fixtures.js';
31cb0ef41Sopenharmony_ciimport assert from 'node:assert';
41cb0ef41Sopenharmony_ciimport { execPath } from 'node:process';
51cb0ef41Sopenharmony_ciimport { describe, it } from 'node:test';
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst cjsEntry = fixtures.path('es-modules', 'cjs-file.cjs');
81cb0ef41Sopenharmony_ciconst cjsImport = fixtures.fileURL('es-modules', 'cjs-file.cjs');
91cb0ef41Sopenharmony_ciconst mjsEntry = fixtures.path('es-modules', 'mjs-file.mjs');
101cb0ef41Sopenharmony_ciconst mjsImport = fixtures.fileURL('es-modules', 'mjs-file.mjs');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cidescribe('import modules using --import', { concurrency: true }, () => {
141cb0ef41Sopenharmony_ci  it('should import when using --eval', async () => {
151cb0ef41Sopenharmony_ci    const { code, signal, stderr, stdout } = await spawnPromisified(
161cb0ef41Sopenharmony_ci      execPath,
171cb0ef41Sopenharmony_ci      [
181cb0ef41Sopenharmony_ci        '--import', mjsImport,
191cb0ef41Sopenharmony_ci        '--eval', 'console.log("log")',
201cb0ef41Sopenharmony_ci      ]
211cb0ef41Sopenharmony_ci    );
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
241cb0ef41Sopenharmony_ci    assert.match(stdout, /^\.mjs file\r?\nlog\r?\n$/);
251cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
261cb0ef41Sopenharmony_ci    assert.strictEqual(signal, null);
271cb0ef41Sopenharmony_ci  });
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  it('should import when using --eval and --input-type=module', async () => {
301cb0ef41Sopenharmony_ci    const { code, signal, stderr, stdout } = await spawnPromisified(
311cb0ef41Sopenharmony_ci      execPath,
321cb0ef41Sopenharmony_ci      [
331cb0ef41Sopenharmony_ci        '--import', mjsImport,
341cb0ef41Sopenharmony_ci        '--input-type', 'module',
351cb0ef41Sopenharmony_ci        '--eval', 'console.log("log")',
361cb0ef41Sopenharmony_ci      ]
371cb0ef41Sopenharmony_ci    );
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
401cb0ef41Sopenharmony_ci    assert.match(stdout, /^\.mjs file\r?\nlog\r?\n$/);
411cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
421cb0ef41Sopenharmony_ci    assert.strictEqual(signal, null);
431cb0ef41Sopenharmony_ci  });
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci  it('should import when main entrypoint is a cjs file', async () => {
461cb0ef41Sopenharmony_ci    const { code, signal, stderr, stdout } = await spawnPromisified(
471cb0ef41Sopenharmony_ci      execPath,
481cb0ef41Sopenharmony_ci      [
491cb0ef41Sopenharmony_ci        '--import', mjsImport,
501cb0ef41Sopenharmony_ci        cjsEntry,
511cb0ef41Sopenharmony_ci      ]
521cb0ef41Sopenharmony_ci    );
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
551cb0ef41Sopenharmony_ci    assert.match(stdout, /^\.mjs file\r?\n\.cjs file\r?\n$/);
561cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
571cb0ef41Sopenharmony_ci    assert.strictEqual(signal, null);
581cb0ef41Sopenharmony_ci  });
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  it('should import mjs when entrypoint is a cjs file', async () => {
611cb0ef41Sopenharmony_ci    const { code, signal, stderr, stdout } = await spawnPromisified(
621cb0ef41Sopenharmony_ci      execPath,
631cb0ef41Sopenharmony_ci      [
641cb0ef41Sopenharmony_ci        '--import', mjsImport,
651cb0ef41Sopenharmony_ci        cjsEntry,
661cb0ef41Sopenharmony_ci      ]
671cb0ef41Sopenharmony_ci    );
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
701cb0ef41Sopenharmony_ci    assert.match(stdout, /^\.mjs file\r?\n\.cjs file\r?\n$/);
711cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
721cb0ef41Sopenharmony_ci    assert.strictEqual(signal, null);
731cb0ef41Sopenharmony_ci  });
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci  it('should import cjs when entrypoint is a mjs file', async () => {
761cb0ef41Sopenharmony_ci    const { code, signal, stderr, stdout } = await spawnPromisified(
771cb0ef41Sopenharmony_ci      execPath,
781cb0ef41Sopenharmony_ci      [
791cb0ef41Sopenharmony_ci        '--import', cjsImport,
801cb0ef41Sopenharmony_ci        mjsEntry,
811cb0ef41Sopenharmony_ci      ]
821cb0ef41Sopenharmony_ci    );
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
851cb0ef41Sopenharmony_ci    assert.match(stdout, /^\.cjs file\r?\n\.mjs file\r?\n$/);
861cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
871cb0ef41Sopenharmony_ci    assert.strictEqual(signal, null);
881cb0ef41Sopenharmony_ci  });
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci  it('should import mjs when entrypoint is a cjs file', async () => {
911cb0ef41Sopenharmony_ci    const { code, signal, stderr, stdout } = await spawnPromisified(
921cb0ef41Sopenharmony_ci      execPath,
931cb0ef41Sopenharmony_ci      [
941cb0ef41Sopenharmony_ci        '--import', mjsImport,
951cb0ef41Sopenharmony_ci        cjsEntry,
961cb0ef41Sopenharmony_ci      ]
971cb0ef41Sopenharmony_ci    );
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
1001cb0ef41Sopenharmony_ci    assert.match(stdout, /^\.mjs file\r?\n\.cjs file\r?\n$/);
1011cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
1021cb0ef41Sopenharmony_ci    assert.strictEqual(signal, null);
1031cb0ef41Sopenharmony_ci  });
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ci  it('should de-duplicate redundant `--import`s', async () => {
1061cb0ef41Sopenharmony_ci    const { code, signal, stderr, stdout } = await spawnPromisified(
1071cb0ef41Sopenharmony_ci      execPath,
1081cb0ef41Sopenharmony_ci      [
1091cb0ef41Sopenharmony_ci        '--import', mjsImport,
1101cb0ef41Sopenharmony_ci        '--import', mjsImport,
1111cb0ef41Sopenharmony_ci        '--import', mjsImport,
1121cb0ef41Sopenharmony_ci        cjsEntry,
1131cb0ef41Sopenharmony_ci      ]
1141cb0ef41Sopenharmony_ci    );
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
1171cb0ef41Sopenharmony_ci    assert.match(stdout, /^\.mjs file\r?\n\.cjs file\r?\n$/);
1181cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
1191cb0ef41Sopenharmony_ci    assert.strictEqual(signal, null);
1201cb0ef41Sopenharmony_ci  });
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ci  it('should import when running --check', async () => {
1231cb0ef41Sopenharmony_ci    const { code, signal, stderr, stdout } = await spawnPromisified(
1241cb0ef41Sopenharmony_ci      execPath,
1251cb0ef41Sopenharmony_ci      [
1261cb0ef41Sopenharmony_ci        '--import', mjsImport,
1271cb0ef41Sopenharmony_ci        '--check',
1281cb0ef41Sopenharmony_ci        cjsEntry,
1291cb0ef41Sopenharmony_ci      ]
1301cb0ef41Sopenharmony_ci    );
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
1331cb0ef41Sopenharmony_ci    assert.match(stdout, /^\.mjs file\r?\n$/);
1341cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
1351cb0ef41Sopenharmony_ci    assert.strictEqual(signal, null);
1361cb0ef41Sopenharmony_ci  });
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ci  it('should import when running --check fails', async () => {
1391cb0ef41Sopenharmony_ci    const { code, signal, stderr, stdout } = await spawnPromisified(
1401cb0ef41Sopenharmony_ci      execPath,
1411cb0ef41Sopenharmony_ci      [
1421cb0ef41Sopenharmony_ci        '--import', mjsImport,
1431cb0ef41Sopenharmony_ci        '--no-warnings',
1441cb0ef41Sopenharmony_ci        '--check',
1451cb0ef41Sopenharmony_ci        fixtures.path('es-modules', 'invalid-cjs.js'),
1461cb0ef41Sopenharmony_ci      ]
1471cb0ef41Sopenharmony_ci    );
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_ci    assert.match(stderr, /SyntaxError: Unexpected token 'export'/);
1501cb0ef41Sopenharmony_ci    assert.match(stdout, /^\.mjs file\r?\n$/);
1511cb0ef41Sopenharmony_ci    assert.strictEqual(code, 1);
1521cb0ef41Sopenharmony_ci    assert.strictEqual(signal, null);
1531cb0ef41Sopenharmony_ci  });
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ci  it('should import --require before --import', async () => {
1561cb0ef41Sopenharmony_ci    const { code, signal, stderr, stdout } = await spawnPromisified(
1571cb0ef41Sopenharmony_ci      execPath,
1581cb0ef41Sopenharmony_ci      [
1591cb0ef41Sopenharmony_ci        '--import', mjsImport,
1601cb0ef41Sopenharmony_ci        '--require', cjsEntry,
1611cb0ef41Sopenharmony_ci        '--eval', 'console.log("log")',
1621cb0ef41Sopenharmony_ci      ]
1631cb0ef41Sopenharmony_ci    );
1641cb0ef41Sopenharmony_ci
1651cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
1661cb0ef41Sopenharmony_ci    assert.match(stdout, /^\.cjs file\r?\n\.mjs file\r?\nlog\r?\n$/);
1671cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
1681cb0ef41Sopenharmony_ci    assert.strictEqual(signal, null);
1691cb0ef41Sopenharmony_ci  });
1701cb0ef41Sopenharmony_ci
1711cb0ef41Sopenharmony_ci  it('should import a module with top level await', async () => {
1721cb0ef41Sopenharmony_ci    const { code, signal, stderr, stdout } = await spawnPromisified(
1731cb0ef41Sopenharmony_ci      execPath,
1741cb0ef41Sopenharmony_ci      [
1751cb0ef41Sopenharmony_ci        '--import', fixtures.fileURL('es-modules', 'esm-top-level-await.mjs'),
1761cb0ef41Sopenharmony_ci        fixtures.path('es-modules', 'print-3.mjs'),
1771cb0ef41Sopenharmony_ci      ]
1781cb0ef41Sopenharmony_ci    );
1791cb0ef41Sopenharmony_ci
1801cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
1811cb0ef41Sopenharmony_ci    assert.match(stdout, /^1\r?\n2\r?\n3\r?\n$/);
1821cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
1831cb0ef41Sopenharmony_ci    assert.strictEqual(signal, null);
1841cb0ef41Sopenharmony_ci  });
1851cb0ef41Sopenharmony_ci});
186