|
| 1 | +import { describe, it } from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { resolveFromImportMap } from "./importmap.ts"; |
| 4 | +import type { ImportMapData } from "./importmap.ts"; |
| 5 | + |
| 6 | +// parseImportMaps() requires a DOM — tested via the browser demo only |
| 7 | + |
| 8 | +describe("resolveFromImportMap", () => { |
| 9 | + it("resolves from global imports", () => { |
| 10 | + const map: ImportMapData = { imports: { react: "https://esm.sh/react" } }; |
| 11 | + assert.equal(resolveFromImportMap("react", map), "https://esm.sh/react"); |
| 12 | + }); |
| 13 | + |
| 14 | + it("returns undefined for unknown specifier", () => { |
| 15 | + const map: ImportMapData = { imports: { react: "https://esm.sh/react" } }; |
| 16 | + assert.equal(resolveFromImportMap("vue", map), undefined); |
| 17 | + }); |
| 18 | + |
| 19 | + it("resolves from matching scope over global imports", () => { |
| 20 | + const map: ImportMapData = { |
| 21 | + imports: { react: "https://esm.sh/react" }, |
| 22 | + scopes: { |
| 23 | + "https://example.com/app/": { react: "https://esm.sh/react@18" }, |
| 24 | + }, |
| 25 | + }; |
| 26 | + assert.equal( |
| 27 | + resolveFromImportMap("react", map, "https://example.com/app/main.js"), |
| 28 | + "https://esm.sh/react@18", |
| 29 | + ); |
| 30 | + }); |
| 31 | + |
| 32 | + it("falls back to global imports when no scope matches", () => { |
| 33 | + const map: ImportMapData = { |
| 34 | + imports: { react: "https://esm.sh/react" }, |
| 35 | + scopes: { |
| 36 | + "https://example.com/other/": { react: "https://esm.sh/react@17" }, |
| 37 | + }, |
| 38 | + }; |
| 39 | + assert.equal( |
| 40 | + resolveFromImportMap("react", map, "https://example.com/app/main.js"), |
| 41 | + "https://esm.sh/react", |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + it("prefers more specific scope", () => { |
| 46 | + const map: ImportMapData = { |
| 47 | + scopes: { |
| 48 | + "https://example.com/": { react: "https://esm.sh/react@17" }, |
| 49 | + "https://example.com/app/": { react: "https://esm.sh/react@18" }, |
| 50 | + }, |
| 51 | + }; |
| 52 | + assert.equal( |
| 53 | + resolveFromImportMap("react", map, "https://example.com/app/main.js"), |
| 54 | + "https://esm.sh/react@18", |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + it("returns undefined when imports and scopes are empty", () => { |
| 59 | + assert.equal(resolveFromImportMap("react", {}), undefined); |
| 60 | + }); |
| 61 | +}); |
0 commit comments