01
Primeiros Passos
Teste Básico
A API do Vitest é compatível com a do Jest.
vitest
import { test, expect } from "vitest";
test("adds", () => {
expect(1 + 2).toBe(3);
});Modo Watch
O Vitest tem um modo watch rápido, alimentado por HMR.
vitest
# vitest
# vitest run
# vitest coverageMocking
vi.mock para mockar módulos.
vitest
vi.mock("./api", () => ({
fetch: vi.fn().mockResolvedValue("data")
}));02
Recursos
Testes de DOM
Funciona com @testing-library para testes de DOM.
vitest
import { render } from "@testing-library/vue";
test("renders", () => {
const { getByText } = render(Component);
expect(getByText("Hello")).toBeTruthy();
});In-Source
Testes in-source com 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?