source: trip-planner-front/node_modules/resolve/test/resolver.js@ 6fe77af

Last change on this file since 6fe77af was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 14.8 KB
Line 
1var path = require('path');
2var test = require('tape');
3var resolve = require('../');
4
5test('async foo', function (t) {
6 t.plan(12);
7 var dir = path.join(__dirname, 'resolver');
8
9 resolve('./foo', { basedir: dir }, function (err, res, pkg) {
10 if (err) t.fail(err);
11 t.equal(res, path.join(dir, 'foo.js'));
12 t.equal(pkg && pkg.name, 'resolve');
13 });
14
15 resolve('./foo.js', { basedir: dir }, function (err, res, pkg) {
16 if (err) t.fail(err);
17 t.equal(res, path.join(dir, 'foo.js'));
18 t.equal(pkg && pkg.name, 'resolve');
19 });
20
21 resolve('./foo', { basedir: dir, 'package': { main: 'resolver' } }, function (err, res, pkg) {
22 if (err) t.fail(err);
23 t.equal(res, path.join(dir, 'foo.js'));
24 t.equal(pkg && pkg.main, 'resolver');
25 });
26
27 resolve('./foo.js', { basedir: dir, 'package': { main: 'resolver' } }, function (err, res, pkg) {
28 if (err) t.fail(err);
29 t.equal(res, path.join(dir, 'foo.js'));
30 t.equal(pkg.main, 'resolver');
31 });
32
33 resolve('./foo', { basedir: dir, filename: path.join(dir, 'baz.js') }, function (err, res) {
34 if (err) t.fail(err);
35 t.equal(res, path.join(dir, 'foo.js'));
36 });
37
38 resolve('foo', { basedir: dir }, function (err) {
39 t.equal(err.message, "Cannot find module 'foo' from '" + path.resolve(dir) + "'");
40 t.equal(err.code, 'MODULE_NOT_FOUND');
41 });
42
43 // Test that filename is reported as the "from" value when passed.
44 resolve('foo', { basedir: dir, filename: path.join(dir, 'baz.js') }, function (err) {
45 t.equal(err.message, "Cannot find module 'foo' from '" + path.join(dir, 'baz.js') + "'");
46 });
47});
48
49test('bar', function (t) {
50 t.plan(6);
51 var dir = path.join(__dirname, 'resolver');
52
53 resolve('foo', { basedir: dir + '/bar' }, function (err, res, pkg) {
54 if (err) t.fail(err);
55 t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
56 t.equal(pkg, undefined);
57 });
58
59 resolve('foo', { basedir: dir + '/bar' }, function (err, res, pkg) {
60 if (err) t.fail(err);
61 t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
62 t.equal(pkg, undefined);
63 });
64
65 resolve('foo', { basedir: dir + '/bar', 'package': { main: 'bar' } }, function (err, res, pkg) {
66 if (err) t.fail(err);
67 t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
68 t.equal(pkg.main, 'bar');
69 });
70});
71
72test('baz', function (t) {
73 t.plan(4);
74 var dir = path.join(__dirname, 'resolver');
75
76 resolve('./baz', { basedir: dir }, function (err, res, pkg) {
77 if (err) t.fail(err);
78 t.equal(res, path.join(dir, 'baz/quux.js'));
79 t.equal(pkg.main, 'quux.js');
80 });
81
82 resolve('./baz', { basedir: dir, 'package': { main: 'resolver' } }, function (err, res, pkg) {
83 if (err) t.fail(err);
84 t.equal(res, path.join(dir, 'baz/quux.js'));
85 t.equal(pkg.main, 'quux.js');
86 });
87});
88
89test('biz', function (t) {
90 t.plan(24);
91 var dir = path.join(__dirname, 'resolver/biz/node_modules');
92
93 resolve('./grux', { basedir: dir }, function (err, res, pkg) {
94 if (err) t.fail(err);
95 t.equal(res, path.join(dir, 'grux/index.js'));
96 t.equal(pkg, undefined);
97 });
98
99 resolve('./grux', { basedir: dir, 'package': { main: 'biz' } }, function (err, res, pkg) {
100 if (err) t.fail(err);
101 t.equal(res, path.join(dir, 'grux/index.js'));
102 t.equal(pkg.main, 'biz');
103 });
104
105 resolve('./garply', { basedir: dir }, function (err, res, pkg) {
106 if (err) t.fail(err);
107 t.equal(res, path.join(dir, 'garply/lib/index.js'));
108 t.equal(pkg.main, './lib');
109 });
110
111 resolve('./garply', { basedir: dir, 'package': { main: 'biz' } }, function (err, res, pkg) {
112 if (err) t.fail(err);
113 t.equal(res, path.join(dir, 'garply/lib/index.js'));
114 t.equal(pkg.main, './lib');
115 });
116
117 resolve('tiv', { basedir: dir + '/grux' }, function (err, res, pkg) {
118 if (err) t.fail(err);
119 t.equal(res, path.join(dir, 'tiv/index.js'));
120 t.equal(pkg, undefined);
121 });
122
123 resolve('tiv', { basedir: dir + '/grux', 'package': { main: 'grux' } }, function (err, res, pkg) {
124 if (err) t.fail(err);
125 t.equal(res, path.join(dir, 'tiv/index.js'));
126 t.equal(pkg.main, 'grux');
127 });
128
129 resolve('tiv', { basedir: dir + '/garply' }, function (err, res, pkg) {
130 if (err) t.fail(err);
131 t.equal(res, path.join(dir, 'tiv/index.js'));
132 t.equal(pkg, undefined);
133 });
134
135 resolve('tiv', { basedir: dir + '/garply', 'package': { main: './lib' } }, function (err, res, pkg) {
136 if (err) t.fail(err);
137 t.equal(res, path.join(dir, 'tiv/index.js'));
138 t.equal(pkg.main, './lib');
139 });
140
141 resolve('grux', { basedir: dir + '/tiv' }, function (err, res, pkg) {
142 if (err) t.fail(err);
143 t.equal(res, path.join(dir, 'grux/index.js'));
144 t.equal(pkg, undefined);
145 });
146
147 resolve('grux', { basedir: dir + '/tiv', 'package': { main: 'tiv' } }, function (err, res, pkg) {
148 if (err) t.fail(err);
149 t.equal(res, path.join(dir, 'grux/index.js'));
150 t.equal(pkg.main, 'tiv');
151 });
152
153 resolve('garply', { basedir: dir + '/tiv' }, function (err, res, pkg) {
154 if (err) t.fail(err);
155 t.equal(res, path.join(dir, 'garply/lib/index.js'));
156 t.equal(pkg.main, './lib');
157 });
158
159 resolve('garply', { basedir: dir + '/tiv', 'package': { main: 'tiv' } }, function (err, res, pkg) {
160 if (err) t.fail(err);
161 t.equal(res, path.join(dir, 'garply/lib/index.js'));
162 t.equal(pkg.main, './lib');
163 });
164});
165
166test('quux', function (t) {
167 t.plan(2);
168 var dir = path.join(__dirname, 'resolver/quux');
169
170 resolve('./foo', { basedir: dir, 'package': { main: 'quux' } }, function (err, res, pkg) {
171 if (err) t.fail(err);
172 t.equal(res, path.join(dir, 'foo/index.js'));
173 t.equal(pkg.main, 'quux');
174 });
175});
176
177test('normalize', function (t) {
178 t.plan(2);
179 var dir = path.join(__dirname, 'resolver/biz/node_modules/grux');
180
181 resolve('../grux', { basedir: dir }, function (err, res, pkg) {
182 if (err) t.fail(err);
183 t.equal(res, path.join(dir, 'index.js'));
184 t.equal(pkg, undefined);
185 });
186});
187
188test('cup', function (t) {
189 t.plan(5);
190 var dir = path.join(__dirname, 'resolver');
191
192 resolve('./cup', { basedir: dir, extensions: ['.js', '.coffee'] }, function (err, res) {
193 if (err) t.fail(err);
194 t.equal(res, path.join(dir, 'cup.coffee'));
195 });
196
197 resolve('./cup.coffee', { basedir: dir }, function (err, res) {
198 if (err) t.fail(err);
199 t.equal(res, path.join(dir, 'cup.coffee'));
200 });
201
202 resolve('./cup', { basedir: dir, extensions: ['.js'] }, function (err, res) {
203 t.equal(err.message, "Cannot find module './cup' from '" + path.resolve(dir) + "'");
204 t.equal(err.code, 'MODULE_NOT_FOUND');
205 });
206
207 // Test that filename is reported as the "from" value when passed.
208 resolve('./cup', { basedir: dir, extensions: ['.js'], filename: path.join(dir, 'cupboard.js') }, function (err, res) {
209 t.equal(err.message, "Cannot find module './cup' from '" + path.join(dir, 'cupboard.js') + "'");
210 });
211});
212
213test('mug', function (t) {
214 t.plan(3);
215 var dir = path.join(__dirname, 'resolver');
216
217 resolve('./mug', { basedir: dir }, function (err, res) {
218 if (err) t.fail(err);
219 t.equal(res, path.join(dir, 'mug.js'));
220 });
221
222 resolve('./mug', { basedir: dir, extensions: ['.coffee', '.js'] }, function (err, res) {
223 if (err) t.fail(err);
224 t.equal(res, path.join(dir, '/mug.coffee'));
225 });
226
227 resolve('./mug', { basedir: dir, extensions: ['.js', '.coffee'] }, function (err, res) {
228 t.equal(res, path.join(dir, '/mug.js'));
229 });
230});
231
232test('other path', function (t) {
233 t.plan(6);
234 var resolverDir = path.join(__dirname, 'resolver');
235 var dir = path.join(resolverDir, 'bar');
236 var otherDir = path.join(resolverDir, 'other_path');
237
238 resolve('root', { basedir: dir, paths: [otherDir] }, function (err, res) {
239 if (err) t.fail(err);
240 t.equal(res, path.join(resolverDir, 'other_path/root.js'));
241 });
242
243 resolve('lib/other-lib', { basedir: dir, paths: [otherDir] }, function (err, res) {
244 if (err) t.fail(err);
245 t.equal(res, path.join(resolverDir, 'other_path/lib/other-lib.js'));
246 });
247
248 resolve('root', { basedir: dir }, function (err, res) {
249 t.equal(err.message, "Cannot find module 'root' from '" + path.resolve(dir) + "'");
250 t.equal(err.code, 'MODULE_NOT_FOUND');
251 });
252
253 resolve('zzz', { basedir: dir, paths: [otherDir] }, function (err, res) {
254 t.equal(err.message, "Cannot find module 'zzz' from '" + path.resolve(dir) + "'");
255 t.equal(err.code, 'MODULE_NOT_FOUND');
256 });
257});
258
259test('path iterator', function (t) {
260 t.plan(2);
261
262 var resolverDir = path.join(__dirname, 'resolver');
263
264 var exactIterator = function (x, start, getPackageCandidates, opts) {
265 return [path.join(resolverDir, x)];
266 };
267
268 resolve('baz', { packageIterator: exactIterator }, function (err, res, pkg) {
269 if (err) t.fail(err);
270 t.equal(res, path.join(resolverDir, 'baz/quux.js'));
271 t.equal(pkg && pkg.name, 'baz');
272 });
273});
274
275test('incorrect main', function (t) {
276 t.plan(1);
277
278 var resolverDir = path.join(__dirname, 'resolver');
279 var dir = path.join(resolverDir, 'incorrect_main');
280
281 resolve('./incorrect_main', { basedir: resolverDir }, function (err, res, pkg) {
282 if (err) t.fail(err);
283 t.equal(res, path.join(dir, 'index.js'));
284 });
285});
286
287test('without basedir', function (t) {
288 t.plan(1);
289
290 var dir = path.join(__dirname, 'resolver/without_basedir');
291 var tester = require(path.join(dir, 'main.js'));
292
293 tester(t, function (err, res, pkg) {
294 if (err) {
295 t.fail(err);
296 } else {
297 t.equal(res, path.join(dir, 'node_modules/mymodule.js'));
298 }
299 });
300});
301
302test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is a file of the same name', function (t) {
303 t.plan(2);
304
305 var dir = path.join(__dirname, 'resolver');
306
307 resolve('./foo', { basedir: path.join(dir, 'same_names') }, function (err, res, pkg) {
308 if (err) t.fail(err);
309 t.equal(res, path.join(dir, 'same_names/foo.js'));
310 });
311
312 resolve('./foo/', { basedir: path.join(dir, 'same_names') }, function (err, res, pkg) {
313 if (err) t.fail(err);
314 t.equal(res, path.join(dir, 'same_names/foo/index.js'));
315 });
316});
317
318test('#211 - incorrectly resolves module-paths like "." when from inside a folder with a sibling file of the same name', function (t) {
319 t.plan(2);
320
321 var dir = path.join(__dirname, 'resolver');
322
323 resolve('./', { basedir: path.join(dir, 'same_names/foo') }, function (err, res, pkg) {
324 if (err) t.fail(err);
325 t.equal(res, path.join(dir, 'same_names/foo/index.js'));
326 });
327
328 resolve('.', { basedir: path.join(dir, 'same_names/foo') }, function (err, res, pkg) {
329 if (err) t.fail(err);
330 t.equal(res, path.join(dir, 'same_names/foo/index.js'));
331 });
332});
333
334test('async: #121 - treating an existing file as a dir when no basedir', function (t) {
335 var testFile = path.basename(__filename);
336
337 t.test('sanity check', function (st) {
338 st.plan(1);
339 resolve('./' + testFile, function (err, res, pkg) {
340 if (err) t.fail(err);
341 st.equal(res, __filename, 'sanity check');
342 });
343 });
344
345 t.test('with a fake directory', function (st) {
346 st.plan(4);
347
348 resolve('./' + testFile + '/blah', function (err, res, pkg) {
349 st.ok(err, 'there is an error');
350 st.notOk(res, 'no result');
351
352 st.equal(err && err.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
353 st.equal(
354 err && err.message,
355 'Cannot find module \'./' + testFile + '/blah\' from \'' + __dirname + '\'',
356 'can not find nonexistent module'
357 );
358 st.end();
359 });
360 });
361
362 t.end();
363});
364
365test('async dot main', function (t) {
366 var start = new Date();
367 t.plan(3);
368 resolve('./resolver/dot_main', function (err, ret) {
369 t.notOk(err);
370 t.equal(ret, path.join(__dirname, 'resolver/dot_main/index.js'));
371 t.ok(new Date() - start < 50, 'resolve.sync timedout');
372 t.end();
373 });
374});
375
376test('async dot slash main', function (t) {
377 var start = new Date();
378 t.plan(3);
379 resolve('./resolver/dot_slash_main', function (err, ret) {
380 t.notOk(err);
381 t.equal(ret, path.join(__dirname, 'resolver/dot_slash_main/index.js'));
382 t.ok(new Date() - start < 50, 'resolve.sync timedout');
383 t.end();
384 });
385});
386
387test('not a directory', function (t) {
388 t.plan(6);
389 var path = './foo';
390 resolve(path, { basedir: __filename }, function (err, res, pkg) {
391 t.ok(err, 'a non-directory errors');
392 t.equal(arguments.length, 1);
393 t.equal(res, undefined);
394 t.equal(pkg, undefined);
395
396 t.equal(err && err.message, 'Cannot find module \'' + path + '\' from \'' + __filename + '\'');
397 t.equal(err && err.code, 'MODULE_NOT_FOUND');
398 });
399});
400
401test('non-string "main" field in package.json', function (t) {
402 t.plan(5);
403
404 var dir = path.join(__dirname, 'resolver');
405 resolve('./invalid_main', { basedir: dir }, function (err, res, pkg) {
406 t.ok(err, 'errors on non-string main');
407 t.equal(err.message, 'package “invalid main” `main` must be a string');
408 t.equal(err.code, 'INVALID_PACKAGE_MAIN');
409 t.equal(res, undefined, 'res is undefined');
410 t.equal(pkg, undefined, 'pkg is undefined');
411 });
412});
413
414test('non-string "main" field in package.json', function (t) {
415 t.plan(5);
416
417 var dir = path.join(__dirname, 'resolver');
418 resolve('./invalid_main', { basedir: dir }, function (err, res, pkg) {
419 t.ok(err, 'errors on non-string main');
420 t.equal(err.message, 'package “invalid main” `main` must be a string');
421 t.equal(err.code, 'INVALID_PACKAGE_MAIN');
422 t.equal(res, undefined, 'res is undefined');
423 t.equal(pkg, undefined, 'pkg is undefined');
424 });
425});
426
427test('browser field in package.json', function (t) {
428 t.plan(3);
429
430 var dir = path.join(__dirname, 'resolver');
431 resolve(
432 './browser_field',
433 {
434 basedir: dir,
435 packageFilter: function packageFilter(pkg) {
436 if (pkg.browser) {
437 pkg.main = pkg.browser; // eslint-disable-line no-param-reassign
438 delete pkg.browser; // eslint-disable-line no-param-reassign
439 }
440 return pkg;
441 }
442 },
443 function (err, res, pkg) {
444 if (err) t.fail(err);
445 t.equal(res, path.join(dir, 'browser_field', 'b.js'));
446 t.equal(pkg && pkg.main, 'b');
447 t.equal(pkg && pkg.browser, undefined);
448 }
449 );
450});
Note: See TracBrowser for help on using the repository browser.