|
| 1 | +// Test package map error handling: invalid JSON, missing fields, bad URLs, duplicates. |
| 2 | +import '../common/index.mjs'; |
| 3 | +import assert from 'node:assert'; |
| 4 | +import { spawnSyncAndAssert, spawnSyncAndExit } from '../common/child_process.js'; |
| 5 | +import * as fixtures from '../common/fixtures.mjs'; |
| 6 | +import tmpdir from '../common/tmpdir.js'; |
| 7 | +import { writeFileSync } from 'node:fs'; |
| 8 | +import { pathToFileURL } from 'node:url'; |
| 9 | + |
| 10 | +tmpdir.refresh(); |
| 11 | + |
| 12 | +const cwd = fixtures.path('package-map/root'); |
| 13 | + |
| 14 | +// Throws ERR_PACKAGE_MAP_INVALID for invalid JSON. |
| 15 | +spawnSyncAndExit(process.execPath, [ |
| 16 | + '--no-warnings', |
| 17 | + '--experimental-package-map', |
| 18 | + fixtures.path('package-map/package-map-invalid-syntax.json'), |
| 19 | + '--input-type=module', |
| 20 | + '--eval', `import x from 'dep-a';`, |
| 21 | +], { cwd }, { |
| 22 | + status: 1, |
| 23 | + signal: null, |
| 24 | + stderr: /ERR_PACKAGE_MAP_INVALID/, |
| 25 | +}); |
| 26 | + |
| 27 | +// Throws ERR_PACKAGE_MAP_INVALID for missing packages field. |
| 28 | +spawnSyncAndExit(process.execPath, [ |
| 29 | + '--no-warnings', |
| 30 | + '--experimental-package-map', |
| 31 | + fixtures.path('package-map/package-map-invalid-schema.json'), |
| 32 | + '--input-type=module', |
| 33 | + '--eval', `import x from 'dep-a';`, |
| 34 | +], { cwd }, { |
| 35 | + status: 1, |
| 36 | + signal: null, |
| 37 | + stderr(output) { |
| 38 | + assert.match(output, /ERR_PACKAGE_MAP_INVALID/); |
| 39 | + assert.match(output, /packages/); |
| 40 | + }, |
| 41 | +}); |
| 42 | + |
| 43 | +// Throws ERR_PACKAGE_MAP_KEY_NOT_FOUND for undefined dependency key. |
| 44 | +spawnSyncAndExit(process.execPath, [ |
| 45 | + '--no-warnings', |
| 46 | + '--experimental-package-map', |
| 47 | + fixtures.path('package-map/package-map-missing-dep.json'), |
| 48 | + '--input-type=module', |
| 49 | + '--eval', `import x from 'nonexistent';`, |
| 50 | +], { cwd }, { |
| 51 | + status: 1, |
| 52 | + signal: null, |
| 53 | + stderr: /ERR_PACKAGE_MAP_KEY_NOT_FOUND/, |
| 54 | +}); |
| 55 | + |
| 56 | +// Throws for non-existent map file. |
| 57 | +spawnSyncAndExit(process.execPath, [ |
| 58 | + '--no-warnings', |
| 59 | + '--experimental-package-map', '/nonexistent/package-map.json', |
| 60 | + '--input-type=module', |
| 61 | + '--eval', `import x from 'dep-a';`, |
| 62 | +], { cwd }, { |
| 63 | + status: 1, |
| 64 | + signal: null, |
| 65 | + stderr(output) { |
| 66 | + assert.match(output, /ERR_PACKAGE_MAP_INVALID/); |
| 67 | + assert.match(output, /no such file or directory/); |
| 68 | + }, |
| 69 | +}); |
| 70 | + |
| 71 | +// Throws ERR_PACKAGE_MAP_INVALID for unsupported URL scheme in path. |
| 72 | +spawnSyncAndExit(process.execPath, [ |
| 73 | + '--no-warnings', |
| 74 | + '--experimental-package-map', |
| 75 | + fixtures.path('package-map/package-map-https-path.json'), |
| 76 | + '--input-type=module', |
| 77 | + '--eval', `import x from 'dep-a';`, |
| 78 | +], { cwd }, { |
| 79 | + status: 1, |
| 80 | + signal: null, |
| 81 | + stderr(output) { |
| 82 | + assert.match(output, /ERR_PACKAGE_MAP_INVALID/); |
| 83 | + assert.match(output, /unsupported URL scheme/); |
| 84 | + assert.match(output, /https:\/\//); |
| 85 | + }, |
| 86 | +}); |
| 87 | + |
| 88 | +// Accepts file:// URLs in path. |
| 89 | +{ |
| 90 | + const fileUrlFixturePath = tmpdir.resolve('package-map-file-url.json'); |
| 91 | + writeFileSync(fileUrlFixturePath, JSON.stringify({ |
| 92 | + packages: { |
| 93 | + 'root': { |
| 94 | + path: pathToFileURL(fixtures.path('package-map/root')).href, |
| 95 | + dependencies: { 'dep-a': 'dep-a' }, |
| 96 | + }, |
| 97 | + 'dep-a': { |
| 98 | + path: pathToFileURL(fixtures.path('package-map/dep-a')).href, |
| 99 | + dependencies: {}, |
| 100 | + }, |
| 101 | + }, |
| 102 | + })); |
| 103 | + |
| 104 | + spawnSyncAndAssert(process.execPath, [ |
| 105 | + '--no-warnings', |
| 106 | + '--experimental-package-map', fileUrlFixturePath, |
| 107 | + '--input-type=module', |
| 108 | + '--eval', `import dep from 'dep-a'; console.log(dep);`, |
| 109 | + ], { cwd }, { stdout: /dep-a-value/, trim: true }); |
| 110 | +} |
| 111 | + |
| 112 | +// Throws ERR_PACKAGE_MAP_INVALID for duplicate package paths. |
| 113 | +spawnSyncAndExit(process.execPath, [ |
| 114 | + '--no-warnings', |
| 115 | + '--experimental-package-map', |
| 116 | + fixtures.path('package-map/package-map-duplicate-path.json'), |
| 117 | + '--input-type=module', |
| 118 | + '--eval', `import x from 'dep-a';`, |
| 119 | +], { cwd }, { |
| 120 | + status: 1, |
| 121 | + signal: null, |
| 122 | + stderr(output) { |
| 123 | + assert.match(output, /ERR_PACKAGE_MAP_INVALID/); |
| 124 | + assert.match(output, /pkg-a/); |
| 125 | + assert.match(output, /pkg-b/); |
| 126 | + }, |
| 127 | +}); |
0 commit comments