UNPKG

@shopify/jest-dom-mocks

Version:
42 lines (38 loc) 996 B
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); class Timer { constructor() { this.isUsingFakeTimers = false; } mock() { if (this.isUsingFakeTimers) { throw new Error('The Timer is already mocked, but you tried to mock it again.'); } jest.useFakeTimers(); this.isUsingFakeTimers = true; } restore() { if (!this.isUsingFakeTimers) { throw new Error('The Timer is already real, but you tried to restore it again.'); } jest.useRealTimers(); this.isUsingFakeTimers = false; } isMocked() { return this.isUsingFakeTimers; } runAllTimers() { this.ensureUsingFakeTimers(); jest.runAllTimers(); } runTimersToTime(time) { this.ensureUsingFakeTimers(); jest.advanceTimersByTime(time); } ensureUsingFakeTimers() { if (!this.isUsingFakeTimers) { throw new Error('You must call Timer.mock() before interacting with the mock Timer.'); } } } exports["default"] = Timer;