-
Notifications
You must be signed in to change notification settings - Fork 0
/
createResolveLinkRelations.test.mjs
173 lines (133 loc) · 5.87 KB
/
createResolveLinkRelations.test.mjs
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// @ts-check
import test from "node:test";
import assert from "node:assert/strict";
import createResolveLinkRelations from "./createResolveLinkRelations.mjs";
test("createResolveLinkRelations", async (t) => {
await t.test("works", async () => {
const resolveLinkRelations = createResolveLinkRelations("test-fixtures");
const resolvedModules = await resolveLinkRelations("/a.mjs");
assert.ok(Array.isArray(resolvedModules));
assert.ok(resolvedModules.includes("/c.mjs"));
assert.ok(resolvedModules.includes("/d.mjs"));
assert.ok(resolvedModules.includes("/lib/aa.mjs"));
assert.ok(resolvedModules.includes("/lib/bb.mjs"));
// Should not resolve dynamic imports
assert.ok(!resolvedModules.includes("/lib/cc.mjs"));
assert.equal(resolvedModules.length, 4);
const resolvedModulesCached = await resolveLinkRelations("/a.mjs");
assert.ok(Array.isArray(resolvedModulesCached));
assert.ok(resolvedModulesCached.includes("/c.mjs"));
assert.ok(resolvedModulesCached.includes("/d.mjs"));
assert.ok(resolvedModulesCached.includes("/lib/aa.mjs"));
assert.ok(resolvedModulesCached.includes("/lib/bb.mjs"));
// Should not resolve dynamic imports
assert.ok(!resolvedModulesCached.includes("/lib/cc.mjs"));
assert.equal(resolvedModulesCached.length, 4);
});
await t.test("can't reach outside of appPath", async (tt) => {
await tt.test("root module", async () => {
const resolveLinkRelations = createResolveLinkRelations("test-fixtures");
const resolvedModules = await resolveLinkRelations("../../a.mjs");
assert.equal(resolvedModules, undefined);
});
await tt.test("imports", async () => {
const resolveLinkRelations = createResolveLinkRelations("test-fixtures");
const resolvedModules = await resolveLinkRelations("./outside.mjs");
assert.equal(resolvedModules, undefined);
});
});
await t.test("module without imports", async () => {
const resolveLinkRelations = createResolveLinkRelations("test-fixtures");
const resolvedModules = await resolveLinkRelations("/d.mjs");
assert.equal(resolvedModules, undefined);
});
await t.test("module doesn't exist", async () => {
const resolveLinkRelations = createResolveLinkRelations("test-fixtures");
const resolvedModules = await resolveLinkRelations("/does-not-exist.mjs");
assert.equal(resolvedModules, undefined);
});
await t.test("resolve import maps", async (tt) => {
await tt.test("basic", async () => {
const resolveLinkRelations = createResolveLinkRelations("test-fixtures", {
importMap: '{ "imports": { "g": "./g.mjs" } }',
});
const resolvedModules = await resolveLinkRelations("/e.mjs");
assert.ok(Array.isArray(resolvedModules));
assert.ok(resolvedModules.includes("/g.mjs"));
});
await tt.test("ignores external urls", async () => {
const resolveLinkRelations = createResolveLinkRelations("test-fixtures", {
importMap:
'{ "imports": { "z": "/z.mjs", "foo": "https://foo.com/bar" } }',
});
const resolvedModules = await resolveLinkRelations("/x.mjs");
assert.ok(Array.isArray(resolvedModules));
assert.ok(resolvedModules.includes("/z.mjs"));
});
await tt.test("resolves root module", async () => {
const resolveLinkRelations = createResolveLinkRelations("test-fixtures", {
importMap: '{ "imports": { "e": "./e.mjs", "g": "./g.mjs" } }',
});
const resolvedModules = await resolveLinkRelations("e");
assert.ok(Array.isArray(resolvedModules));
assert.ok(resolvedModules.includes("/g.mjs"));
});
});
await t.test("async cache", async () => {
const cache = new Map();
const asyncCache = {
cache: new Map(),
/** @param {any} key */
async get(key) {
await new Promise((resolve) => setTimeout(resolve, 0));
return cache.get(key);
},
/**
* @param {any} key
* @param {any} value
*/
async set(key, value) {
await new Promise((resolve) => setTimeout(resolve, 0));
cache.set(key, value);
},
};
const resolveLinkRelations = createResolveLinkRelations("test-fixtures", {
cache: asyncCache,
});
const resolvedModules = await resolveLinkRelations("/a.mjs");
assert.ok(Array.isArray(resolvedModules));
assert.ok(resolvedModules.includes("/c.mjs"));
assert.ok(resolvedModules.includes("/d.mjs"));
assert.ok(resolvedModules.includes("/lib/aa.mjs"));
assert.ok(resolvedModules.includes("/lib/bb.mjs"));
// Should not resolve dynamic imports
assert.ok(!resolvedModules.includes("/lib/cc.mjs"));
assert.equal(resolvedModules.length, 4);
const resolvedModulesCached = await resolveLinkRelations("/a.mjs");
assert.ok(Array.isArray(resolvedModulesCached));
assert.ok(resolvedModulesCached.includes("/c.mjs"));
assert.ok(resolvedModulesCached.includes("/d.mjs"));
assert.ok(resolvedModulesCached.includes("/lib/aa.mjs"));
assert.ok(resolvedModulesCached.includes("/lib/bb.mjs"));
// Should not resolve dynamic imports
assert.ok(!resolvedModulesCached.includes("/lib/cc.mjs"));
assert.equal(resolvedModulesCached.length, 4);
});
await t.test("no duplicates", async () => {
const resolveLinkRelations = createResolveLinkRelations("test-fixtures");
const resolvedModules = await resolveLinkRelations("/a.mjs");
assert.ok(Array.isArray(resolvedModules));
});
await t.test("resolveSpecifier", async (tt) => {
await tt.test("basic", async () => {
const resolveLinkRelations = createResolveLinkRelations("test-fixtures");
const resolvedModules = await resolveLinkRelations("/a.mjs", {
resolveSpecifier(specifier) {
return specifier.replace("/a", "/b");
},
});
assert.ok(Array.isArray(resolvedModules));
assert.ok(resolvedModules.includes("/d.mjs"));
});
});
});