Fake Timers
noun
say it: “FAKE TY-merz” /feɪk ˈtaɪmərz/
A testing tool that swaps setTimeout and the clock for pretend ones, so a test can jump minutes ahead in an instant.
What is Fake Timers in JavaScript?
Fake Timers — A testing tool that swaps the real setTimeout, setInterval and clock for pretend ones the test controls. The test moves time forward itself, so a 5-minute seat hold is checked in a moment. In Vitest, vi.useFakeTimers() turns them on.
import { it, expect, vi } from 'vitest';
it('releases the seats after 5 minutes', () => {
vi.useFakeTimers();
const release = vi.fn();
setTimeout(release, 5 * 60 * 1000);
vi.advanceTimersByTime(5 * 60 * 1000);
expect(release).toHaveBeenCalled();
vi.useRealTimers();
});
In a sentence
- “With fake timers on, the test skipped ahead five minutes and checked that the movie seats were released.”
- “I turned on fake timers so the search test didn't have to wait for the 300 millisecond pause.”
Level: Freelancer · More JavaScript terms at this level
Updated