|
| 1 | +import { spawnPromisified } from '../common/index.mjs'; |
| 2 | +import * as fixtures from '../common/fixtures.mjs'; |
| 3 | +import assert from 'node:assert'; |
| 4 | +import { execPath } from 'node:process'; |
| 5 | +import { describe, it } from 'node:test'; |
| 6 | + |
| 7 | +// Helper function to assert the spawned process |
| 8 | +async function assertSpawnedProcess(args, options = {}, expected = {}) { |
| 9 | + const { code, signal, stderr, stdout } = await spawnPromisified(execPath, args, options); |
| 10 | + |
| 11 | + if (expected.stderr) { |
| 12 | + assert.match(stderr, expected.stderr); |
| 13 | + } |
| 14 | + |
| 15 | + if (expected.stdout) { |
| 16 | + assert.match(stdout, expected.stdout); |
| 17 | + } |
| 18 | + |
| 19 | + assert.strictEqual(code, expected.code ?? 0); |
| 20 | + assert.strictEqual(signal, expected.signal ?? null); |
| 21 | +} |
| 22 | + |
| 23 | +// Common expectation for experimental feature warning in stderr |
| 24 | +const experimentalFeatureWarning = { stderr: /--entry-url is an experimental feature/ }; |
| 25 | + |
| 26 | +describe('--entry-url', { concurrency: true }, () => { |
| 27 | + it('should reject loading a path that contains %', async () => { |
| 28 | + await assertSpawnedProcess( |
| 29 | + ['--entry-url', './test-esm-double-encoding-native%20.mjs'], |
| 30 | + { cwd: fixtures.fileURL('es-modules') }, |
| 31 | + { |
| 32 | + code: 1, |
| 33 | + stderr: /ERR_MODULE_NOT_FOUND/, |
| 34 | + } |
| 35 | + ); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should support loading properly encoded Unix path', async () => { |
| 39 | + await assertSpawnedProcess( |
| 40 | + ['--entry-url', fixtures.fileURL('es-modules/test-esm-double-encoding-native%20.mjs').pathname], |
| 41 | + {}, |
| 42 | + experimentalFeatureWarning |
| 43 | + ); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should support loading absolute URLs', async () => { |
| 47 | + await assertSpawnedProcess( |
| 48 | + ['--entry-url', fixtures.fileURL('printA.js')], |
| 49 | + {}, |
| 50 | + { |
| 51 | + ...experimentalFeatureWarning, |
| 52 | + stdout: /^A\r?\n$/, |
| 53 | + } |
| 54 | + ); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should support loading relative URLs', async () => { |
| 58 | + await assertSpawnedProcess( |
| 59 | + ['--entry-url', 'es-modules/print-entrypoint.mjs?key=value#hash'], |
| 60 | + { cwd: fixtures.fileURL('./') }, |
| 61 | + { |
| 62 | + ...experimentalFeatureWarning, |
| 63 | + stdout: /print-entrypoint\.mjs\?key=value#hash\r?\n$/, |
| 64 | + } |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should support loading `data:` URLs', async () => { |
| 69 | + await assertSpawnedProcess( |
| 70 | + ['--entry-url', 'data:text/javascript,console.log(import.meta.url)'], |
| 71 | + {}, |
| 72 | + { |
| 73 | + ...experimentalFeatureWarning, |
| 74 | + stdout: /^data:text\/javascript,console\.log\(import\.meta\.url\)\r?\n$/, |
| 75 | + } |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should support loading TypeScript URLs', async () => { |
| 80 | + const typescriptUrls = [ |
| 81 | + 'typescript/cts/test-require-ts-file.cts', |
| 82 | + 'typescript/mts/test-import-ts-file.mts', |
| 83 | + ]; |
| 84 | + |
| 85 | + for (const url of typescriptUrls) { |
| 86 | + await assertSpawnedProcess( |
| 87 | + ['--entry-url', '--experimental-strip-types', fixtures.fileURL(url)], |
| 88 | + {}, |
| 89 | + { |
| 90 | + ...experimentalFeatureWarning, |
| 91 | + stdout: /Hello, TypeScript!/, |
| 92 | + } |
| 93 | + ); |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | +}); |
0 commit comments