01
Primeros pasos
Prueba básica
La API de Vitest es compatible con Jest.
vitest
import { test, expect } from "vitest";
test("adds", () => {
expect(1 + 2).toBe(3);
});Modo watch
Vitest cuenta con un modo watch rápido impulsado por HMR.
vitest
# vitest
# vitest run
# vitest coverageMocking
vi.mock para simular módulos.
vitest
vi.mock("./api", () => ({
fetch: vi.fn().mockResolvedValue("data")
}));02
Características
Pruebas de DOM
Funciona con @testing-library para pruebas de DOM.
vitest
import { render } from "@testing-library/vue";
test("renders", () => {
const { getByText } = render(Component);
expect(getByText("Hello")).toBeTruthy();
});In-Source
Pruebas dentro del propio código fuente con 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?