testing

Enforces vitest and Testing Library conventions for React component tests with MSW mocks and form fillers.

10|3|Updated Oct 23, 2023
One-click install
npx skills add https://github.com/Layer-Fi/layer-react --skill testing-layer-fi
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: testing
Source: https://github.com/Layer-Fi/layer-react/tree/main/src/testUtils
Command: npx skills add https://github.com/Layer-Fi/layer-react --skill testing-layer-fi

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires vitest, @testing-library/react, @testing-library/user-event, @testing-library/jest-dom, msw.

What problem does it solve? Writing React component tests that survive refactors is hard: tests that assert on internal state, use brittle selectors, or leave requests unmocked break constantly and pass when they shouldn't. This Skill codifies the repository's testing conventions so every test asserts what a user can perceive, uses accessible queries, and mocks every network call deterministically. ## Core Features & Use Cases - Query and interaction discipline: Enforces the getByRole-first query priority, userEvent over fireEvent, awaited interactions, and findBy* over waitFor for DOM assertions. - Standardized rendering and mocking: Requires a module-scope render helper wrapping LayerTestProvider, MSW per-test mocks via endpoint mock/mockError builders, and request-body assertions through onRequest spies. - Form fillers and fixed clocks: Provides createFormFiller kinds (text, number, checkbox, toggle, radio, comboBox) addressed by accessible label, plus setupFakeSystemTime with pre-derived fixed date constants. - Use Case: When adding a test for a new account-creation form, define one renderForm helper, declare a FillFormSpec constant, mock the POST endpoint with server.use, and assert the exact request body the app sent. ## Quick Start Write a vitest test for my React component following this repository's testing conventions, using LayerTestProvider, MSW endpoint mocks, and the form filler helpers.

Frequently Asked Questions about testing

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

FAQPage Schema
How do I test React components with vitest and Testing Library?▼

Render the component inside a provider wrapper, query elements with getByRole using accessible names, and interact via userEvent with every call awaited. Assert on visible output and request payloads, never on internal state or hook call counts.

How do I mock API requests in vitest tests with MSW?▼

Import the endpoint module and register its mock or mockError builder with server.use per test. Use the onRequest callback to spy on the request body and params instead of stubbing fetch, since unmocked requests fail the test.

userEvent vs fireEvent in Testing Library, which should I use?▼

Use userEvent. fireEvent dispatches a single synthetic event, while userEvent fires the real keydown/keypress/input sequence and respects disabled states and pointer-events, which is what actually catches broken handlers.

Why do I get 'not wrapped in act' warnings in React tests?▼

The warning means a state update landed that the test never awaited, usually from an un-awaited user.click or an unresolved async render. Await the visible consequence with findBy or waitFor instead of wrapping in act().

How do I test date-dependent logic with vitest fake timers?▼

Pin the clock with vi.useFakeTimers and vi.setSystemTime in beforeEach, restoring real timers in afterEach. When combining a fake clock with userEvent, pass advanceTimers: vi.advanceTimersByTime to userEvent.setup or interactions will hang.

When should I use getByTestId in Testing Library?▼

Only when no role, label, text, or alt query can address the element. Role-based queries match text split across child elements and print the accessible tree on failure, making tests more robust than test ids.