Skip to content

Vitest Aide-mémoire

Framework de test nouvelle génération propulsé par Vite.

01

Premiers pas

Test de base

L'API Vitest est compatible avec Jest.

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

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

Mode watch

Vitest dispose d'un mode watch rapide propulsé par HMR.

vitest
# vitest
# vitest run
# vitest coverage

Mocking

vi.mock pour le mocking de modules.

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

Fonctionnalités

Tests du DOM

Fonctionne avec @testing-library pour les tests du DOM.

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

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

Tests in-source

Tests in-source avec 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?