-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathresolve.test.js
321 lines (295 loc) · 7.7 KB
/
resolve.test.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
const path = require("path");
const resolve = require("../");
const fixtures = path.join(__dirname, "fixtures");
const asyncContextResolve = resolve.create({
extensions: [".js", ".json", ".node"],
resolveToContext: true
});
const syncContextResolve = resolve.create.sync({
extensions: [".js", ".json", ".node"],
resolveToContext: true
});
const issue238Resolve = resolve.create({
extensions: [".js", ".jsx", ".ts", ".tsx"],
modules: ["src/a", "src/b", "src/common", "node_modules"]
});
const preferRelativeResolve = resolve.create({
preferRelative: true
});
function testResolve(name, context, moduleName, result) {
describe(name, () => {
it("should resolve sync correctly", () => {
const filename = resolve.sync(context, moduleName);
expect(filename).toBeDefined();
expect(filename).toEqual(result);
});
it("should resolve async correctly", function (done) {
resolve(context, moduleName, function (err, filename) {
if (err) return done(err);
expect(filename).toBeDefined();
expect(filename).toEqual(result);
done();
});
});
});
}
function testResolveContext(name, context, moduleName, result) {
describe(name, () => {
it("should resolve async correctly", function (done) {
asyncContextResolve(context, moduleName, function (err, filename) {
if (err) done(err);
expect(filename).toBeDefined();
expect(filename).toEqual(result);
done();
});
});
it("should resolve sync correctly", () => {
const filename = syncContextResolve(context, moduleName);
expect(filename).toBeDefined();
expect(filename).toEqual(result);
});
});
}
describe("resolve", () => {
testResolve(
"absolute path",
fixtures,
path.join(fixtures, "main1.js"),
path.join(fixtures, "main1.js")
);
testResolve(
"file with .js",
fixtures,
"./main1.js",
path.join(fixtures, "main1.js")
);
testResolve(
"file without extension",
fixtures,
"./main1",
path.join(fixtures, "main1.js")
);
testResolve(
"another file with .js",
fixtures,
"./a.js",
path.join(fixtures, "a.js")
);
testResolve(
"another file without extension",
fixtures,
"./a",
path.join(fixtures, "a.js")
);
testResolve(
"file in module with .js",
fixtures,
"m1/a.js",
path.join(fixtures, "node_modules", "m1", "a.js")
);
testResolve(
"file in module without extension",
fixtures,
"m1/a",
path.join(fixtures, "node_modules", "m1", "a.js")
);
testResolve(
"another file in module without extension",
fixtures,
"complexm/step1",
path.join(fixtures, "node_modules", "complexm", "step1.js")
);
testResolve(
"from submodule to file in sibling module",
path.join(fixtures, "node_modules", "complexm"),
"m2/b.js",
path.join(fixtures, "node_modules", "m2", "b.js")
);
testResolve(
"from submodule to file in sibling of parent module",
path.join(fixtures, "node_modules", "complexm", "web_modules", "m1"),
"m2/b.js",
path.join(fixtures, "node_modules", "m2", "b.js")
);
testResolve(
"from nested directory to overwritten file in module",
path.join(fixtures, "multiple_modules"),
"m1/a.js",
path.join(fixtures, "multiple_modules", "node_modules", "m1", "a.js")
);
testResolve(
"from nested directory to not overwritten file in module",
path.join(fixtures, "multiple_modules"),
"m1/b.js",
path.join(fixtures, "node_modules", "m1", "b.js")
);
testResolve(
"file with query",
fixtures,
"./main1.js?query",
path.join(fixtures, "main1.js") + "?query"
);
testResolve(
"file with fragment",
fixtures,
"./main1.js#fragment",
path.join(fixtures, "main1.js") + "#fragment"
);
testResolve(
"file with fragment and query",
fixtures,
"./main1.js#fragment?query",
path.join(fixtures, "main1.js") + "#fragment?query"
);
testResolve(
"file with query and fragment",
fixtures,
"./main1.js?#fragment",
path.join(fixtures, "main1.js") + "?#fragment"
);
testResolve(
"file in module with query",
fixtures,
"m1/a?query",
path.join(fixtures, "node_modules", "m1", "a.js") + "?query"
);
testResolve(
"file in module with fragment",
fixtures,
"m1/a#fragment",
path.join(fixtures, "node_modules", "m1", "a.js") + "#fragment"
);
testResolve(
"file in module with fragment and query",
fixtures,
"m1/a#fragment?query",
path.join(fixtures, "node_modules", "m1", "a.js") + "#fragment?query"
);
testResolve(
"file in module with query and fragment",
fixtures,
"m1/a?#fragment",
path.join(fixtures, "node_modules", "m1", "a.js") + "?#fragment"
);
testResolveContext("context for fixtures", fixtures, "./", fixtures);
testResolveContext(
"context for fixtures/lib",
fixtures,
"./lib",
path.join(fixtures, "lib")
);
testResolveContext(
"context for fixtures with ..",
fixtures,
"./lib/../../fixtures/./lib/..",
fixtures
);
testResolveContext(
"context for fixtures with query",
fixtures,
"./?query",
fixtures + "?query"
);
testResolve(
"differ between directory and file, resolve file",
fixtures,
"./dirOrFile",
path.join(fixtures, "dirOrFile.js")
);
testResolve(
"differ between directory and file, resolve directory",
fixtures,
"./dirOrFile/",
path.join(fixtures, "dirOrFile", "index.js")
);
testResolve(
"find node_modules outside of node_modules",
path.join(fixtures, "browser-module", "node_modules"),
"m1/a",
path.join(fixtures, "node_modules", "m1", "a.js")
);
testResolve(
"don't crash on main field pointing to self",
fixtures,
"./main-field-self",
path.join(fixtures, "main-field-self", "index.js")
);
testResolve(
"don't crash on main field pointing to self",
fixtures,
"./main-field-self2",
path.join(fixtures, "main-field-self2", "index.js")
);
testResolve(
"handle fragment edge case (no fragment)",
fixtures,
"./no#fragment/#/#",
path.join(fixtures, "no\0#fragment/\0#", "\0#.js")
);
testResolve(
"handle fragment edge case (fragment)",
fixtures,
"./no#fragment/#/",
path.join(fixtures, "no.js") + "#fragment/#/"
);
testResolve(
"handle fragment escaping",
fixtures,
"./no\0#fragment/\0#/\0##fragment",
path.join(fixtures, "no\0#fragment/\0#", "\0#.js") + "#fragment"
);
it("should correctly resolve", function (done) {
const issue238 = path.resolve(fixtures, "issue-238");
issue238Resolve(
path.resolve(issue238, "./src/common"),
"config/myObjectFile",
function (err, filename) {
if (err) done(err);
expect(filename).toBeDefined();
expect(filename).toEqual(
path.resolve(issue238, "./src/common/config/myObjectFile.js")
);
done();
}
);
});
it("should correctly resolve with preferRelative", function (done) {
preferRelativeResolve(fixtures, "main1.js", function (err, filename) {
if (err) done(err);
expect(filename).toBeDefined();
expect(filename).toEqual(path.join(fixtures, "main1.js"));
done();
});
});
it("should correctly resolve with preferRelative", function (done) {
preferRelativeResolve(fixtures, "m1/a.js", function (err, filename) {
if (err) done(err);
expect(filename).toBeDefined();
expect(filename).toEqual(
path.join(fixtures, "node_modules", "m1", "a.js")
);
done();
});
});
it("should not crash when passing undefined as path", done => {
// @ts-expect-error testing invalid arguments
resolve(fixtures, undefined, err => {
expect(err).toBeInstanceOf(Error);
done();
});
});
it("should not crash when passing undefined as context", done => {
// @ts-expect-error testing invalid arguments
resolve({}, undefined, "./test/resolve.js", err => {
expect(err).toBeInstanceOf(Error);
done();
});
});
it("should not crash when passing undefined everywhere", done => {
// @ts-expect-error testing invalid arguments
resolve(undefined, undefined, undefined, undefined, err => {
expect(err).toBeInstanceOf(Error);
done();
});
});
});