UNPKG

1.32 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var Timer = /** @class */ (function () {
4 function Timer() {
5 this.isUsingFakeTimers = false;
6 }
7 Timer.prototype.mock = function () {
8 if (this.isUsingFakeTimers) {
9 throw new Error('The Timer is already mocked, but you tried to mock it again.');
10 }
11 jest.useFakeTimers();
12 this.isUsingFakeTimers = true;
13 };
14 Timer.prototype.restore = function () {
15 if (!this.isUsingFakeTimers) {
16 throw new Error('The Timer is already real, but you tried to restore it again.');
17 }
18 jest.useRealTimers();
19 this.isUsingFakeTimers = false;
20 };
21 Timer.prototype.isMocked = function () {
22 return this.isUsingFakeTimers;
23 };
24 Timer.prototype.runAllTimers = function () {
25 this.ensureUsingFakeTimers();
26 jest.runAllTimers();
27 };
28 Timer.prototype.runTimersToTime = function (time) {
29 this.ensureUsingFakeTimers();
30 jest.runTimersToTime(time);
31 };
32 Timer.prototype.ensureUsingFakeTimers = function () {
33 if (!this.isUsingFakeTimers) {
34 throw new Error('You must call Timer.mock() before interacting with the mock Timer.');
35 }
36 };
37 return Timer;
38}());
39exports.default = Timer;