forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
installCommonGlobals.test.ts
48 lines (35 loc) · 1.23 KB
/
installCommonGlobals.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
/**
* 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 {createContext, runInContext} from 'vm';
declare global {
function DTRACE_NET_SERVER_CONNECTION(): unknown;
}
const fake = jest.fn();
globalThis.DTRACE_NET_SERVER_CONNECTION = fake;
let installCommonGlobals: typeof import('../installCommonGlobals').default;
function getGlobal(): typeof globalThis {
return runInContext('this', createContext());
}
beforeEach(() => {
installCommonGlobals = require('../installCommonGlobals').default;
});
afterEach(() => {
jest.clearAllMocks();
jest.resetModules();
});
it('returns the passed object', () => {
const myGlobal = getGlobal();
expect(installCommonGlobals(myGlobal, {})).toBe(myGlobal);
});
it('turns a V8 global object into a Node global object', () => {
const myGlobal = installCommonGlobals(getGlobal(), {});
expect(myGlobal.process).toBeDefined();
expect(myGlobal.DTRACE_NET_SERVER_CONNECTION).toBeDefined();
expect(myGlobal.DTRACE_NET_SERVER_CONNECTION).not.toBe(fake);
myGlobal.DTRACE_NET_SERVER_CONNECTION();
expect(fake).toHaveBeenCalledTimes(1);
});