/
isolateModules.test.ts
55 lines (45 loc) · 1.27 KB
/
isolateModules.test.ts
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
/**
* 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 {tmpdir} from 'os';
import * as path from 'path';
import {cleanup, createEmptyPackage, writeFiles} from '../Utils';
import runJest from '../runJest';
const DIR = path.resolve(tmpdir(), 'isolate-modules.test');
beforeEach(() => {
cleanup(DIR);
createEmptyPackage(DIR);
});
afterAll(() => cleanup(DIR));
test('works with mocks', () => {
writeFiles(DIR, {
'config.js': `
module.exports.getBoolean = function getBoolean(variableName) {
return false;
}
`,
'read.js': `
const {getBoolean} = require('./config');
const value = getBoolean('foo');
console.log("was " + value);
`,
'test.js': `
jest.mock('./config');
const config = require('./config');
test('dummy test', () => {
const configGetMock = config.getBoolean.mockImplementation(() => {
return true;
});
jest.isolateModules(() => {
require("./read");
});
expect(configGetMock).toBeCalledTimes(1);
})
`,
});
const {exitCode} = runJest(DIR);
expect(exitCode).toBe(0);
});