diff --git a/__test__/lib/workers.test.ts b/__test__/lib/workers.test.ts index 689e8c5..64a4fbc 100644 --- a/__test__/lib/workers.test.ts +++ b/__test__/lib/workers.test.ts @@ -250,4 +250,31 @@ describe('setintervalByTimeout', () => { jest.useRealTimers() }) + + test('setintervalByTimeout should cancel next invoke when clearMyInterval is called', async () => { + + jest.useFakeTimers() + + const mockFn = jest.fn() + const fn = () => { + return new Promise(resolve => { + setTimeout(() => { + mockFn() + resolve() + }, 1000) + }) + } + + + const clearMyInterval = setintervalByTimeout(fn, 500) + + setTimeout(() => { + clearMyInterval() + }, 600) + await jest.advanceTimersByTimeAsync(2000) + expect(mockFn).toHaveBeenCalledTimes(1) + await jest.advanceTimersByTimeAsync(2000) + expect(mockFn).toHaveBeenCalledTimes(1) + jest.useRealTimers() + }) })