vi.mock()
Replaces an entire module with a mock version. Useful for mocking external APIs, databases, or third-party libraries.
Syntax
testing
vi.mock("module-name")
vi.mock("module-name", () => ({ ... }))Example
testing
vi.mock("../lib/emailService", () => ({
sendEmail: vi.fn().mockResolvedValue({ success: true }),
}));
test("sends welcome email on signup", async () => {
const { sendEmail } = await import("../lib/emailService");
await signUp("bob@example.com");
expect(sendEmail).toHaveBeenCalledWith("bob@example.com", "Welcome!");
});