/
runtime_require_module.test.js
349 lines (313 loc) · 10.5 KB
/
runtime_require_module.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/**
* 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';
import path from 'path';
import slash from 'slash';
let createRuntime;
describe('Runtime requireModule', () => {
beforeEach(() => {
createRuntime = require('createRuntime');
});
it('finds haste modules', () =>
createRuntime(__filename).then(runtime => {
const exports = runtime.requireModule(
runtime.__mockRootPath,
'RegularModule',
);
expect(exports.isRealModule).toBe(true);
}));
it('provides `module` to modules', () =>
createRuntime(__filename).then(runtime => {
const exports = runtime.requireModule(
runtime.__mockRootPath,
'RegularModule',
);
expect(Object.keys(exports.module)).toEqual([
'children',
'exports',
'filename',
'id',
'loaded',
'parent',
'paths',
]);
}));
it('provides `module.parent` to modules', () =>
createRuntime(__filename).then(runtime => {
const exports = runtime.requireModule(
runtime.__mockRootPath,
'RequireRegularModule',
);
expect(Object.keys(exports.parent)).toEqual([
'children',
'exports',
'filename',
'id',
'loaded',
'parent',
'paths',
]);
}));
it('`module.parent` should be undefined for entrypoints', () =>
createRuntime(__filename).then(runtime => {
const exports = runtime.requireModule(
runtime.__mockRootPath,
'RegularModule',
);
expect(exports.parent).toBeNull();
}));
it('resolve module.parent.require correctly', () =>
createRuntime(__filename).then(runtime => {
const exports = runtime.requireModule(
runtime.__mockRootPath,
'inner_parent_module',
);
expect(exports.outputString).toEqual('This should happen');
}));
it('resolve module.parent.filename correctly', () =>
createRuntime(__filename).then(runtime => {
const exports = runtime.requireModule(
runtime.__mockRootPath,
'inner_parent_module',
);
expect(slash(exports.parentFileName.replace(__dirname, ''))).toEqual(
'/test_root/inner_parent_module.js',
);
}));
it('provides `module.loaded` to modules', () =>
createRuntime(__filename).then(runtime => {
const exports = runtime.requireModule(
runtime.__mockRootPath,
'RegularModule',
);
// `exports.loaded` is set while the module is loaded, so should be `false`
expect(exports.loaded).toEqual(false);
// After the module is loaded we can query `module.loaded` again, at which point it should be `true`
expect(exports.isLoaded()).toEqual(true);
}));
it('provides `module.filename` to modules', () =>
createRuntime(__filename).then(runtime => {
const exports = runtime.requireModule(
runtime.__mockRootPath,
'RegularModule',
);
expect(
exports.filename.endsWith('test_root' + path.sep + 'RegularModule.js'),
).toBe(true);
}));
it('provides `module.paths` to modules', () => {
const altModuleDir = 'bower_components';
const moduleDirectories = ['node_modules', altModuleDir];
return createRuntime(__filename, {moduleDirectories}).then(runtime => {
const exports = runtime.requireModule(
runtime.__mockRootPath,
'RegularModule',
);
expect(exports.paths.length).toBeGreaterThan(0);
exports.paths.forEach(path => {
expect(moduleDirectories.some(dir => path.endsWith(dir))).toBe(true);
});
});
});
it('provides `require.main` to modules', () =>
createRuntime(__filename).then(runtime => {
runtime._moduleRegistry.set(__filename, module);
[
'./test_root/modules_with_main/export_main.js',
'./test_root/modules_with_main/re_export_main.js',
].forEach(modulePath => {
const mainModule = runtime.requireModule(__filename, modulePath);
expect(mainModule).toBe(module);
});
}));
it('throws on non-existent haste modules', () =>
createRuntime(__filename).then(runtime => {
expect(() => {
runtime.requireModule(runtime.__mockRootPath, 'DoesntExist');
}).toThrow(new Error("Cannot find module 'DoesntExist' from 'root.js'"));
}));
it('finds relative-path modules without file extension', () =>
createRuntime(__filename).then(runtime => {
const exports = runtime.requireModule(
runtime.__mockRootPath,
'./RegularModule',
);
expect(exports.isRealModule).toBe(true);
}));
it('finds relative-path modules with file extension', () =>
createRuntime(__filename).then(runtime => {
const exports = runtime.requireModule(
runtime.__mockRootPath,
'./RegularModule.js',
);
expect(exports.isRealModule).toBe(true);
}));
it('throws on non-existent relative-path modules', () =>
createRuntime(__filename).then(runtime => {
expect(() => {
runtime.requireModule(runtime.__mockRootPath, './DoesntExist');
}).toThrow(
new Error("Cannot find module './DoesntExist' from 'root.js'"),
);
}));
it('finds node core built-in modules', () =>
createRuntime(__filename).then(runtime => {
expect(() => {
runtime.requireModule(runtime.__mockRootPath, 'fs');
}).not.toThrow();
}));
it('finds and loads JSON files without file extension', () =>
createRuntime(__filename).then(runtime => {
const exports = runtime.requireModule(
runtime.__mockRootPath,
'./JSONFile',
);
expect(exports.isJSONModule).toBe(true);
}));
it('finds and loads JSON files with file extension', () =>
createRuntime(__filename).then(runtime => {
const exports = runtime.requireModule(
runtime.__mockRootPath,
'./JSONFile.json',
);
expect(exports.isJSONModule).toBe(true);
}));
it('requires a JSON file twice successfully', () =>
createRuntime(__filename).then(runtime => {
const exports1 = runtime.requireModule(
runtime.__mockRootPath,
'./JSONFile.json',
);
const exports2 = runtime.requireModule(
runtime.__mockRootPath,
'./JSONFile.json',
);
expect(exports1.isJSONModule).toBe(true);
expect(exports2.isJSONModule).toBe(true);
expect(exports1).toBe(exports2);
}));
it('provides manual mock when real module doesnt exist', () =>
createRuntime(__filename).then(runtime => {
const exports = runtime.requireModule(
runtime.__mockRootPath,
'ExclusivelyManualMock',
);
expect(exports.isExclusivelyManualMockModule).toBe(true);
}));
it(`doesn't override real modules with manual mocks when explicitly unmocked`, () =>
createRuntime(__filename, {
automock: true,
}).then(runtime => {
const root = runtime.requireModule(runtime.__mockRootPath, './root.js');
root.jest.resetModuleRegistry();
root.jest.unmock('ManuallyMocked');
const exports = runtime.requireModule(
runtime.__mockRootPath,
'ManuallyMocked',
);
expect(exports.isManualMockModule).toBe(false);
}));
it('resolves haste packages properly', () =>
createRuntime(__filename).then(runtime => {
const hastePackage = runtime.requireModule(
runtime.__mockRootPath,
'haste-package/core/module',
);
expect(hastePackage.isHastePackage).toBe(true);
}));
it('resolves node modules properly when crawling node_modules', () =>
// While we are crawling a node module, we shouldn't put package.json
// files of node modules to resolve to `package.json` but rather resolve
// to whatever the package.json's `main` field says.
createRuntime(__filename, {
haste: {
providesModuleNodeModules: ['not-a-haste-package'],
},
}).then(runtime => {
const hastePackage = runtime.requireModule(
runtime.__mockRootPath,
'not-a-haste-package',
);
expect(hastePackage.isNodeModule).toBe(true);
}));
it('resolves platform extensions based on the default platform', () =>
Promise.all([
createRuntime(__filename).then(runtime => {
const exports = runtime.requireModule(
runtime.__mockRootPath,
'Platform',
);
expect(exports.platform).toBe('default');
}),
createRuntime(__filename, {
haste: {
defaultPlatform: 'ios',
platforms: ['ios', 'android'],
},
}).then(runtime => {
const exports = runtime.requireModule(
runtime.__mockRootPath,
'Platform',
);
expect(exports.platform).toBe('ios');
}),
createRuntime(__filename, {
haste: {
platforms: ['ios', 'android'],
},
}).then(runtime => {
const exports = runtime.requireModule(
runtime.__mockRootPath,
'Platform',
);
expect(exports.platform).toBe('default');
}),
createRuntime(__filename, {
haste: {
defaultPlatform: 'android',
platforms: ['ios', 'android'],
},
}).then(runtime => {
const exports = runtime.requireModule(
runtime.__mockRootPath,
'Platform',
);
expect(exports.platform).toBe('android');
}),
createRuntime(__filename, {
haste: {
defaultPlatform: 'windows',
platforms: ['ios', 'android', 'native', 'windows'],
},
}).then(runtime => {
const exports = runtime.requireModule(
runtime.__mockRootPath,
'Platform',
);
// We prefer `native` over the default module if the default one
// cannot be found.
expect(exports.platform).toBe('native');
}),
]));
it('finds modules encoded in UTF-8 *with BOM*', () =>
createRuntime(__filename).then(runtime => {
const exports = runtime.requireModule(
runtime.__mockRootPath,
'./utf8_with_bom.js',
);
expect(exports).toBe('isModuleEncodedInUTF8WithBOM');
}));
it('finds and loads JSON files encoded in UTF-8 *with BOM*', () =>
createRuntime(__filename).then(runtime => {
const exports = runtime.requireModule(
runtime.__mockRootPath,
'./utf8_with_bom.json',
);
expect(exports.isJSONModuleEncodedInUTF8WithBOM).toBe(true);
}));
});