0Pricing
Sveltejs Academy · Lesson

Mocking Modules and Fetch

Mock external modules, fetch calls, and SvelteKit modules in unit tests.

Why Mock

Tests should isolate the unit under test. Mock external modules to avoid network calls and flaky tests.

vi.mock

Vitest provides vi.mock to replace modules.

vi.mock("$lib/api", () => ({ getUser: () => ({ name: "Mock" }) }));

All lessons in this course

  1. Vitest Setup for Svelte Projects
  2. @testing-library/svelte: render and fireEvent
  3. Testing Stores and Reactivity
  4. Mocking Modules and Fetch
← Back to Sveltejs Academy