/
runtime_create_mock_from_module.test.js
67 lines (54 loc) · 2.12 KB
/
runtime_create_mock_from_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
/**
* 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 createRuntime;
const moduleNameMapper = {
'module/name/(.*)': '<rootDir>/mapped_module_$1.js',
};
describe('Runtime', () => {
beforeEach(() => {
createRuntime = require('createRuntime');
});
describe('createMockFromModule', () => {
it('does not cause side effects in the rest of the module system when generating a mock', () =>
createRuntime(__filename).then(runtime => {
const testRequire = runtime.requireModule.bind(
runtime,
runtime.__mockRootPath,
);
const module = testRequire('RegularModule');
const origModuleStateValue = module.getModuleStateValue();
expect(origModuleStateValue).toBe('default');
// Generate a mock for a module with side effects
const mock = module.jest.createMockFromModule('ModuleWithSideEffects');
// Make sure we get a mock.
expect(mock.fn()).toBe(undefined);
expect(module.getModuleStateValue()).toBe(origModuleStateValue);
}));
it('resolves mapped modules correctly', () =>
createRuntime(__filename, {moduleNameMapper}).then(runtime => {
const root = runtime.requireModule(runtime.__mockRootPath);
const mockModule = root.jest.createMockFromModule(
'module/name/createMockFromModule',
);
expect(mockModule.test.mock).toBeTruthy();
}));
});
it('creates mock objects in the right environment', () =>
createRuntime(__filename).then(runtime => {
const testRequire = runtime.requireModule.bind(
runtime,
runtime.__mockRootPath,
);
const module = testRequire('RegularModule');
const mockModule = module.jest.createMockFromModule('RegularModule');
const testObjectPrototype = Object.getPrototypeOf(module.object);
const mockObjectPrototype = Object.getPrototypeOf(mockModule.object);
expect(mockObjectPrototype).toBe(testObjectPrototype);
}));
});