/
resolveWithPaths.test.js
42 lines (35 loc) · 1.41 KB
/
resolveWithPaths.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
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {resolve} from 'path';
test('finds a module relative to one of the given paths', () => {
expect(require.resolve('./mod.js', {paths: ['../dir']})).toEqual(
resolve(__dirname, '..', 'dir', 'mod.js'),
);
});
test('finds a module without a leading "./" relative to one of the given paths', () => {
expect(require.resolve('mod.js', {paths: ['../dir']})).toEqual(
resolve(__dirname, '..', 'dir', 'mod.js'),
);
});
test('finds a node_module above one of the given paths', () => {
expect(require.resolve('mod', {paths: ['../dir']})).toEqual(
resolve(__dirname, '..', 'node_modules', 'mod', 'index.js'),
);
});
test('finds a native node module when paths are given', () => {
expect(require.resolve('fs', {paths: ['../dir']})).toEqual('fs');
});
test('throws an error if the module cannot be found from given paths', () => {
expect(() => require.resolve('./mod.js', {paths: ['..']})).toThrowError(
"Cannot resolve module './mod.js' from paths ['..'] from ",
);
});
test('throws module not found error if the module cannot be found from given paths', () => {
expect(() => require.resolve('./mod.js', {paths: ['..']})).toThrow(
expect.objectContaining({code: 'MODULE_NOT_FOUND'}),
);
});