0

Testing: Vitest + RTL

Testingused in ~70% of apps

Interviewers don't ask 'do you write tests?' — they ask WHAT you test and WHERE. The candidate who tries to unit-test an async Server Component with Vitest and watches it explode looks like they've never shipped. Knowing the boundary — sync components, Server Actions and Zod schemas in Vitest; async RSC and full flows in e2e — is the senior signal.

Core idea. Vitest + React Testing Library is your fast inner loop for everything synchronous: client components, pure functions, Zod schemas, and Server Actions called as plain functions. RTL tests behaviour a user can observe (roles, text), not implementation details. The one thing this stack CANNOT do is render an async Server Component — those need e2e.
Playground, this isn't a screenshot
LikeButton.tsxclick it yourself
function LikeButton() {
const [n, setN] = useState(0)
onClick={() => setN(n + 1)}
return <button>Like · {n}</button>
}
LikeButton.test.tsxvitest + RTL
test('counts up when clicked', async () => {
const user = userEvent.setup()
render(<LikeButton />)
const btn = screen.getByRole('button', { name: /like/i })
expect(btn).toHaveTextContent('Like · 0')
await user.click(btn)
expect(btn).toHaveTextContent('Like · 1')
})

The test finds the button by its role + accessible name and asserts the visible label — so it survives a refactor of the internals, yet still fails the moment real behaviour breaks.

↑ play a little first, then dig in