1 var path = require('path');
2 var test = require('tape');
3 var resolve = require('../');
5 test('async foo', function (t) {
7 var dir = path.join(__dirname, 'resolver');
9 resolve('./foo', { basedir: dir }, function (err, res, pkg) {
11 t.equal(res, path.join(dir, 'foo.js'));
12 t.equal(pkg && pkg.name, 'resolve');
15 resolve('./foo.js', { basedir: dir }, function (err, res, pkg) {
17 t.equal(res, path.join(dir, 'foo.js'));
18 t.equal(pkg && pkg.name, 'resolve');
21 resolve('./foo', { basedir: dir, 'package': { main: 'resolver' } }, function (err, res, pkg) {
23 t.equal(res, path.join(dir, 'foo.js'));
24 t.equal(pkg && pkg.main, 'resolver');
27 resolve('./foo.js', { basedir: dir, 'package': { main: 'resolver' } }, function (err, res, pkg) {
29 t.equal(res, path.join(dir, 'foo.js'));
30 t.equal(pkg.main, 'resolver');
33 resolve('./foo', { basedir: dir, filename: path.join(dir, 'baz.js') }, function (err, res) {
35 t.equal(res, path.join(dir, 'foo.js'));
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');
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') + "'");
49 test('bar', function (t) {
51 var dir = path.join(__dirname, 'resolver');
53 resolve('foo', { basedir: dir + '/bar' }, function (err, res, pkg) {
55 t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
56 t.equal(pkg, undefined);
59 resolve('foo', { basedir: dir + '/bar' }, function (err, res, pkg) {
61 t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
62 t.equal(pkg, undefined);
65 resolve('foo', { basedir: dir + '/bar', 'package': { main: 'bar' } }, function (err, res, pkg) {
67 t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
68 t.equal(pkg.main, 'bar');
72 test('baz', function (t) {
74 var dir = path.join(__dirname, 'resolver');
76 resolve('./baz', { basedir: dir }, function (err, res, pkg) {
78 t.equal(res, path.join(dir, 'baz/quux.js'));
79 t.equal(pkg.main, 'quux.js');
82 resolve('./baz', { basedir: dir, 'package': { main: 'resolver' } }, function (err, res, pkg) {
84 t.equal(res, path.join(dir, 'baz/quux.js'));
85 t.equal(pkg.main, 'quux.js');
89 test('biz', function (t) {
91 var dir = path.join(__dirname, 'resolver/biz/node_modules');
93 resolve('./grux', { basedir: dir }, function (err, res, pkg) {
95 t.equal(res, path.join(dir, 'grux/index.js'));
96 t.equal(pkg, undefined);
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');
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');
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');
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);
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');
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);
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');
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);
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');
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');
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');
166 test('quux', function (t) {
168 var dir = path.join(__dirname, 'resolver/quux');
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');
177 test('normalize', function (t) {
179 var dir = path.join(__dirname, 'resolver/biz/node_modules/grux');
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);
188 test('cup', function (t) {
190 var dir = path.join(__dirname, 'resolver');
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'));
197 resolve('./cup.coffee', { basedir: dir }, function (err, res) {
198 if (err) t.fail(err);
199 t.equal(res, path.join(dir, 'cup.coffee'));
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');
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') + "'");
213 test('mug', function (t) {
215 var dir = path.join(__dirname, 'resolver');
217 resolve('./mug', { basedir: dir }, function (err, res) {
218 if (err) t.fail(err);
219 t.equal(res, path.join(dir, 'mug.js'));
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'));
227 resolve('./mug', { basedir: dir, extensions: ['.js', '.coffee'] }, function (err, res) {
228 t.equal(res, path.join(dir, '/mug.js'));
232 test('other path', function (t) {
234 var resolverDir = path.join(__dirname, 'resolver');
235 var dir = path.join(resolverDir, 'bar');
236 var otherDir = path.join(resolverDir, 'other_path');
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'));
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'));
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');
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');
259 test('path iterator', function (t) {
262 var resolverDir = path.join(__dirname, 'resolver');
264 var exactIterator = function (x, start, getPackageCandidates, opts) {
265 return [path.join(resolverDir, x)];
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');
275 test('incorrect main', function (t) {
278 var resolverDir = path.join(__dirname, 'resolver');
279 var dir = path.join(resolverDir, 'incorrect_main');
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'));
287 test('without basedir', function (t) {
290 var dir = path.join(__dirname, 'resolver/without_basedir');
291 var tester = require(path.join(dir, 'main.js'));
293 tester(t, function (err, res, pkg) {
297 t.equal(res, path.join(dir, 'node_modules/mymodule.js'));
302 test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is a file of the same name', function (t) {
305 var dir = path.join(__dirname, 'resolver');
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'));
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'));
318 test('#211 - incorrectly resolves module-paths like "." when from inside a folder with a sibling file of the same name', function (t) {
321 var dir = path.join(__dirname, 'resolver');
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'));
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'));
334 test('async: #121 - treating an existing file as a dir when no basedir', function (t) {
335 var testFile = path.basename(__filename);
337 t.test('sanity check', function (st) {
339 resolve('./' + testFile, function (err, res, pkg) {
340 if (err) t.fail(err);
341 st.equal(res, __filename, 'sanity check');
345 t.test('with a fake directory', function (st) {
348 resolve('./' + testFile + '/blah', function (err, res, pkg) {
349 st.ok(err, 'there is an error');
350 st.notOk(res, 'no result');
352 st.equal(err && err.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
355 'Cannot find module \'./' + testFile + '/blah\' from \'' + __dirname + '\'',
356 'can not find nonexistent module'
365 test('async dot main', function (t) {
366 var start = new Date();
368 resolve('./resolver/dot_main', function (err, ret) {
370 t.equal(ret, path.join(__dirname, 'resolver/dot_main/index.js'));
371 t.ok(new Date() - start < 50, 'resolve.sync timedout');
376 test('async dot slash main', function (t) {
377 var start = new Date();
379 resolve('./resolver/dot_slash_main', function (err, ret) {
381 t.equal(ret, path.join(__dirname, 'resolver/dot_slash_main/index.js'));
382 t.ok(new Date() - start < 50, 'resolve.sync timedout');
387 test('not a directory', function (t) {
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);
396 t.equal(err && err.message, 'Cannot find module \'' + path + '\' from \'' + __filename + '\'');
397 t.equal(err && err.code, 'MODULE_NOT_FOUND');
401 test('non-string "main" field in package.json', function (t) {
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');
414 test('non-string "main" field in package.json', function (t) {
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');
427 test('browser field in package.json', function (t) {
430 var dir = path.join(__dirname, 'resolver');
435 packageFilter: function packageFilter(pkg) {
437 pkg.main = pkg.browser; // eslint-disable-line no-param-reassign
438 delete pkg.browser; // eslint-disable-line no-param-reassign
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);