Mocking Fetch & Timers
Stub network with fetch mocks and control time with fake timers; write stable, fast tests for async components.
Why mock network & time
Goal: Mock async work so tests are fast and reliable.
- Stub fetch responses
- Control timers with fake clocks
- Assert using findBy*/waitFor
Common patterns
- fetch: spyOn global.fetch → mockResolvedValue
- Timers: vi.useFakeTimers(); advance timers
- Prefer findBy* for async DOM
All lessons in this course
- React Testing Library + Vitest/Jest Setup
- Queries, Events, Assertions — Forms & Async UI
- Mocking Fetch & Timers