|
1 | 1 | import { spawnPromisified } from '../common/index.mjs'; |
2 | 2 | import * as fixtures from '../common/fixtures.mjs'; |
| 3 | +import tmpdir from '../common/tmpdir.js'; |
3 | 4 | import assert from 'node:assert'; |
| 5 | +import { writeFileSync } from 'node:fs'; |
4 | 6 | import { execPath } from 'node:process'; |
5 | 7 | import { describe, it } from 'node:test'; |
| 8 | +import { pathToFileURL } from 'node:url'; |
| 9 | + |
| 10 | +tmpdir.refresh(); |
6 | 11 |
|
7 | 12 | const packageMapPath = fixtures.path('package-map/package-map.json'); |
8 | 13 |
|
| 14 | +// Generated at runtime because file:// URLs must be absolute, making them machine-dependent. |
| 15 | +const fileUrlFixturePath = tmpdir.resolve('package-map-file-url.json'); |
| 16 | +writeFileSync(fileUrlFixturePath, JSON.stringify({ |
| 17 | + packages: { |
| 18 | + root: { |
| 19 | + path: pathToFileURL(fixtures.path('package-map/root')).href, |
| 20 | + dependencies: { 'dep-a': 'dep-a' }, |
| 21 | + }, |
| 22 | + 'dep-a': { |
| 23 | + path: pathToFileURL(fixtures.path('package-map/dep-a')).href, |
| 24 | + dependencies: {}, |
| 25 | + }, |
| 26 | + }, |
| 27 | +})); |
| 28 | + |
9 | 29 | describe('ESM: --experimental-package-map', () => { |
10 | 30 |
|
11 | 31 | // =========== Basic Resolution =========== |
@@ -130,6 +150,32 @@ describe('ESM: --experimental-package-map', () => { |
130 | 150 | assert.match(stderr, /not found/); |
131 | 151 | }); |
132 | 152 |
|
| 153 | + it('throws ERR_PACKAGE_MAP_INVALID for unsupported URL scheme in path', async () => { |
| 154 | + const { code, stderr } = await spawnPromisified(execPath, [ |
| 155 | + '--experimental-package-map', |
| 156 | + fixtures.path('package-map/package-map-https-path.json'), |
| 157 | + '--input-type=module', |
| 158 | + '--eval', `import x from 'dep-a';`, |
| 159 | + ], { cwd: fixtures.path('package-map/root') }); |
| 160 | + |
| 161 | + assert.notStrictEqual(code, 0); |
| 162 | + assert.match(stderr, /ERR_PACKAGE_MAP_INVALID/); |
| 163 | + assert.match(stderr, /unsupported URL scheme/); |
| 164 | + assert.match(stderr, /https:\/\//); |
| 165 | + }); |
| 166 | + |
| 167 | + it('accepts file:// URLs in path', async () => { |
| 168 | + const { code, stdout, stderr } = await spawnPromisified(execPath, [ |
| 169 | + '--experimental-package-map', fileUrlFixturePath, |
| 170 | + '--input-type=module', |
| 171 | + '--eval', |
| 172 | + `import dep from 'dep-a'; console.log(dep);`, |
| 173 | + ], { cwd: fixtures.path('package-map/root') }); |
| 174 | + |
| 175 | + assert.strictEqual(code, 0, stderr); |
| 176 | + assert.match(stdout, /dep-a-value/); |
| 177 | + }); |
| 178 | + |
133 | 179 | it('throws ERR_PACKAGE_MAP_INVALID for duplicate package paths', async () => { |
134 | 180 | const { code, stderr } = await spawnPromisified(execPath, [ |
135 | 181 | '--experimental-package-map', |
|
0 commit comments