UNPKG

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