UNPKG

@shopify/jest-dom-mocks

Version:
38 lines (36 loc) 918 B
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.'); } } } export { Timer as default };