Skip to content

Vitest Cheatsheet

Next-generation testing framework powered by Vite.

01

Getting Started

Basic Test

Vitest API is compatible with Jest.

vitest
import { test, expect } from "vitest";

test("adds", () => {
  expect(1 + 2).toBe(3);
});

Watch Mode

Vitest has fast HMR-powered watch mode.

vitest
# vitest
# vitest run
# vitest coverage

Mocking

vi.mock for module mocking.

vitest
vi.mock("./api", () => ({
  fetch: vi.fn().mockResolvedValue("data")
}));
02

Features

DOM Testing

Works with @testing-library for DOM tests.

vitest
import { render } from "@testing-library/vue";

test("renders", () => {
  const { getByText } = render(Component);
  expect(getByText("Hello")).toBeTruthy();
});

In-Source

In-source testing with import.meta.vitest.

vitest
// function.ts
export function add(a, b) {
  return a + b;
}

if (import.meta.vitest) {
  const { test, expect } = import.meta.vitest;
  test("add", () => expect(add(1,2)).toBe(3));
}

Was this helpful?