-
Notifications
You must be signed in to change notification settings - Fork 22
/
eszip_test.ts
72 lines (65 loc) · 1.82 KB
/
eszip_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { build, Parser } from "./mod.ts";
import {
assert,
assertEquals,
assertRejects,
} from "https://deno.land/std@0.123.0/testing/asserts.ts";
Deno.test("roundtrip build + parse", async () => {
const eszip = await build([
"https://example.com/mod.ts",
"https://example.com/a.ts",
"external:main.js",
], async (specifier: string) => {
if (specifier === "external:main.js") {
return {
kind: "external",
specifier,
};
}
if (specifier === "https://example.com/a.ts") {
return {
kind: "module",
specifier,
headers: {
"content-type": "text/typescript",
},
content: "export const a = 1;",
};
}
return {
kind: "module",
specifier: "https://example.com/mod.ts",
headers: {
"content-type": "application/typescript",
},
content: `import "https://example.com/a.ts";`,
};
});
assert(eszip instanceof Uint8Array);
const parser = new Parser();
const specifiers = await parser.parseBytes(eszip);
assertEquals(specifiers, [
"https://example.com/mod.ts",
"https://example.com/a.ts",
]);
await parser.load();
const mod = await parser.getModuleSource("https://example.com/mod.ts");
assertEquals(mod, 'import "https://example.com/a.ts";\n');
const a = await parser.getModuleSource("https://example.com/a.ts");
assertEquals(a, "export const a = 1;\n");
});
Deno.test("build default loader", async () => {
const eszip = await build(["https://deno.land/std@0.123.0/fs/mod.ts"]);
assert(eszip instanceof Uint8Array);
});
Deno.test("loader errors", async () => {
await assertRejects(
() =>
build(
["https://deno.land/std@0.123.0/fs/mod.ts"],
(specifier: string) => Promise.reject(new Error("oops")),
),
undefined,
"oops",
);
});