UNPKG

8.67 kBJavaScriptView Raw
1var path = require('path');
2var test = require('tape');
3var resolve = require('../');
4
5test('foo', function (t) {
6 var dir = path.join(__dirname, 'resolver');
7
8 t.equal(
9 resolve.sync('./foo', { basedir: dir }),
10 path.join(dir, 'foo.js')
11 );
12
13 t.equal(
14 resolve.sync('./foo.js', { basedir: dir }),
15 path.join(dir, 'foo.js')
16 );
17
18 t.equal(
19 resolve.sync('./foo.js', { basedir: dir, filename: path.join(dir, 'bar.js') }),
20 path.join(dir, 'foo.js')
21 );
22
23 t.throws(function () {
24 resolve.sync('foo', { basedir: dir });
25 });
26
27 // Test that filename is reported as the "from" value when passed.
28 t.throws(
29 function () {
30 resolve.sync('foo', { basedir: dir, filename: path.join(dir, 'bar.js') });
31 },
32 {
33 name: 'Error',
34 message: "Cannot find module 'foo' from '" + path.join(dir, 'bar.js') + "'"
35 }
36 );
37
38 t.end();
39});
40
41test('bar', function (t) {
42 var dir = path.join(__dirname, 'resolver');
43
44 t.equal(
45 resolve.sync('foo', { basedir: path.join(dir, 'bar') }),
46 path.join(dir, 'bar/node_modules/foo/index.js')
47 );
48 t.end();
49});
50
51test('baz', function (t) {
52 var dir = path.join(__dirname, 'resolver');
53
54 t.equal(
55 resolve.sync('./baz', { basedir: dir }),
56 path.join(dir, 'baz/quux.js')
57 );
58 t.end();
59});
60
61test('biz', function (t) {
62 var dir = path.join(__dirname, 'resolver/biz/node_modules');
63 t.equal(
64 resolve.sync('./grux', { basedir: dir }),
65 path.join(dir, 'grux/index.js')
66 );
67
68 t.equal(
69 resolve.sync('tiv', { basedir: path.join(dir, 'grux') }),
70 path.join(dir, 'tiv/index.js')
71 );
72
73 t.equal(
74 resolve.sync('grux', { basedir: path.join(dir, 'tiv') }),
75 path.join(dir, 'grux/index.js')
76 );
77 t.end();
78});
79
80test('normalize', function (t) {
81 var dir = path.join(__dirname, 'resolver/biz/node_modules/grux');
82 t.equal(
83 resolve.sync('../grux', { basedir: dir }),
84 path.join(dir, 'index.js')
85 );
86 t.end();
87});
88
89test('cup', function (t) {
90 var dir = path.join(__dirname, 'resolver');
91 t.equal(
92 resolve.sync('./cup', {
93 basedir: dir,
94 extensions: ['.js', '.coffee']
95 }),
96 path.join(dir, 'cup.coffee')
97 );
98
99 t.equal(
100 resolve.sync('./cup.coffee', { basedir: dir }),
101 path.join(dir, 'cup.coffee')
102 );
103
104 t.throws(function () {
105 resolve.sync('./cup', {
106 basedir: dir,
107 extensions: ['.js']
108 });
109 });
110
111 t.end();
112});
113
114test('mug', function (t) {
115 var dir = path.join(__dirname, 'resolver');
116 t.equal(
117 resolve.sync('./mug', { basedir: dir }),
118 path.join(dir, 'mug.js')
119 );
120
121 t.equal(
122 resolve.sync('./mug', {
123 basedir: dir,
124 extensions: ['.coffee', '.js']
125 }),
126 path.join(dir, 'mug.coffee')
127 );
128
129 t.equal(
130 resolve.sync('./mug', {
131 basedir: dir,
132 extensions: ['.js', '.coffee']
133 }),
134 path.join(dir, 'mug.js')
135 );
136
137 t.end();
138});
139
140test('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');
144
145 t.equal(
146 resolve.sync('root', {
147 basedir: dir,
148 paths: [otherDir]
149 }),
150 path.join(resolverDir, 'other_path/root.js')
151 );
152
153 t.equal(
154 resolve.sync('lib/other-lib', {
155 basedir: dir,
156 paths: [otherDir]
157 }),
158 path.join(resolverDir, 'other_path/lib/other-lib.js')
159 );
160
161 t.throws(function () {
162 resolve.sync('root', { basedir: dir });
163 });
164
165 t.throws(function () {
166 resolve.sync('zzz', {
167 basedir: dir,
168 paths: [otherDir]
169 });
170 });
171
172 t.end();
173});
174
175test('incorrect main', function (t) {
176 var resolverDir = path.join(__dirname, 'resolver');
177 var dir = path.join(resolverDir, 'incorrect_main');
178
179 t.equal(
180 resolve.sync('./incorrect_main', { basedir: resolverDir }),
181 path.join(dir, 'index.js')
182 );
183
184 t.end();
185});
186
187var stubStatSync = function stubStatSync(fn) {
188 var fs = require('fs');
189 var statSync = fs.statSync;
190 try {
191 fs.statSync = function () {
192 throw new EvalError('Unknown Error');
193 };
194 return fn();
195 } finally {
196 fs.statSync = statSync;
197 }
198};
199
200test('#79 - re-throw non ENOENT errors from stat', function (t) {
201 var dir = path.join(__dirname, 'resolver');
202
203 stubStatSync(function () {
204 t.throws(function () {
205 resolve.sync('foo', { basedir: dir });
206 }, /Unknown Error/);
207 });
208
209 t.end();
210});
211
212test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is a file of the same name', function (t) {
213 var dir = path.join(__dirname, 'resolver');
214
215 t.equal(
216 resolve.sync('./foo', { basedir: path.join(dir, 'same_names') }),
217 path.join(dir, 'same_names/foo.js')
218 );
219 t.equal(
220 resolve.sync('./foo/', { basedir: path.join(dir, 'same_names') }),
221 path.join(dir, 'same_names/foo/index.js')
222 );
223 t.end();
224});
225
226test('sync: #121 - treating an existing file as a dir when no basedir', function (t) {
227 var testFile = path.basename(__filename);
228
229 t.test('sanity check', function (st) {
230 st.equal(
231 resolve.sync('./' + testFile),
232 __filename,
233 'sanity check'
234 );
235 st.end();
236 });
237
238 t.test('with a fake directory', function (st) {
239 function run() { return resolve.sync('./' + testFile + '/blah'); }
240
241 st.throws(run, 'throws an error');
242
243 try {
244 run();
245 } catch (e) {
246 st.equal(e.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
247 st.equal(
248 e.message,
249 'Cannot find module \'./' + testFile + '/blah\' from \'' + __dirname + '\'',
250 'can not find nonexistent module'
251 );
252 }
253
254 st.end();
255 });
256
257 t.end();
258});
259
260test('sync dot main', function (t) {
261 var start = new Date();
262 t.equal(resolve.sync('./resolver/dot_main'), path.join(__dirname, 'resolver/dot_main/index.js'));
263 t.ok(new Date() - start < 50, 'resolve.sync timedout');
264 t.end();
265});
266
267test('sync dot slash main', function (t) {
268 var start = new Date();
269 t.equal(resolve.sync('./resolver/dot_slash_main'), path.join(__dirname, 'resolver/dot_slash_main/index.js'));
270 t.ok(new Date() - start < 50, 'resolve.sync timedout');
271 t.end();
272});
273
274test('not a directory', function (t) {
275 var path = './foo';
276 try {
277 resolve.sync(path, { basedir: __filename });
278 t.fail();
279 } catch (err) {
280 t.ok(err, 'a non-directory errors');
281 t.equal(err && err.message, 'Cannot find module \'' + path + "' from '" + __filename + "'");
282 t.equal(err && err.code, 'MODULE_NOT_FOUND');
283 }
284 t.end();
285});
286
287test('non-string "main" field in package.json', function (t) {
288 var dir = path.join(__dirname, 'resolver');
289 try {
290 var result = resolve.sync('./invalid_main', { basedir: dir });
291 t.equal(result, undefined, 'result should not exist');
292 t.fail('should not get here');
293 } catch (err) {
294 t.ok(err, 'errors on non-string main');
295 t.equal(err.message, 'package “invalid main” `main` must be a string');
296 t.equal(err.code, 'INVALID_PACKAGE_MAIN');
297 }
298 t.end();
299});
300
301test('non-string "main" field in package.json', function (t) {
302 var dir = path.join(__dirname, 'resolver');
303 try {
304 var result = resolve.sync('./invalid_main', { basedir: dir });
305 t.equal(result, undefined, 'result should not exist');
306 t.fail('should not get here');
307 } catch (err) {
308 t.ok(err, 'errors on non-string main');
309 t.equal(err.message, 'package “invalid main” `main` must be a string');
310 t.equal(err.code, 'INVALID_PACKAGE_MAIN');
311 }
312 t.end();
313});
314
315test('browser field in package.json', function (t) {
316 var dir = path.join(__dirname, 'resolver');
317 var res = resolve.sync('./browser_field', {
318 basedir: dir,
319 packageFilter: function packageFilter(pkg) {
320 if (pkg.browser) {
321 pkg.main = pkg.browser; // eslint-disable-line no-param-reassign
322 delete pkg.browser; // eslint-disable-line no-param-reassign
323 }
324 return pkg;
325 }
326 });
327 t.equal(res, path.join(dir, 'browser_field', 'b.js'));
328 t.end();
329});