forked from jestjs/jest
/
modernFakeTimers.ts
184 lines (155 loc) · 4.27 KB
/
modernFakeTimers.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
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
/**
* 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 {
FakeTimerWithContext,
FakeMethod as FakeableAPI,
InstalledClock,
FakeTimerInstallOpts as SinonFakeTimersConfig,
withGlobal,
} from '@sinonjs/fake-timers';
import type {Config} from '@jest/types';
import {formatStackTrace} from 'jest-message-util';
export default class FakeTimers {
private _clock!: InstalledClock;
private _config: Config.ProjectConfig;
private _fakingTime: boolean;
private _global: typeof globalThis;
private _fakeTimers: FakeTimerWithContext;
constructor({
global,
config,
}: {
global: typeof globalThis;
config: Config.ProjectConfig;
}) {
this._global = global;
this._config = config;
this._fakingTime = false;
this._fakeTimers = withGlobal(global);
}
clearAllTimers(): void {
if (this._fakingTime) {
this._clock.reset();
}
}
dispose(): void {
this.useRealTimers();
}
runAllTimers(): void {
if (this._checkFakeTimers()) {
this._clock.runAll();
}
}
runOnlyPendingTimers(): void {
if (this._checkFakeTimers()) {
this._clock.runToLast();
}
}
advanceTimersToNextTimer(steps = 1): void {
if (this._checkFakeTimers()) {
for (let i = steps; i > 0; i--) {
this._clock.next();
// Fire all timers at this point: https://github.com/sinonjs/fake-timers/issues/250
this._clock.tick(0);
if (this._clock.countTimers() === 0) {
break;
}
}
}
}
advanceTimersByTime(msToRun: number): void {
if (this._checkFakeTimers()) {
this._clock.tick(msToRun);
}
}
runAllTicks(): void {
if (this._checkFakeTimers()) {
// @ts-expect-error - doesn't exist?
this._clock.runMicrotasks();
}
}
useRealTimers(): void {
if (this._fakingTime) {
this._clock.uninstall();
this._fakingTime = false;
}
}
useFakeTimers(fakeTimersConfig?: Config.FakeTimersConfig): void {
if (this._fakingTime) {
this._clock.uninstall();
}
this._clock = this._fakeTimers.install(
this._toSinonFakeTimersConfig(fakeTimersConfig),
);
this._fakingTime = true;
}
reset(): void {
if (this._checkFakeTimers()) {
const {now} = this._clock;
this._clock.reset();
this._clock.setSystemTime(now);
}
}
setSystemTime(now?: number | Date): void {
if (this._checkFakeTimers()) {
this._clock.setSystemTime(now);
}
}
getRealSystemTime(): number {
return Date.now();
}
now(): number {
return this._clock.now;
}
getTimerCount(): number {
if (this._checkFakeTimers()) {
return this._clock.countTimers();
}
return 0;
}
private _checkFakeTimers() {
if (!this._fakingTime) {
this._global.console.warn(
'A function to advance timers was called but the timers APIs are not replaced ' +
'with fake timers. Call `jest.useFakeTimers()` in this test file or enable ' +
"fake timers for all tests by setting 'fakeTimers': {'enableGlobally': true} " +
`in Jest configuration file.\nStack Trace:\n${formatStackTrace(
new Error().stack!,
this._config,
{noStackTrace: false},
)}`,
);
}
return this._fakingTime;
}
private _toSinonFakeTimersConfig(
fakeTimersConfig: Config.FakeTimersConfig = {},
): SinonFakeTimersConfig {
fakeTimersConfig = {
...this._config.fakeTimers,
...fakeTimersConfig,
} as Config.FakeTimersConfig;
const advanceTimeDelta =
typeof fakeTimersConfig.advanceTimers === 'number'
? fakeTimersConfig.advanceTimers
: undefined;
const toFake = new Set(
Object.keys(this._fakeTimers.timers) as Array<FakeableAPI>,
);
fakeTimersConfig.doNotFake?.forEach(nameOfFakeableAPI => {
toFake.delete(nameOfFakeableAPI);
});
return {
advanceTimeDelta,
loopLimit: fakeTimersConfig.timerLimit || 100_000,
now: fakeTimersConfig.now ?? Date.now(),
shouldAdvanceTime: Boolean(fakeTimersConfig.advanceTimers),
shouldClearNativeTimers: true,
toFake: Array.from(toFake),
};
}
}