@shopify/jest-dom-mocks
Version:
Jest mocking utilities for working with the DOM
39 lines (36 loc) • 989 B
JavaScript
import FakePromise from 'promise';
class Promise$1 {
constructor() {
this.originalPromise = global.Promise;
this.isUsingFakePromise = false;
}
mock() {
if (this.isUsingFakePromise) {
throw new Error('Promise is already mocked, but you tried to mock it again.');
}
jest.useFakeTimers();
global.Promise = FakePromise;
this.isUsingFakePromise = true;
}
restore() {
if (!this.isUsingFakePromise) {
throw new Error('Promise is already real, but you tried to restore it again.');
}
jest.useRealTimers();
global.Promise = this.originalPromise;
this.isUsingFakePromise = false;
}
isMocked() {
return this.isUsingFakePromise;
}
runPending() {
this.ensureUsingFakePromise();
jest.runAllTimers();
}
ensureUsingFakePromise() {
if (!this.isUsingFakePromise) {
throw new Error('You must call Promise.mock() before interacting with the mock Promise.');
}
}
}
export { Promise$1 as default };