forked from jestjs/jest
/
resolve.test.js
138 lines (107 loc) · 4.29 KB
/
resolve.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* 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.
*/
'use strict';
let platform;
function testRequire(filename) {
return () => (platform = require(filename));
}
test('should explicitly resolve filename.<platform>.js', () => {
expect(testRequire('../test1.android.js')).not.toThrow();
expect(platform.extension).toBe('android.js');
});
test('should explicitly resolve filename.native.js', () => {
expect(testRequire('../test1.native.js')).not.toThrow();
expect(platform.extension).toBe('native.js');
});
test('should explicitly resolve filename.js', () => {
expect(testRequire('../test1.js')).not.toThrow();
expect(platform.extension).toBe('js');
});
test('should explicitly resolve filename.json', () => {
expect(testRequire('../test1.json')).not.toThrow();
expect(platform.extension).toBe('json');
});
test('should resolve filename.<platform>.js', () => {
expect(testRequire('../test1')).not.toThrow();
expect(platform.extension).toBe('android.js');
});
test('should resolve filename.<platform>.js from haste package', () => {
expect(testRequire('custom-resolve/test1')).not.toThrow();
expect(platform.extension).toBe('android.js');
});
test('should resolve filename.native.js', () => {
expect(testRequire('../test2')).not.toThrow();
expect(platform.extension).toBe('native.js');
});
test('should resolve filename.native.js with moduleNameMapper', () => {
expect(testRequire('test2mapper')).not.toThrow();
expect(platform.extension).toBe('native.js');
});
test('should resolve filename.js', () => {
expect(testRequire('../test3')).not.toThrow();
expect(platform.extension).toBe('js');
});
test('should resolve filename.json', () => {
expect(testRequire('../test4')).not.toThrow();
expect(platform.extension).toBe('json');
});
test('should preserve identity for symlinks', () => {
expect(require('../../../packages/jest-resolve')).toBe(
require('jest-resolve')
);
});
test('should require resolve haste files correctly', () => {
// We unmock Test5 (they should already be, but to be sure).
jest.unmock('Test5');
// Test5 is a standard module, that has a mock (but it is unmocked here).
expect(require.resolve('Test5')).toBe(require.resolve('../Test5'));
expect(require('Test5').key).toBe('real');
// Test6 only exits as a mock; so even when unmocked, we resolve to the mock.
expect(require.resolve('Test6')).toBe(require.resolve('../__mocks__/Test6'));
expect(require('Test6').key).toBe('mock');
});
test('should require resolve haste mocks correctly', () => {
// Now we mock Test5 and Test6.
jest.mock('Test5');
jest.mock('Test6');
// The resolution still points to the real one, but requires the mock.
expect(require.resolve('Test5')).toBe(require.resolve('../Test5'));
expect(require('Test5').key).toBe('mock');
// And Test6 points to the mock, because Test6 does not exist as a module.
expect(require.resolve('Test6')).toBe(require.resolve('../__mocks__/Test6'));
expect(require('Test6').key).toBe('mock');
});
test('should throw module not found error if the module has dependencies that cannot be found', () => {
try {
require('Test7');
throw new Error('Requiring Test7 should have thrown an error');
} catch (error) {
expect(error.code).toBe('MODULE_NOT_FOUND');
expect(error.message).toMatchInlineSnapshot(`
"Cannot find module 'nope' from 'requiresUnexistingModule.js'
Require stack:
requiresUnexistingModule.js
Test7.js
__tests__/resolve.test.js
However, Jest was able to find:
'./requiresUnexistingModule.js'
You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'json', 'jsx', 'ts', 'tsx', 'node'].
See https://jestjs.io/docs/en/configuration#modulefileextensions-arraystring"
`);
}
});
test('should throw module not found error if the module cannot be found', () => {
try {
require('Test8');
throw new Error('Requiring Test8 should have thrown an error');
} catch (error) {
expect(error.code).toBe('MODULE_NOT_FOUND');
expect(error.message.split('\n')[0]).toBe(
"Cannot find module 'Test8' from 'resolve.test.js'"
);
}
});