vitest
18
总安装量
18
周安装量
#19749
全站排名
安装命令
npx skills add https://github.com/prowler-cloud/prowler --skill vitest
Agent 安装分布
opencode
18
gemini-cli
18
github-copilot
18
codex
18
kimi-cli
18
amp
18
Skill 文档
For E2E tests: Use
prowler-test-uiskill (Playwright). This skill covers unit/integration tests with Vitest + React Testing Library.
Test Structure (REQUIRED)
Use Given/When/Then (AAA) pattern with comments:
it("should update user name when form is submitted", async () => {
// Given - Arrange
const user = userEvent.setup();
const onSubmit = vi.fn();
render(<UserForm onSubmit={onSubmit} />);
// When - Act
await user.type(screen.getByLabelText(/name/i), "John");
await user.click(screen.getByRole("button", { name: /submit/i }));
// Then - Assert
expect(onSubmit).toHaveBeenCalledWith({ name: "John" });
});
Describe Block Organization
describe("ComponentName", () => {
describe("when [condition]", () => {
it("should [expected behavior]", () => {});
});
});
Group by behavior, NOT by method.
Query Priority (REQUIRED)
| Priority | Query | Use Case |
|---|---|---|
| 1 | getByRole |
Buttons, inputs, headings |
| 2 | getByLabelText |
Form fields |
| 3 | getByPlaceholderText |
Inputs without label |
| 4 | getByText |
Static text |
| 5 | getByTestId |
Last resort only |
// â
GOOD
screen.getByRole("button", { name: /submit/i });
screen.getByLabelText(/email/i);
// â BAD
container.querySelector(".btn-primary");
userEvent over fireEvent (REQUIRED)
// â
ALWAYS use userEvent
const user = userEvent.setup();
await user.click(button);
await user.type(input, "hello");
// â NEVER use fireEvent for interactions
fireEvent.click(button);
Async Testing Patterns
// â
findBy for elements that appear async
const element = await screen.findByText(/loaded/i);
// â
waitFor for assertions
await waitFor(() => {
expect(screen.getByText(/success/i)).toBeInTheDocument();
});
// â
ONE assertion per waitFor
await waitFor(() => expect(mockFn).toHaveBeenCalled());
await waitFor(() => expect(screen.getByText(/done/i)).toBeVisible());
// â NEVER multiple assertions in waitFor
await waitFor(() => {
expect(mockFn).toHaveBeenCalled();
expect(screen.getByText(/done/i)).toBeVisible(); // Slower failures
});
Mocking
// Basic mock
const handleClick = vi.fn();
// Mock with return value
const fetchUser = vi.fn().mockResolvedValue({ name: "John" });
// Always clean up
afterEach(() => {
vi.restoreAllMocks();
});
vi.spyOn vs vi.mock
| Method | When to Use |
|---|---|
vi.spyOn |
Observe without replacing (PREFERRED) |
vi.mock |
Replace entire module (use sparingly) |
Common Matchers
// Presence
expect(element).toBeInTheDocument();
expect(element).toBeVisible();
// State
expect(button).toBeDisabled();
expect(input).toHaveValue("text");
expect(checkbox).toBeChecked();
// Content
expect(element).toHaveTextContent(/hello/i);
expect(element).toHaveAttribute("href", "/home");
// Functions
expect(fn).toHaveBeenCalledWith(arg1, arg2);
expect(fn).toHaveBeenCalledTimes(2);
What NOT to Test
// â Internal state
expect(component.state.isLoading).toBe(true);
// â Third-party libraries
expect(axios.get).toHaveBeenCalled();
// â Static content (unless conditional)
expect(screen.getByText("Welcome")).toBeInTheDocument();
// â
User-visible behavior
expect(screen.getByRole("button")).toBeDisabled();
File Organization
components/
âââ Button/
â âââ Button.tsx
â âââ Button.test.tsx # Co-located
â âââ index.ts
Commands
pnpm test # Watch mode
pnpm test:run # Single run
pnpm test:coverage # With coverage
pnpm test Button # Filter by name