-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
node_environment.test.ts
60 lines (45 loc) · 1.79 KB
/
node_environment.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
56
57
58
59
60
/**
* 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 {makeProjectConfig} from '@jest/test-utils';
import NodeEnvironment = require('../');
const isTextEncoderDefined = typeof TextEncoder === 'function';
describe('NodeEnvironment', () => {
it('uses a copy of the process object', () => {
const env1 = new NodeEnvironment(makeProjectConfig());
const env2 = new NodeEnvironment(makeProjectConfig());
expect(env1.global.process).not.toBe(env2.global.process);
});
it('exposes process.on', () => {
const env1 = new NodeEnvironment(makeProjectConfig());
expect(env1.global.process.on).not.toBe(null);
});
it('exposes global.global', () => {
const env1 = new NodeEnvironment(makeProjectConfig());
expect(env1.global.global).toBe(env1.global);
});
it('should configure setTimeout/setInterval to use the node api', () => {
const env1 = new NodeEnvironment(makeProjectConfig());
env1.fakeTimers!.useFakeTimers();
const timer1 = env1.global.setTimeout(() => {}, 0);
const timer2 = env1.global.setInterval(() => {}, 0);
[timer1, timer2].forEach(timer => {
// @ts-expect-error
expect(timer.id).not.toBeUndefined();
expect(typeof timer.ref).toBe('function');
expect(typeof timer.unref).toBe('function');
});
});
it('has modern fake timers implementation', () => {
const env = new NodeEnvironment(makeProjectConfig());
expect(env.fakeTimersModern).toBeDefined();
});
if (isTextEncoderDefined) {
test('TextEncoder references the same global Uint8Array constructor', () => {
expect(new TextEncoder().encode('abc')).toBeInstanceOf(Uint8Array);
});
}
});