forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolve.test.js
281 lines (249 loc) Β· 7.67 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
* Copyright (c) 2014-present, Facebook, Inc. 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';
jest.mock('../__mocks__/userResolver');
const fs = require('fs');
const path = require('path');
const ModuleMap = require('jest-haste-map').ModuleMap;
const Resolver = require('../');
const userResolver = require('../__mocks__/userResolver');
beforeEach(() => {
userResolver.mockClear();
});
describe('isCoreModule', () => {
it('returns false if `hasCoreModules` is false.', () => {
const moduleMap = new ModuleMap();
const resolver = new Resolver(moduleMap, {
hasCoreModules: false,
});
const isCore = resolver.isCoreModule('assert');
expect(isCore).toEqual(false);
});
it('returns true if `hasCoreModules` is true and `moduleName` is a core module.', () => {
const moduleMap = new ModuleMap();
const resolver = new Resolver(moduleMap, {});
const isCore = resolver.isCoreModule('assert');
expect(isCore).toEqual(true);
});
it('returns false if `hasCoreModules` is true and `moduleName` is not a core module.', () => {
const moduleMap = new ModuleMap();
const resolver = new Resolver(moduleMap, {});
const isCore = resolver.isCoreModule('not-a-core-module');
expect(isCore).toEqual(false);
});
});
describe('findNodeModule', () => {
it('is possible to override the default resolver', () => {
const cwd = process.cwd();
const resolvedCwd = fs.realpathSync(cwd) || cwd;
const nodePaths = process.env.NODE_PATH
? process.env.NODE_PATH.split(path.delimiter)
.filter(Boolean)
.map(p => path.resolve(resolvedCwd, p))
: null;
userResolver.mockImplementation(() => 'module');
const newPath = Resolver.findNodeModule('test', {
basedir: '/',
browser: true,
extensions: ['js'],
moduleDirectory: ['node_modules'],
paths: ['/something'],
resolver: require.resolve('../__mocks__/userResolver'),
});
expect(newPath).toBe('module');
expect(userResolver.mock.calls[0][0]).toBe('test');
expect(userResolver.mock.calls[0][1]).toEqual({
basedir: '/',
browser: true,
extensions: ['js'],
moduleDirectory: ['node_modules'],
paths: (nodePaths || []).concat(['/something']),
});
});
});
describe('resolveModule', () => {
let moduleMap;
beforeEach(() => {
moduleMap = new ModuleMap({
duplicates: [],
map: [],
mocks: [],
});
});
it('is possible to resolve node modules', () => {
const resolver = new Resolver(moduleMap, {
extensions: ['.js'],
});
const src = require.resolve('../');
const resolved = resolver.resolveModule(
src,
'./__mocks__/mockJsDependency',
);
expect(resolved).toBe(require.resolve('../__mocks__/mockJsDependency.js'));
});
it('is possible to resolve node modules with custom extensions', () => {
const resolver = new Resolver(moduleMap, {
extensions: ['.js', '.jsx'],
});
const src = require.resolve('../');
const resolvedJsx = resolver.resolveModule(
src,
'./__mocks__/mockJsxDependency',
);
expect(resolvedJsx).toBe(
require.resolve('../__mocks__/mockJsxDependency.jsx'),
);
});
it('is possible to resolve node modules with custom extensions and platforms', () => {
const resolver = new Resolver(moduleMap, {
extensions: ['.js', '.jsx'],
platforms: ['native'],
});
const src = require.resolve('../');
const resolvedJsx = resolver.resolveModule(
src,
'./__mocks__/mockJsxDependency',
);
expect(resolvedJsx).toBe(
require.resolve('../__mocks__/mockJsxDependency.native.jsx'),
);
});
});
describe('getMockModule', () => {
it('is possible to use custom resolver to resolve deps inside mock modules with moduleNameMapper', () => {
userResolver.mockImplementation(() => 'module');
const moduleMap = new ModuleMap({
duplicates: [],
map: [],
mocks: [],
});
const resolver = new Resolver(moduleMap, {
moduleNameMapper: [
{
moduleName: '$1',
regex: /(.*)/,
},
],
resolver: require.resolve('../__mocks__/userResolver'),
});
const src = require.resolve('../');
resolver.getMockModule(src, 'dependentModule');
expect(userResolver).toHaveBeenCalled();
expect(userResolver.mock.calls[0][0]).toBe('dependentModule');
expect(userResolver.mock.calls[0][1]).toHaveProperty(
'basedir',
path.dirname(src),
);
});
it('is possible to use custom resolver to resolve deps inside mock modules without moduleNameMapper', () => {
userResolver.mockImplementation(() => 'module');
const moduleMap = new ModuleMap({
duplicates: [],
map: [],
mocks: [],
});
const resolver = new Resolver(moduleMap, {
resolver: require.resolve('../__mocks__/userResolver'),
});
const src = require.resolve('../');
resolver.getMockModule(src, 'dependentModule');
expect(userResolver).toHaveBeenCalled();
expect(userResolver.mock.calls[0][0]).toBe('dependentModule');
expect(userResolver.mock.calls[0][1]).toHaveProperty(
'basedir',
path.dirname(src),
);
});
});
describe('Resolver.getModulePaths() -> nodeModulesPaths()', () => {
let moduleMap;
const path_methods = {
cache: {},
names: ['dirname', 'resolve', 'parse', 'isAbsolute', 'join'],
platform: '',
};
const save_path = () => {
path_methods.platform =
path.resolve === path.win32.resolve ? 'win32' : 'posix';
path_methods.names.forEach(name => {
path_methods.cache[name] = path[name];
});
};
const update_path = os => {
path_methods.names.forEach(name => {
path[name] = path[os][name];
});
};
const restore_path = () => {
const os = path_methods.platform;
path_methods.names.forEach(name => {
path[os][name] = path_methods.cache[name];
path[name] = path_methods.cache[name];
});
};
beforeEach(() => {
moduleMap = new ModuleMap({
duplicates: [],
map: [],
mocks: [],
});
});
const test_win32 = expect => {
const cwd = 'D:\\project';
const src = 'C:\\path\\to\\node_modules';
const resolver = new Resolver(moduleMap, {
moduleDirectories: [src, 'node_modules'],
});
const dirs_expected = [
src,
cwd + '\\node_modules',
path.dirname(cwd).replace(/\\$/, '') + '\\node_modules',
];
const dirs_actual = resolver.getModulePaths(cwd);
expect(dirs_actual).toEqual(expect.arrayContaining(dirs_expected));
};
const test_posix = expect => {
const cwd = '/temp/project';
const src = '/root/path/to/node_modules';
const resolver = new Resolver(moduleMap, {
moduleDirectories: [src, 'node_modules'],
});
const dirs_expected = [
src,
cwd + '/node_modules',
path.dirname(cwd) + '/node_modules',
'/node_modules',
];
const dirs_actual = resolver.getModulePaths(cwd);
expect(dirs_actual).toEqual(expect.arrayContaining(dirs_expected));
};
// run tests sequentially
it('can resolve node modules relative to absolute paths in "moduleDirectories" on all platforms', () => {
return Promise.resolve(expect)
.then(expect => {
save_path();
return expect;
})
.then(expect => {
update_path('win32');
test_win32(expect);
restore_path();
return expect;
})
.then(expect => {
update_path('posix');
test_posix(expect);
restore_path();
return expect;
})
.catch(error => {
restore_path();
throw error;
});
});
});