UNPKG

3.61 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.default = void 0;
7
8function _fakeTimers() {
9 const data = require('@sinonjs/fake-timers');
10
11 _fakeTimers = function () {
12 return data;
13 };
14
15 return data;
16}
17
18function _jestMessageUtil() {
19 const data = require('jest-message-util');
20
21 _jestMessageUtil = function () {
22 return data;
23 };
24
25 return data;
26}
27
28function _defineProperty(obj, key, value) {
29 if (key in obj) {
30 Object.defineProperty(obj, key, {
31 value: value,
32 enumerable: true,
33 configurable: true,
34 writable: true
35 });
36 } else {
37 obj[key] = value;
38 }
39 return obj;
40}
41
42class FakeTimers {
43 constructor({global, config, maxLoops}) {
44 _defineProperty(this, '_clock', void 0);
45
46 _defineProperty(this, '_config', void 0);
47
48 _defineProperty(this, '_fakingTime', void 0);
49
50 _defineProperty(this, '_global', void 0);
51
52 _defineProperty(this, '_fakeTimers', void 0);
53
54 _defineProperty(this, '_maxLoops', void 0);
55
56 this._global = global;
57 this._config = config;
58 this._maxLoops = maxLoops || 100000;
59 this._fakingTime = false;
60 this._fakeTimers = (0, _fakeTimers().withGlobal)(global);
61 }
62
63 clearAllTimers() {
64 if (this._fakingTime) {
65 this._clock.reset();
66 }
67 }
68
69 dispose() {
70 this.useRealTimers();
71 }
72
73 runAllTimers() {
74 if (this._checkFakeTimers()) {
75 this._clock.runAll();
76 }
77 }
78
79 runOnlyPendingTimers() {
80 if (this._checkFakeTimers()) {
81 this._clock.runToLast();
82 }
83 }
84
85 advanceTimersToNextTimer(steps = 1) {
86 if (this._checkFakeTimers()) {
87 for (let i = steps; i > 0; i--) {
88 this._clock.next(); // Fire all timers at this point: https://github.com/sinonjs/fake-timers/issues/250
89
90 this._clock.tick(0);
91
92 if (this._clock.countTimers() === 0) {
93 break;
94 }
95 }
96 }
97 }
98
99 advanceTimersByTime(msToRun) {
100 if (this._checkFakeTimers()) {
101 this._clock.tick(msToRun);
102 }
103 }
104
105 runAllTicks() {
106 if (this._checkFakeTimers()) {
107 // @ts-expect-error
108 this._clock.runMicrotasks();
109 }
110 }
111
112 useRealTimers() {
113 if (this._fakingTime) {
114 this._clock.uninstall();
115
116 this._fakingTime = false;
117 }
118 }
119
120 useFakeTimers() {
121 if (!this._fakingTime) {
122 const toFake = Object.keys(this._fakeTimers.timers);
123 this._clock = this._fakeTimers.install({
124 loopLimit: this._maxLoops,
125 now: Date.now(),
126 toFake
127 });
128 this._fakingTime = true;
129 }
130 }
131
132 reset() {
133 if (this._checkFakeTimers()) {
134 const {now} = this._clock;
135
136 this._clock.reset();
137
138 this._clock.setSystemTime(now);
139 }
140 }
141
142 setSystemTime(now) {
143 if (this._checkFakeTimers()) {
144 this._clock.setSystemTime(now);
145 }
146 }
147
148 getRealSystemTime() {
149 return Date.now();
150 }
151
152 getTimerCount() {
153 if (this._checkFakeTimers()) {
154 return this._clock.countTimers();
155 }
156
157 return 0;
158 }
159
160 _checkFakeTimers() {
161 if (!this._fakingTime) {
162 this._global.console.warn(
163 'A function to advance timers was called but the timers API is not ' +
164 'mocked with fake timers. Call `jest.useFakeTimers()` in this test or ' +
165 'enable fake timers globally by setting `"timers": "fake"` in the ' +
166 'configuration file\nStack Trace:\n' +
167 (0, _jestMessageUtil().formatStackTrace)(
168 new Error().stack,
169 this._config,
170 {
171 noStackTrace: false
172 }
173 )
174 );
175 }
176
177 return this._fakingTime;
178 }
179}
180
181exports.default = FakeTimers;