Skip to content

Commit 8fc7530

Browse files
committed
test: add unit tests for error handling, import resolution, and source tracking
1 parent 329d7cc commit 8fc7530

7 files changed

Lines changed: 687 additions & 0 deletions

File tree

src/error.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { describe, it } from "node:test";
2+
import assert from "node:assert/strict";
3+
import { ModuleTSXError, warn } from "./error.ts";
4+
5+
describe("ModuleTSXError", () => {
6+
it("is an instance of Error", () => {
7+
const err = new ModuleTSXError("oops");
8+
assert.ok(err instanceof Error);
9+
assert.ok(err instanceof ModuleTSXError);
10+
});
11+
12+
it("has name ModuleTSXError", () => {
13+
assert.equal(new ModuleTSXError("x").name, "ModuleTSXError");
14+
});
15+
16+
it("preserves message", () => {
17+
assert.equal(new ModuleTSXError("hello").message, "hello");
18+
});
19+
20+
it("supports cause via ErrorOptions", () => {
21+
const cause = new Error("root");
22+
const err = new ModuleTSXError("wrapped", { cause });
23+
assert.equal((err as any).cause, cause);
24+
});
25+
});
26+
27+
describe("warn", () => {
28+
it("does not throw", () => {
29+
assert.doesNotThrow(() => warn("test message", 1, 2));
30+
});
31+
});

src/importmap.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

Comments
 (0)