You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Vitest unit testing patterns with React Testing Library. Trigger: When writing unit tests for React components, hooks, or utilities.
license
Apache-2.0
metadata
author
version
scope
auto_invoke
prowler-cloud
1.0
root
ui
Writing Vitest tests
Writing React component tests
Writing unit tests for UI
Testing hooks or utilities
allowed-tools
Read, Edit, Write, Glob, Grep, Bash, Task
For E2E tests: Use prowler-test-ui skill (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 - Arrangeconstuser=userEvent.setup();constonSubmit=vi.fn();render(<UserFormonSubmit={onSubmit}/>);// When - Actawaituser.type(screen.getByLabelText(/name/i),"John");awaituser.click(screen.getByRole("button",{name: /submit/i}));// Then - Assertexpect(onSubmit).toHaveBeenCalledWith({name: "John"});});
// ✅ ALWAYS use userEventconstuser=userEvent.setup();awaituser.click(button);awaituser.type(input,"hello");// ❌ NEVER use fireEvent for interactionsfireEvent.click(button);
Async Testing Patterns
// ✅ findBy for elements that appear asyncconstelement=awaitscreen.findByText(/loaded/i);// ✅ waitFor for assertionsawaitwaitFor(()=>{expect(screen.getByText(/success/i)).toBeInTheDocument();});// ✅ ONE assertion per waitForawaitwaitFor(()=>expect(mockFn).toHaveBeenCalled());awaitwaitFor(()=>expect(screen.getByText(/done/i)).toBeVisible());// ❌ NEVER multiple assertions in waitForawaitwaitFor(()=>{expect(mockFn).toHaveBeenCalled();expect(screen.getByText(/done/i)).toBeVisible();// Slower failures});