09 / 12
vi.fn, vi.mock and mocking an API with MSW.
A mock is a test copy standing in for a real dependency. It puts the test under your control. You do not use a real network request, a real analytics service or a real timer.
Do
Network requests (API calls).
Analytics and logging services.
APIs that are missing in the browser or have a side effect (window.open, navigator.sendBeacon).
Time (setTimeout, Date).
Don't
Your own child components. The user sees them on screen as they really are.
The component under test itself.
React or Testing Library functions.
vi.fn() produces a function that records its calls. It is used to test callback props. You used it for onAddToCart and onApply in the earlier steps.
| Code | Meaning |
|---|---|
vi.fn() | An empty fake function. Returns undefined. |
vi.fn().mockReturnValue(5) | Returns 5 on every call. |
vi.fn().mockResolvedValue(data) | Returns Promise.resolve(data). For async functions. |
vi.fn().mockRejectedValue(new Error("error")) | Returns a rejected Promise. |
For the return values in this table, see the Vitest documentation.1
const loadProducts = vi.fn().mockResolvedValue(products);
render(<ProductList loadProducts={loadProducts} onAddToCart={vi.fn()} />);
await screen.findAllByRole("article");
expect(loadProducts).toHaveBeenCalledTimes(1);| Check | Meaning |
|---|---|
toHaveBeenCalled() | It was called at least once. |
toHaveBeenCalledTimes(n) | It was called exactly n times. |
toHaveBeenCalledWith(...) | It was called with these arguments at least once. |
not.toHaveBeenCalled() | It was never called. |
The product button sends an analytics event on every click:
import { trackEvent } from "./analytics";
import type { Product } from "./types";
type AddToCartButtonProps = {
product: Product;
onAdd: (product: Product) => void;
};
export function AddToCartButton({ product, onAdd }: AddToCartButtonProps) {
function handleClick() {
onAdd(product);
trackEvent("add_to_cart", { productId: product.id });
}
return (
<button type="button" onClick={handleClick}>
Add to cart
</button>
);
}The trackEvent function calls navigator.sendBeacon. jsdom does not provide that API, so without a mock the test fails. Mock the module:
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { AddToCartButton } from "./AddToCartButton";
import { trackEvent } from "./analytics";
import type { Product } from "./types";
vi.mock(import("./analytics"));
const keyboard: Product = {
id: 2,
name: "Mechanical Keyboard",
price: 2199,
inStock: true,
};
test("adds the product and sends the analytics event", async () => {
const user = userEvent.setup();
const onAdd = vi.fn();
render(<AddToCartButton product={keyboard} onAdd={onAdd} />);
await user.click(screen.getByRole("button", { name: "Add to cart" }));
expect(onAdd).toHaveBeenCalledWith(keyboard);
expect(trackEvent).toHaveBeenCalledWith("add_to_cart", { productId: 2 });
});If you do not pass a factory function, Vitest loads the original module and mocks everything it exports automatically. The methods return undefined.2 The import("./analytics") form has TypeScript check the path for typos. If the file moves, the path and the types are updated together.2
Warning
Vitest moves the vi.mock call to the top of the file (hoisting). Wherever you write the call, it runs before the imports. That is why you write it at the file's top level. Inside the mock's factory function you cannot use the other variables in the file.2
Tip
By default Vitest clears the mock call history before every test (clearMocks), so a call count in one test does not affect another.3
vi.spyOn(object, "method") watches an existing method. For example: vi.spyOn(window, "open").mockImplementation(() => null). That lets you test that the "Share" button opens a new window.
vi.restoreAllMocks() restores only the spies created with vi.spyOn. It does not affect automatically mocked modules.4
To undo the spy after every test, write afterEach(() => vi.restoreAllMocks()).
The products come from an API. Instead of mocking the fetch function by hand, use MSW (Mock Service Worker).5 MSW intercepts the network request and returns a fake response.67 Your application code keeps using the real fetch.
Write the API call first:
import type { Product } from "./types";
export async function fetchProducts(): Promise<Product[]> {
const response = await fetch("https://api.shop.test/products");
if (!response.ok) {
throw new Error("Products could not be loaded.");
}
return response.json();
}Warning
Do not use a relative URL such as fetch("/products") in a test. In the Node environment a relative URL cannot be resolved and the request fails. Use the full URL.
Install the package:8
$ pnpm add -D mswDefine the default responses in src/mocks/handlers.ts. In MSW 3, http and HttpResponse come from the msw/http path:9
import { http, HttpResponse } from "msw/http";
import type { Product } from "../types";
export const products: Product[] = [
{ id: 1, name: "Wireless Headphones", price: 1299, oldPrice: 1499, inStock: true },
{ id: 2, name: "Mechanical Keyboard", price: 2199, inStock: true },
{ id: 3, name: "Gaming Mouse", price: 899, inStock: false },
];
export const handlers = [
http.get("https://api.shop.test/products", () =>
HttpResponse.json(products),
),
];Create the server in src/mocks/server.ts:
import { setupServer } from "msw/node";
import { handlers } from "./handlers";
export const server = setupServer(...handlers);Start the server in src/vitest.setup.ts:
import "@testing-library/jest-dom/vitest";
import { server } from "./mocks/server";
beforeAll(() => server.listen({ onUnhandledFrame: "error" }));
afterEach(() => server.resetHandlers());
afterAll(() => server.close());| Line | Its job |
|---|---|
onUnhandledFrame: "error" | A request with no handler fails. You never hit the real API by accident.10 |
resetHandlers() | Deletes the temporary handlers added in a test. The tests do not affect each other. |
Tip
If you use MSW 2.x, the option is called onUnhandledRequest, and http and HttpResponse are imported from the msw package directly.9
Now use the fetchProducts function itself:
import { render, screen } from "@testing-library/react";
import { http, HttpResponse } from "msw/http";
import { fetchProducts } from "./fetch-products";
import { server } from "./mocks/server";
import { ProductList } from "./ProductList";
test("shows the products from the API", async () => {
render(<ProductList loadProducts={fetchProducts} onAddToCart={vi.fn()} />);
expect(await screen.findAllByRole("article")).toHaveLength(3);
});
test("shows the empty state when the API returns an empty list", async () => {
server.use(
http.get("https://api.shop.test/products", () => HttpResponse.json([])),
);
render(<ProductList loadProducts={fetchProducts} onAddToCart={vi.fn()} />);
expect(await screen.findByText("No products found.")).toBeInTheDocument();
});server.use adds the new handler in front of the existing ones. That handler stays until resetHandlers is called.11 This is why the resetHandlers call in afterEach is needed.
Tip
You can use the same handlers in the browser for the development environment too. That topic is outside this guide's scope.
| Situation | Tool |
|---|---|
| A callback prop | vi.fn() |
| Analytics, a logger, a browser API | vi.mock or vi.spyOn |
| An API call | MSW |
setTimeout, Date | Fake timers.12 |
What happens when the API returns a 500? Test that:
With server.use, add a handler for https://api.shop.test/products that returns new HttpResponse(null, { status: 500 }).
Render the ProductList component with fetchProducts.
Confirm the Products could not be loaded. message with findByRole("alert").
Sources
The behaviour of `mockReturnValue`, `mockResolvedValue` and `mockRejectedValue`.
`vi.mock` hoisting, the automatic mocking rules and the `import()` form.
The `vi.clearAllMocks()` call before every test, and the default value.
The behaviour of `vi.spyOn`, `vi.restoreAllMocks` and `mockImplementation`.
The code samples use MSW v1 and Jest. The idea of mocking at the network boundary is the same as this lesson's.
Setting MSW up, `onUnhandledRequest` and the server lifecycle.
How MSW intercepts the request and returns a fake response. The talk covers version 2.0.
Installation, defining handlers, `setupServer` and the Vitest setup file.
The `msw/http` import path, the `onUnhandledFrame` name and the Node 22 requirement.
The `onUnhandledFrame` option and the `"error"` strategy.
Adding a handler at runtime, and how long it lasts.
Mocking time, and `Date` changing with `vi.useFakeTimers`.
Check the lesson's main ideas with three questions.
Your progress is stored only in this browser.
Last updated: November 3, 2026