01
快速上手
基础测试
Vitest API 与 Jest 兼容。
vitest
import { test, expect } from "vitest";
test("adds", () => {
expect(1 + 2).toBe(3);
});监听模式
Vitest 拥有基于 HMR 的快速监听模式。
vitest
# vitest
# vitest run
# vitest coverage模拟
用 vi.mock 模拟模块 。
vitest
vi.mock("./api", () => ({
fetch: vi.fn().mockResolvedValue("data")
}));02
核心特性
DOM 测试
配合 @testing-library 做 DOM 测试。
vitest
import { render } from "@testing-library/vue";
test("renders", () => {
const { getByText } = render(Component);
expect(getByText("Hello")).toBeTruthy();
});源内测试
用 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));
}这篇内容对您有帮助吗?