kb-testing-nextjs

Standardize Next.js unit and component tests with Vitest and React Testing Library.

Updated May 19, 2026
One-click install
npx skills add https://github.com/TimeKast/AgendaInteligente --skill kb-testing-nextjs
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: kb-testing-nextjs
Source: https://github.com/TimeKast/AgendaInteligente/tree/main/.claude/skills/kb-testing-nextjs
Command: npx skills add https://github.com/TimeKast/AgendaInteligente --skill kb-testing-nextjs

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

It solves brittle and flaky unit/component tests in Next.js by standardizing mock, query, and assertion patterns for Vitest and React Testing Library.

Core Features & Use Cases

  • Hoist-safe factory mocks: Prevent Vitest hoisting ReferenceErrors by declaring mocks outside vi.mock() factories and binding them at call time.
  • Behavior-first RTL queries: Apply a consistent DOM query priority using roles and labels, reserving getByTestId as a last resort.
  • Chainable thenable mocks for query builders: Mock fluent Drizzle-style builders with a thenable chain that resolves when awaited.
  • ActionResult-shape assertions: Verify discriminated { data } | { error }-style results by checking return shape instead of expecting throws.
  • Interaction realism: Use userEvent.setup() to drive clicks and form interactions in a way that matches real browser behavior.

Quick Start

Use the kb-testing-nextjs guidelines to write a Next.js Vitest unit test for an action or component by mocking dependencies with hoist-safe vi.mock factories, rendering with RTL, interacting via userEvent.setup(), and asserting on observable output shapes rather than caught exceptions.

Frequently Asked Questions about kb-testing-nextjs

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fix Vitest hoisting ReferenceErrors when mocking modules in Next.js tests?

To fix Vitest hoisting ReferenceErrors in Next.js tests, declare mocks outside `vi.mock()` factories and bind them at call time using hoist-safe factory patterns. This prevents variable initialization errors caused by Vitest's hoisting behavior.

How do I mock Drizzle fluent query builders in Vitest without breaking chained methods?

Mock Drizzle fluent query builders in Vitest by creating chainable thenable mocks that resolve when awaited. This allows the mock to handle sequential method calls and return asynchronous results accurately during unit tests.

What is the best query priority for React Testing Library in Next.js component tests?

The best query priority for React Testing Library in Next.js component tests is behavior-first, using accessible roles and labels. Reserve `getByTestId` as a last resort to ensure tests interact with the DOM as real users would.

How do I assert Next.js Server Action results without expecting thrown exceptions?

Assert Next.js Server Action results by verifying the ActionResult shape, checking for discriminated `{ data } | { error }` return objects. This avoids expecting thrown exceptions and validates the actual output structure of the action.

Can I use userEvent with React Testing Library to simulate realistic form interactions in Vitest?

Yes, you can use `userEvent.setup()` with React Testing Library in Vitest to drive clicks and form interactions. This approach matches real browser behavior more accurately than manual event firing, ensuring reliable component tests.

Why do my Next.js component tests flake when using Vitest and React Testing Library together?

Next.js component tests flake with Vitest and React Testing Library due to improper mock hoisting, non-thenable query builder chains, or unrealistic event simulations. Standardizing on hoist-safe mocks and `userEvent.setup()` resolves these flaky outcomes.