|
1 | 1 | import { spawnPromisified } from '../common/index.mjs';
|
| 2 | +import * as fixtures from '../common/fixtures.mjs'; |
| 3 | +import { spawn } from 'node:child_process'; |
2 | 4 | import { describe, it } from 'node:test';
|
3 |
| -import { strictEqual } from 'node:assert'; |
4 |
| - |
5 |
| -describe('--experimental-detect-module', () => { |
6 |
| - it('permits ESM syntax in --eval input without requiring --input-type=module', async () => { |
7 |
| - const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [ |
8 |
| - '--experimental-detect-module', |
9 |
| - '--eval', |
10 |
| - 'import { version } from "node:process"; console.log(version);', |
11 |
| - ]); |
12 |
| - |
13 |
| - strictEqual(stderr, ''); |
14 |
| - strictEqual(stdout, `${process.version}\n`); |
15 |
| - strictEqual(code, 0); |
16 |
| - strictEqual(signal, null); |
| 5 | +import { strictEqual, match } from 'node:assert'; |
| 6 | + |
| 7 | +describe('--experimental-detect-module', { concurrency: true }, () => { |
| 8 | + describe('string input', { concurrency: true }, () => { |
| 9 | + it('permits ESM syntax in --eval input without requiring --input-type=module', async () => { |
| 10 | + const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [ |
| 11 | + '--experimental-detect-module', |
| 12 | + '--eval', |
| 13 | + 'import { version } from "node:process"; console.log(version);', |
| 14 | + ]); |
| 15 | + |
| 16 | + strictEqual(stderr, ''); |
| 17 | + strictEqual(stdout, `${process.version}\n`); |
| 18 | + strictEqual(code, 0); |
| 19 | + strictEqual(signal, null); |
| 20 | + }); |
| 21 | + |
| 22 | + // ESM is unsupported for --print via --input-type=module |
| 23 | + |
| 24 | + it('permits ESM syntax in STDIN input without requiring --input-type=module', async () => { |
| 25 | + const child = spawn(process.execPath, [ |
| 26 | + '--experimental-detect-module', |
| 27 | + ]); |
| 28 | + child.stdin.end('console.log(typeof import.meta.resolve)'); |
| 29 | + |
| 30 | + match((await child.stdout.toArray()).toString(), /^function\r?\n$/); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should be overridden by --input-type', async () => { |
| 34 | + const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ |
| 35 | + '--experimental-detect-module', |
| 36 | + '--input-type=commonjs', |
| 37 | + '--eval', |
| 38 | + 'import.meta.url', |
| 39 | + ]); |
| 40 | + |
| 41 | + match(stderr, /SyntaxError: Cannot use 'import\.meta' outside a module/); |
| 42 | + strictEqual(stdout, ''); |
| 43 | + strictEqual(code, 1); |
| 44 | + strictEqual(signal, null); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should be overridden by --experimental-default-type', async () => { |
| 48 | + const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ |
| 49 | + '--experimental-detect-module', |
| 50 | + '--experimental-default-type=commonjs', |
| 51 | + '--eval', |
| 52 | + 'import.meta.url', |
| 53 | + ]); |
| 54 | + |
| 55 | + match(stderr, /SyntaxError: Cannot use 'import\.meta' outside a module/); |
| 56 | + strictEqual(stdout, ''); |
| 57 | + strictEqual(code, 1); |
| 58 | + strictEqual(signal, null); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + describe('file input in a typeless package', { concurrency: true }, () => { |
| 63 | + for (const { testName, entryPath } of [ |
| 64 | + { |
| 65 | + testName: 'permits CommonJS syntax in a .js entry point', |
| 66 | + entryPath: fixtures.path('es-modules/package-without-type/commonjs.js'), |
| 67 | + }, |
| 68 | + { |
| 69 | + testName: 'permits ESM syntax in a .js entry point', |
| 70 | + entryPath: fixtures.path('es-modules/package-without-type/module.js'), |
| 71 | + }, |
| 72 | + { |
| 73 | + testName: 'permits CommonJS syntax in a .js file imported by a CommonJS entry point', |
| 74 | + entryPath: fixtures.path('es-modules/package-without-type/imports-commonjs.cjs'), |
| 75 | + }, |
| 76 | + { |
| 77 | + testName: 'permits ESM syntax in a .js file imported by a CommonJS entry point', |
| 78 | + entryPath: fixtures.path('es-modules/package-without-type/imports-esm.js'), |
| 79 | + }, |
| 80 | + { |
| 81 | + testName: 'permits CommonJS syntax in a .js file imported by an ESM entry point', |
| 82 | + entryPath: fixtures.path('es-modules/package-without-type/imports-commonjs.mjs'), |
| 83 | + }, |
| 84 | + { |
| 85 | + testName: 'permits ESM syntax in a .js file imported by an ESM entry point', |
| 86 | + entryPath: fixtures.path('es-modules/package-without-type/imports-esm.mjs'), |
| 87 | + }, |
| 88 | + ]) { |
| 89 | + it(testName, async () => { |
| 90 | + const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [ |
| 91 | + '--experimental-detect-module', |
| 92 | + entryPath, |
| 93 | + ]); |
| 94 | + |
| 95 | + strictEqual(stderr, ''); |
| 96 | + strictEqual(stdout, 'executed\n'); |
| 97 | + strictEqual(code, 0); |
| 98 | + strictEqual(signal, null); |
| 99 | + }); |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + describe('file input in a "type": "commonjs" package', { concurrency: true }, () => { |
| 104 | + for (const { testName, entryPath } of [ |
| 105 | + { |
| 106 | + testName: 'disallows ESM syntax in a .js entry point', |
| 107 | + entryPath: fixtures.path('es-modules/package-type-commonjs/module.js'), |
| 108 | + }, |
| 109 | + { |
| 110 | + testName: 'disallows ESM syntax in a .js file imported by a CommonJS entry point', |
| 111 | + entryPath: fixtures.path('es-modules/package-type-commonjs/imports-esm.js'), |
| 112 | + }, |
| 113 | + { |
| 114 | + testName: 'disallows ESM syntax in a .js file imported by an ESM entry point', |
| 115 | + entryPath: fixtures.path('es-modules/package-type-commonjs/imports-esm.mjs'), |
| 116 | + }, |
| 117 | + ]) { |
| 118 | + it(testName, async () => { |
| 119 | + const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [ |
| 120 | + '--experimental-detect-module', |
| 121 | + entryPath, |
| 122 | + ]); |
| 123 | + |
| 124 | + match(stderr, /SyntaxError: Unexpected token 'export'/); |
| 125 | + strictEqual(stdout, ''); |
| 126 | + strictEqual(code, 1); |
| 127 | + strictEqual(signal, null); |
| 128 | + }); |
| 129 | + } |
| 130 | + }); |
| 131 | + |
| 132 | + describe('file input in a "type": "module" package', { concurrency: true }, () => { |
| 133 | + for (const { testName, entryPath } of [ |
| 134 | + { |
| 135 | + testName: 'disallows CommonJS syntax in a .js entry point', |
| 136 | + entryPath: fixtures.path('es-modules/package-type-module/cjs.js'), |
| 137 | + }, |
| 138 | + { |
| 139 | + testName: 'disallows CommonJS syntax in a .js file imported by a CommonJS entry point', |
| 140 | + entryPath: fixtures.path('es-modules/package-type-module/imports-commonjs.cjs'), |
| 141 | + }, |
| 142 | + { |
| 143 | + testName: 'disallows CommonJS syntax in a .js file imported by an ESM entry point', |
| 144 | + entryPath: fixtures.path('es-modules/package-type-module/imports-commonjs.mjs'), |
| 145 | + }, |
| 146 | + ]) { |
| 147 | + it(testName, async () => { |
| 148 | + const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [ |
| 149 | + '--experimental-detect-module', |
| 150 | + entryPath, |
| 151 | + ]); |
| 152 | + |
| 153 | + match(stderr, /ReferenceError: module is not defined in ES module scope/); |
| 154 | + strictEqual(stdout, ''); |
| 155 | + strictEqual(code, 1); |
| 156 | + strictEqual(signal, null); |
| 157 | + }); |
| 158 | + } |
17 | 159 | });
|
18 |
| -}) |
| 160 | +}); |
0 commit comments