1 var path = require('path');
2 var test = require('tape');
3 var resolve = require('../');
5 test('foo', function (t) {
6 var dir = path.join(__dirname, 'resolver');
9 resolve.sync('./foo', { basedir: dir }),
10 path.join(dir, 'foo.js')
14 resolve.sync('./foo.js', { basedir: dir }),
15 path.join(dir, 'foo.js')
19 resolve.sync('./foo.js', { basedir: dir, filename: path.join(dir, 'bar.js') }),
20 path.join(dir, 'foo.js')
23 t.throws(function () {
24 resolve.sync('foo', { basedir: dir });
27 // Test that filename is reported as the "from" value when passed.
30 resolve.sync('foo', { basedir: dir, filename: path.join(dir, 'bar.js') });
34 message: "Cannot find module 'foo' from '" + path.join(dir, 'bar.js') + "'"
41 test('bar', function (t) {
42 var dir = path.join(__dirname, 'resolver');
45 resolve.sync('foo', { basedir: path.join(dir, 'bar') }),
46 path.join(dir, 'bar/node_modules/foo/index.js')
51 test('baz', function (t) {
52 var dir = path.join(__dirname, 'resolver');
55 resolve.sync('./baz', { basedir: dir }),
56 path.join(dir, 'baz/quux.js')
61 test('biz', function (t) {
62 var dir = path.join(__dirname, 'resolver/biz/node_modules');
64 resolve.sync('./grux', { basedir: dir }),
65 path.join(dir, 'grux/index.js')
69 resolve.sync('tiv', { basedir: path.join(dir, 'grux') }),
70 path.join(dir, 'tiv/index.js')
74 resolve.sync('grux', { basedir: path.join(dir, 'tiv') }),
75 path.join(dir, 'grux/index.js')
80 test('normalize', function (t) {
81 var dir = path.join(__dirname, 'resolver/biz/node_modules/grux');
83 resolve.sync('../grux', { basedir: dir }),
84 path.join(dir, 'index.js')
89 test('cup', function (t) {
90 var dir = path.join(__dirname, 'resolver');
92 resolve.sync('./cup', {
94 extensions: ['.js', '.coffee']
96 path.join(dir, 'cup.coffee')
100 resolve.sync('./cup.coffee', { basedir: dir }),
101 path.join(dir, 'cup.coffee')
104 t.throws(function () {
105 resolve.sync('./cup', {
114 test('mug', function (t) {
115 var dir = path.join(__dirname, 'resolver');
117 resolve.sync('./mug', { basedir: dir }),
118 path.join(dir, 'mug.js')
122 resolve.sync('./mug', {
124 extensions: ['.coffee', '.js']
126 path.join(dir, 'mug.coffee')
130 resolve.sync('./mug', {
132 extensions: ['.js', '.coffee']
134 path.join(dir, 'mug.js')
140 test('other path', function (t) {
141 var resolverDir = path.join(__dirname, 'resolver');
142 var dir = path.join(resolverDir, 'bar');
143 var otherDir = path.join(resolverDir, 'other_path');
146 resolve.sync('root', {
150 path.join(resolverDir, 'other_path/root.js')
154 resolve.sync('lib/other-lib', {
158 path.join(resolverDir, 'other_path/lib/other-lib.js')
161 t.throws(function () {
162 resolve.sync('root', { basedir: dir });
165 t.throws(function () {
166 resolve.sync('zzz', {
175 test('path iterator', function (t) {
176 var resolverDir = path.join(__dirname, 'resolver');
178 var exactIterator = function (x, start, getPackageCandidates, opts) {
179 return [path.join(resolverDir, x)];
183 resolve.sync('baz', { packageIterator: exactIterator }),
184 path.join(resolverDir, 'baz/quux.js')
190 test('incorrect main', function (t) {
191 var resolverDir = path.join(__dirname, 'resolver');
192 var dir = path.join(resolverDir, 'incorrect_main');
195 resolve.sync('./incorrect_main', { basedir: resolverDir }),
196 path.join(dir, 'index.js')
202 var stubStatSync = function stubStatSync(fn) {
203 var fs = require('fs');
204 var statSync = fs.statSync;
206 fs.statSync = function () {
207 throw new EvalError('Unknown Error');
211 fs.statSync = statSync;
215 test('#79 - re-throw non ENOENT errors from stat', function (t) {
216 var dir = path.join(__dirname, 'resolver');
218 stubStatSync(function () {
219 t.throws(function () {
220 resolve.sync('foo', { basedir: dir });
227 test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is a file of the same name', function (t) {
228 var dir = path.join(__dirname, 'resolver');
231 resolve.sync('./foo', { basedir: path.join(dir, 'same_names') }),
232 path.join(dir, 'same_names/foo.js')
235 resolve.sync('./foo/', { basedir: path.join(dir, 'same_names') }),
236 path.join(dir, 'same_names/foo/index.js')
241 test('#211 - incorrectly resolves module-paths like "." when from inside a folder with a sibling file of the same name', function (t) {
242 var dir = path.join(__dirname, 'resolver');
245 resolve.sync('./', { basedir: path.join(dir, 'same_names/foo') }),
246 path.join(dir, 'same_names/foo/index.js')
249 resolve.sync('.', { basedir: path.join(dir, 'same_names/foo') }),
250 path.join(dir, 'same_names/foo/index.js')
255 test('sync: #121 - treating an existing file as a dir when no basedir', function (t) {
256 var testFile = path.basename(__filename);
258 t.test('sanity check', function (st) {
260 resolve.sync('./' + testFile),
267 t.test('with a fake directory', function (st) {
268 function run() { return resolve.sync('./' + testFile + '/blah'); }
270 st.throws(run, 'throws an error');
275 st.equal(e.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
278 'Cannot find module \'./' + testFile + '/blah\' from \'' + __dirname + '\'',
279 'can not find nonexistent module'
289 test('sync dot main', function (t) {
290 var start = new Date();
291 t.equal(resolve.sync('./resolver/dot_main'), path.join(__dirname, 'resolver/dot_main/index.js'));
292 t.ok(new Date() - start < 50, 'resolve.sync timedout');
296 test('sync dot slash main', function (t) {
297 var start = new Date();
298 t.equal(resolve.sync('./resolver/dot_slash_main'), path.join(__dirname, 'resolver/dot_slash_main/index.js'));
299 t.ok(new Date() - start < 50, 'resolve.sync timedout');
303 test('not a directory', function (t) {
306 resolve.sync(path, { basedir: __filename });
309 t.ok(err, 'a non-directory errors');
310 t.equal(err && err.message, 'Cannot find module \'' + path + "' from '" + __filename + "'");
311 t.equal(err && err.code, 'MODULE_NOT_FOUND');
316 test('non-string "main" field in package.json', function (t) {
317 var dir = path.join(__dirname, 'resolver');
319 var result = resolve.sync('./invalid_main', { basedir: dir });
320 t.equal(result, undefined, 'result should not exist');
321 t.fail('should not get here');
323 t.ok(err, 'errors on non-string main');
324 t.equal(err.message, 'package “invalid main” `main` must be a string');
325 t.equal(err.code, 'INVALID_PACKAGE_MAIN');
330 test('non-string "main" field in package.json', function (t) {
331 var dir = path.join(__dirname, 'resolver');
333 var result = resolve.sync('./invalid_main', { basedir: dir });
334 t.equal(result, undefined, 'result should not exist');
335 t.fail('should not get here');
337 t.ok(err, 'errors on non-string main');
338 t.equal(err.message, 'package “invalid main” `main` must be a string');
339 t.equal(err.code, 'INVALID_PACKAGE_MAIN');
344 test('browser field in package.json', function (t) {
345 var dir = path.join(__dirname, 'resolver');
346 var res = resolve.sync('./browser_field', {
348 packageFilter: function packageFilter(pkg) {
350 pkg.main = pkg.browser; // eslint-disable-line no-param-reassign
351 delete pkg.browser; // eslint-disable-line no-param-reassign
356 t.equal(res, path.join(dir, 'browser_field', 'b.js'));