Mocking Modules and Fetch
Mock external modules, fetch calls, and SvelteKit modules in unit tests.
Mocking Modules and Fetch is a free Sveltejs Academy lesson on CoddyKit. This is lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Sveltejs Academy learning path, and your progress syncs across the web and the CoddyKit app. The Sveltejs Academy course includes 4 lessons in total.
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" }) }));Mock fetch
Use vi.spyOn(global, "fetch") or a library like msw for realistic API mocks.
vi.spyOn(global, "fetch").mockResolvedValue({ json: () => ({ items: [] }) });Module Hoisting
vi.mock calls are hoisted. Declare them at the top of the file.
Restore Mocks
Use vi.restoreAllMocks() in afterEach to clean up.
MSW for HTTP
Mock Service Worker intercepts at the network layer for realistic API behavior in tests.
Mocking SvelteKit Modules
You can mock $app/stores, $app/navigation, and other SvelteKit imports for isolated tests.
Type-Safe Mocks
Use vi.fn() with typed signatures for type-safe spies.
Clear Between Tests
Use vi.clearAllMocks() or beforeEach setups to start fresh.
Partial Mocks
Mock only specific exports of a module while keeping the rest real.
Real vs Mock Trade-off
Over-mocking can make tests meaningless. Test integration with real implementations where reasonable.
Quick Check
What does vi.mock do?
Recap
Use vi.mock and spies to isolate units; consider MSW for realistic API mocks at the network layer.
Learn HTML with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 41
- Lessons
- 163
Frequently Asked Questions
Is the “Mocking Modules and Fetch” lesson free?
Yes — the full text of “Mocking Modules and Fetch” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Sveltejs Academy course, upgrade to CoddyKit PRO. The Sveltejs Academy course includes 4 lessons in total.
What will I learn in “Mocking Modules and Fetch”?
Mock external modules, fetch calls, and SvelteKit modules in unit tests. You practise Sveltejs Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Sveltejs Academy?
No prior experience is required. Sveltejs Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
How long does the “Mocking Modules and Fetch” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Sveltejs Academy lesson?
Yes. Every Sveltejs Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Vitest Setup for Svelte Projects
- @testing-library/svelte: render and fireEvent
- Testing Stores and Reactivity
- Mocking Modules and Fetch