@ranimontagna/agent-toolkit 0.1.5 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +68 -14
- package/package.json +1 -1
- package/skills/backend/go/golang-patterns/LICENSE +21 -0
- package/skills/backend/go/golang-patterns/NOTICE.md +10 -0
- package/skills/backend/go/golang-patterns/SKILL.md +674 -0
- package/skills/backend/go/golang-testing/LICENSE +21 -0
- package/skills/backend/go/golang-testing/NOTICE.md +10 -0
- package/skills/backend/go/golang-testing/SKILL.md +329 -0
- package/skills/backend/java/java-coding-standards/LICENSE +21 -0
- package/skills/backend/java/java-coding-standards/NOTICE.md +10 -0
- package/skills/backend/java/java-coding-standards/SKILL.md +383 -0
- package/skills/backend/java/java-junit/LICENSE +21 -0
- package/skills/backend/java/java-junit/NOTICE.md +10 -0
- package/skills/backend/java/java-junit/SKILL.md +64 -0
- package/skills/frontend/react/react-patterns/LICENSE +21 -0
- package/skills/frontend/react/react-patterns/NOTICE.md +11 -0
- package/skills/frontend/react/react-patterns/SKILL.md +341 -0
- package/skills/frontend/react/react-patterns/rules/react/LICENSE +21 -0
- package/skills/frontend/react/react-patterns/rules/react/NOTICE.md +11 -0
- package/skills/frontend/react/react-patterns/rules/react/coding-style.md +109 -0
- package/skills/frontend/react/react-patterns/rules/react/hooks.md +187 -0
- package/skills/frontend/react/react-patterns/rules/react/patterns.md +194 -0
- package/skills/frontend/react/react-patterns/rules/react/security.md +180 -0
- package/skills/frontend/react/react-patterns/rules/react/testing.md +208 -0
- package/skills/frontend/react/react-performance/LICENSE +21 -0
- package/skills/frontend/react/react-performance/NOTICE.md +11 -0
- package/skills/frontend/react/react-performance/SKILL.md +574 -0
- package/skills/frontend/react/react-performance/rules/react/LICENSE +21 -0
- package/skills/frontend/react/react-performance/rules/react/NOTICE.md +11 -0
- package/skills/frontend/react/react-performance/rules/react/coding-style.md +109 -0
- package/skills/frontend/react/react-performance/rules/react/hooks.md +187 -0
- package/skills/frontend/react/react-performance/rules/react/patterns.md +194 -0
- package/skills/frontend/react/react-performance/rules/react/security.md +180 -0
- package/skills/frontend/react/react-performance/rules/react/testing.md +208 -0
- package/skills/frontend/react/react-testing/LICENSE +21 -0
- package/skills/frontend/react/react-testing/NOTICE.md +11 -0
- package/skills/frontend/react/react-testing/SKILL.md +423 -0
- package/skills/frontend/react/react-testing/rules/react/LICENSE +21 -0
- package/skills/frontend/react/react-testing/rules/react/NOTICE.md +11 -0
- package/skills/frontend/react/react-testing/rules/react/coding-style.md +109 -0
- package/skills/frontend/react/react-testing/rules/react/hooks.md +187 -0
- package/skills/frontend/react/react-testing/rules/react/patterns.md +194 -0
- package/skills/frontend/react/react-testing/rules/react/security.md +180 -0
- package/skills/frontend/react/react-testing/rules/react/testing.md +208 -0
- package/skills/frontend/react-native/react-native-expert/LICENSE +21 -0
- package/skills/frontend/react-native/react-native-expert/NOTICE.md +11 -0
- package/skills/frontend/react-native/react-native-expert/SKILL.md +187 -0
- package/skills/frontend/react-native/react-native-expert/references/expo-router.md +187 -0
- package/skills/frontend/react-native/react-native-expert/references/list-optimization.md +204 -0
- package/skills/frontend/react-native/react-native-expert/references/platform-handling.md +188 -0
- package/skills/frontend/react-native/react-native-expert/references/project-structure.md +171 -0
- package/skills/frontend/react-native/react-native-expert/references/storage-hooks.md +173 -0
- package/skills/frontend/react-native/react-native-unistyles-v3/LICENSE +21 -0
- package/skills/frontend/react-native/react-native-unistyles-v3/NOTICE.md +11 -0
- package/skills/frontend/react-native/react-native-unistyles-v3/SKILL.md +159 -0
- package/skills/frontend/react-native/react-native-unistyles-v3/references/api-reference.md +495 -0
- package/skills/frontend/react-native/react-native-unistyles-v3/references/common-issues.md +389 -0
- package/skills/frontend/react-native/react-native-unistyles-v3/references/setup-guide.md +217 -0
- package/skills/frontend/react-native/react-native-unistyles-v3/references/styling-patterns.md +705 -0
- package/skills/frontend/react-native/react-native-unistyles-v3/references/third-party-integration.md +318 -0
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
---
|
|
2
|
+
paths:
|
|
3
|
+
- "**/*.test.tsx"
|
|
4
|
+
- "**/*.test.jsx"
|
|
5
|
+
- "**/*.spec.tsx"
|
|
6
|
+
- "**/*.spec.jsx"
|
|
7
|
+
- "**/__tests__/**/*.ts"
|
|
8
|
+
- "**/__tests__/**/*.tsx"
|
|
9
|
+
---
|
|
10
|
+
# React Testing
|
|
11
|
+
|
|
12
|
+
> This file extends the upstream `typescript/testing.md` and `common/testing.md` rules with React specific content.
|
|
13
|
+
|
|
14
|
+
## Library Choice
|
|
15
|
+
|
|
16
|
+
- **React Testing Library (RTL)** — the standard for component testing. Tests behavior through the rendered DOM.
|
|
17
|
+
- **Vitest** — preferred runner for new Vite-based projects. Faster than Jest, native ESM, same API.
|
|
18
|
+
- **Jest** — still the default for Next.js / CRA projects. RTL works identically.
|
|
19
|
+
- **Playwright Component Testing** — when component tests need a real browser engine (animation, layout, complex events)
|
|
20
|
+
- **Cypress Component Testing** — alternative real-browser component runner
|
|
21
|
+
|
|
22
|
+
Pick one component test runner per project — do not mix RTL + Playwright CT in the same repo.
|
|
23
|
+
|
|
24
|
+
## Core Principle
|
|
25
|
+
|
|
26
|
+
Test what the user sees and does, not implementation details.
|
|
27
|
+
|
|
28
|
+
- Query by accessible role first, then label, then text — fall back to `data-testid` only when nothing else fits
|
|
29
|
+
- Never assert on internal state, props passed to children, or which hooks were called
|
|
30
|
+
- Refactor without breaking tests = the test was testing behavior; that is the goal
|
|
31
|
+
|
|
32
|
+
## Query Priority
|
|
33
|
+
|
|
34
|
+
RTL exposes queries in three families. Use this priority order top-down:
|
|
35
|
+
|
|
36
|
+
1. **Accessible to everyone**
|
|
37
|
+
- `getByRole(role, { name })` — primary choice
|
|
38
|
+
- `getByLabelText` — for form inputs
|
|
39
|
+
- `getByPlaceholderText` — when no label is available (and add a label)
|
|
40
|
+
- `getByText` — for non-interactive text
|
|
41
|
+
- `getByDisplayValue` — for form fields with a current value
|
|
42
|
+
|
|
43
|
+
2. **Semantic queries**
|
|
44
|
+
- `getByAltText` — for images
|
|
45
|
+
- `getByTitle` — last resort, low accessibility value
|
|
46
|
+
|
|
47
|
+
3. **Test IDs**
|
|
48
|
+
- `getByTestId("some-id")` — escape hatch only, when none of the above work
|
|
49
|
+
|
|
50
|
+
`getBy*` throws when no match. `queryBy*` returns null (use for asserting absence). `findBy*` returns a promise (use for async).
|
|
51
|
+
|
|
52
|
+
## User Interaction
|
|
53
|
+
|
|
54
|
+
Prefer `userEvent` over `fireEvent`. `userEvent` simulates real browser sequences (focus, keydown, beforeinput, input, keyup) — `fireEvent` dispatches a single synthetic event.
|
|
55
|
+
|
|
56
|
+
```tsx
|
|
57
|
+
import userEvent from "@testing-library/user-event";
|
|
58
|
+
|
|
59
|
+
test("submits the form", async () => {
|
|
60
|
+
const user = userEvent.setup();
|
|
61
|
+
render(<UserForm onSubmit={handleSubmit} />);
|
|
62
|
+
|
|
63
|
+
await user.type(screen.getByLabelText("Email"), "user@example.com");
|
|
64
|
+
await user.click(screen.getByRole("button", { name: /save/i }));
|
|
65
|
+
|
|
66
|
+
expect(handleSubmit).toHaveBeenCalledWith({ email: "user@example.com" });
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
- Always `await` `userEvent` calls — they are async
|
|
71
|
+
- Call `userEvent.setup()` once at the top of each test, then reuse the returned `user`
|
|
72
|
+
|
|
73
|
+
## Async Assertions
|
|
74
|
+
|
|
75
|
+
```tsx
|
|
76
|
+
// WRONG: synchronous query for async-rendered content
|
|
77
|
+
expect(screen.getByText("Loaded")).toBeInTheDocument(); // throws — not in DOM yet
|
|
78
|
+
|
|
79
|
+
// CORRECT: findBy* (returns a promise, retries)
|
|
80
|
+
expect(await screen.findByText("Loaded")).toBeInTheDocument();
|
|
81
|
+
|
|
82
|
+
// CORRECT: waitFor for non-element assertions
|
|
83
|
+
await waitFor(() => expect(saveSpy).toHaveBeenCalled());
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
- `findBy*` for async element appearance
|
|
87
|
+
- `waitFor` for async expectations on side effects or other matchers
|
|
88
|
+
- Never `setTimeout` + assertion — flaky
|
|
89
|
+
|
|
90
|
+
## Network Mocking with MSW
|
|
91
|
+
|
|
92
|
+
Use Mock Service Worker for any test that hits a network boundary. MSW runs at the network layer, so the component, hooks, and fetch library all behave as in production.
|
|
93
|
+
|
|
94
|
+
```tsx
|
|
95
|
+
// test setup
|
|
96
|
+
import { setupServer } from "msw/node";
|
|
97
|
+
import { http, HttpResponse } from "msw";
|
|
98
|
+
|
|
99
|
+
const server = setupServer(
|
|
100
|
+
http.get("/api/users/:id", ({ params }) =>
|
|
101
|
+
HttpResponse.json({ id: params.id, name: "Alice" }),
|
|
102
|
+
),
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
beforeAll(() => server.listen());
|
|
106
|
+
afterEach(() => server.resetHandlers());
|
|
107
|
+
afterAll(() => server.close());
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Per-test override:
|
|
111
|
+
|
|
112
|
+
```tsx
|
|
113
|
+
test("renders error on 500", async () => {
|
|
114
|
+
server.use(http.get("/api/users/:id", () => new HttpResponse(null, { status: 500 })));
|
|
115
|
+
render(<UserPage id="1" />);
|
|
116
|
+
expect(await screen.findByText(/something went wrong/i)).toBeInTheDocument();
|
|
117
|
+
});
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Avoid Snapshot Tests for Components
|
|
121
|
+
|
|
122
|
+
Snapshots of rendered output are brittle, hard to review, and rubber-stamped by reviewers. Use them only for:
|
|
123
|
+
|
|
124
|
+
- Pure data serialization (e.g., a transformer that produces a stable string)
|
|
125
|
+
- Catching unintended regressions in non-visual output
|
|
126
|
+
|
|
127
|
+
For component visual regression, use Playwright / Cypress / Percy screenshots — actual visual diffs, not DOM diffs.
|
|
128
|
+
|
|
129
|
+
## Test Setup Helpers
|
|
130
|
+
|
|
131
|
+
Wrap providers once:
|
|
132
|
+
|
|
133
|
+
```tsx
|
|
134
|
+
function renderWithProviders(ui: React.ReactElement) {
|
|
135
|
+
return render(
|
|
136
|
+
<QueryClientProvider client={new QueryClient()}>
|
|
137
|
+
<ThemeProvider theme={lightTheme}>
|
|
138
|
+
<Router>{ui}</Router>
|
|
139
|
+
</ThemeProvider>
|
|
140
|
+
</QueryClientProvider>,
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Export from `test-utils.tsx` and use everywhere.
|
|
146
|
+
|
|
147
|
+
## Custom Hook Testing
|
|
148
|
+
|
|
149
|
+
Use `renderHook` from RTL:
|
|
150
|
+
|
|
151
|
+
```tsx
|
|
152
|
+
import { renderHook, act } from "@testing-library/react";
|
|
153
|
+
|
|
154
|
+
test("useCounter increments", () => {
|
|
155
|
+
const { result } = renderHook(() => useCounter());
|
|
156
|
+
act(() => result.current.increment());
|
|
157
|
+
expect(result.current.count).toBe(1);
|
|
158
|
+
});
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
- Always wrap state-changing calls in `act`
|
|
162
|
+
- Always test through the public hook API, not internal implementation
|
|
163
|
+
|
|
164
|
+
## Accessibility Assertions
|
|
165
|
+
|
|
166
|
+
```tsx
|
|
167
|
+
import { axe } from "vitest-axe"; // or jest-axe
|
|
168
|
+
|
|
169
|
+
test("UserCard has no a11y violations", async () => {
|
|
170
|
+
const { container } = render(<UserCard user={mockUser} />);
|
|
171
|
+
expect(await axe(container)).toHaveNoViolations();
|
|
172
|
+
});
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
Run axe assertions in component tests — catches missing labels, ARIA misuse, color contrast (limited).
|
|
176
|
+
|
|
177
|
+
## When to Reach for Playwright / Cypress
|
|
178
|
+
|
|
179
|
+
Component test with RTL + JSDOM cannot:
|
|
180
|
+
|
|
181
|
+
- Test real layout (flexbox, grid, viewport-dependent rendering)
|
|
182
|
+
- Test scrolling, drag-and-drop, paste from clipboard
|
|
183
|
+
- Test browser-native animation, CSS transitions
|
|
184
|
+
- Test cross-frame interactions (iframes, popups)
|
|
185
|
+
|
|
186
|
+
For those, use Playwright Component Testing or end-to-end Playwright/Cypress runs. See the e2e-testing skill when it is installed.
|
|
187
|
+
|
|
188
|
+
## Coverage Targets
|
|
189
|
+
|
|
190
|
+
| Layer | Target |
|
|
191
|
+
|---|---|
|
|
192
|
+
| Pure utility functions | ≥90% |
|
|
193
|
+
| Custom hooks | ≥85% |
|
|
194
|
+
| Components (presentational) | ≥80% — behavior, not lines |
|
|
195
|
+
| Container components | ≥70% — golden paths + error states |
|
|
196
|
+
| Pages (E2E covered separately) | Smoke test per route minimum |
|
|
197
|
+
|
|
198
|
+
## Anti-Patterns
|
|
199
|
+
|
|
200
|
+
- Asserting on `container.querySelector` — bypasses accessibility queries
|
|
201
|
+
- Asserting on number of renders — implementation detail
|
|
202
|
+
- Mocking React hooks (`jest.mock("react", ...)`) — refactor the component instead
|
|
203
|
+
- Mocking child components by default — tests the integration, not the parent in isolation
|
|
204
|
+
- Manual `act()` warnings ignored — they indicate real bugs
|
|
205
|
+
|
|
206
|
+
## Skill Reference
|
|
207
|
+
|
|
208
|
+
See `skills/react-testing/SKILL.md` for end-to-end test examples, MSW patterns, and accessibility test scaffolding.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Affaan Mustafa
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Attribution
|
|
2
|
+
|
|
3
|
+
This skill is copied from Affaan Mustafa's public `affaan-m/ECC` repository.
|
|
4
|
+
|
|
5
|
+
- Source: https://github.com/affaan-m/ECC/tree/main/skills/react-testing
|
|
6
|
+
- Imported from commit: `0f84c0e2796703fbda87d577b2636351418c7442`
|
|
7
|
+
- Upstream skill name: `react-testing`
|
|
8
|
+
- License: MIT
|
|
9
|
+
- Copyright: Copyright (c) 2026 Affaan Mustafa
|
|
10
|
+
|
|
11
|
+
The upstream MIT license text is preserved in `LICENSE`.
|
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: react-testing
|
|
3
|
+
description: React component testing with React Testing Library, Vitest/Jest, MSW for network mocking, accessibility assertions with axe, and the decision boundary between component tests and Playwright/Cypress end-to-end runs. Use when writing or fixing tests for React components, hooks, or pages.
|
|
4
|
+
origin: ECC
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# React Testing
|
|
8
|
+
|
|
9
|
+
Comprehensive React testing patterns for behavior-focused component tests, custom hook tests, accessibility assertions, and network-level mocking.
|
|
10
|
+
|
|
11
|
+
## When to Activate
|
|
12
|
+
|
|
13
|
+
- Writing tests for React components, custom hooks, or pages
|
|
14
|
+
- Adding test coverage to legacy untested components
|
|
15
|
+
- Migrating from Enzyme or class-component-era patterns to React Testing Library
|
|
16
|
+
- Setting up Vitest or Jest for a new React project
|
|
17
|
+
- Mocking HTTP requests in tests
|
|
18
|
+
- Asserting accessibility violations
|
|
19
|
+
- Deciding which tests belong in RTL vs Playwright Component Testing vs full E2E
|
|
20
|
+
|
|
21
|
+
## Core Principle
|
|
22
|
+
|
|
23
|
+
Test what the user sees and does, not implementation details.
|
|
24
|
+
|
|
25
|
+
A test should:
|
|
26
|
+
|
|
27
|
+
- Render the component with the same providers it has in production
|
|
28
|
+
- Interact with it via accessible queries (role, label) and `userEvent`
|
|
29
|
+
- Assert visible output and observable side effects (callback fired, request sent)
|
|
30
|
+
|
|
31
|
+
A test should NOT:
|
|
32
|
+
|
|
33
|
+
- Inspect component state, props passed to children, or which hooks were called
|
|
34
|
+
- Mock React itself or framework hooks
|
|
35
|
+
- Assert on the number of renders or DOM structure beyond what affects users
|
|
36
|
+
|
|
37
|
+
## Library Choice
|
|
38
|
+
|
|
39
|
+
| Runner | When | Note |
|
|
40
|
+
|---|---|---|
|
|
41
|
+
| **Vitest** | Vite, Remix, modern setups | Faster, native ESM, Jest-compatible API |
|
|
42
|
+
| **Jest** | Next.js, CRA, established repos | Default for many React projects |
|
|
43
|
+
| **Playwright Component Testing** | Real browser engine needed | Use when JSDOM lacks the required feature |
|
|
44
|
+
| **Cypress Component Testing** | Real browser, Cypress already in use | Alternative to Playwright CT |
|
|
45
|
+
|
|
46
|
+
Pick one. Do not run RTL + Vitest AND Playwright CT in the same repo unless you have a clear lane separation.
|
|
47
|
+
|
|
48
|
+
## Query Priority
|
|
49
|
+
|
|
50
|
+
React Testing Library exposes queries in three tiers — use top-down:
|
|
51
|
+
|
|
52
|
+
1. **Accessible to everyone**: `getByRole`, `getByLabelText`, `getByPlaceholderText`, `getByText`, `getByDisplayValue`
|
|
53
|
+
2. **Semantic**: `getByAltText`, `getByTitle`
|
|
54
|
+
3. **Test IDs (escape hatch)**: `getByTestId`
|
|
55
|
+
|
|
56
|
+
```tsx
|
|
57
|
+
// Best
|
|
58
|
+
screen.getByRole("button", { name: /save/i });
|
|
59
|
+
|
|
60
|
+
// OK for inputs
|
|
61
|
+
screen.getByLabelText("Email");
|
|
62
|
+
|
|
63
|
+
// Last resort
|
|
64
|
+
screen.getByTestId("save-btn");
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Variants:
|
|
68
|
+
|
|
69
|
+
- `getBy*` — throws if no match
|
|
70
|
+
- `queryBy*` — returns `null` (use for "assert absence")
|
|
71
|
+
- `findBy*` — async, returns a Promise (use for elements that appear after async work)
|
|
72
|
+
|
|
73
|
+
## User Interaction with `userEvent`
|
|
74
|
+
|
|
75
|
+
```tsx
|
|
76
|
+
import userEvent from "@testing-library/user-event";
|
|
77
|
+
|
|
78
|
+
test("submits the form", async () => {
|
|
79
|
+
const user = userEvent.setup();
|
|
80
|
+
const onSubmit = vi.fn();
|
|
81
|
+
render(<UserForm onSubmit={onSubmit} />);
|
|
82
|
+
|
|
83
|
+
await user.type(screen.getByLabelText("Email"), "user@example.com");
|
|
84
|
+
await user.click(screen.getByRole("button", { name: /save/i }));
|
|
85
|
+
|
|
86
|
+
expect(onSubmit).toHaveBeenCalledWith({ email: "user@example.com" });
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
- Always `await` userEvent calls
|
|
91
|
+
- Call `userEvent.setup()` once per test, reuse the returned `user`
|
|
92
|
+
- `userEvent` simulates a real browser sequence; `fireEvent` dispatches a single synthetic event — prefer `userEvent`
|
|
93
|
+
|
|
94
|
+
## Async Patterns
|
|
95
|
+
|
|
96
|
+
```tsx
|
|
97
|
+
// Element that appears after async work
|
|
98
|
+
expect(await screen.findByText("Loaded")).toBeInTheDocument();
|
|
99
|
+
|
|
100
|
+
// Side effect assertion
|
|
101
|
+
await waitFor(() => expect(saveSpy).toHaveBeenCalled());
|
|
102
|
+
|
|
103
|
+
// Element that should disappear
|
|
104
|
+
await waitForElementToBeRemoved(() => screen.queryByText("Loading"));
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Never `setTimeout` + assertion — flaky. Use the matchers above.
|
|
108
|
+
|
|
109
|
+
## Network Mocking with MSW
|
|
110
|
+
|
|
111
|
+
Mock Service Worker mocks at the network layer. The component, hooks, and fetch library all behave exactly as in production.
|
|
112
|
+
|
|
113
|
+
### Setup
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
// test/setup.ts
|
|
117
|
+
import { setupServer } from "msw/node";
|
|
118
|
+
import { http, HttpResponse } from "msw";
|
|
119
|
+
|
|
120
|
+
export const handlers = [
|
|
121
|
+
http.get("/api/users/:id", ({ params }) =>
|
|
122
|
+
HttpResponse.json({ id: params.id, name: "Alice" }),
|
|
123
|
+
),
|
|
124
|
+
http.post("/api/users", async ({ request }) => {
|
|
125
|
+
const body = await request.json();
|
|
126
|
+
return HttpResponse.json({ id: "new-id", ...body }, { status: 201 });
|
|
127
|
+
}),
|
|
128
|
+
];
|
|
129
|
+
|
|
130
|
+
export const server = setupServer(...handlers);
|
|
131
|
+
|
|
132
|
+
beforeAll(() => server.listen({ onUnhandledRequest: "error" }));
|
|
133
|
+
afterEach(() => server.resetHandlers());
|
|
134
|
+
afterAll(() => server.close());
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Configure `onUnhandledRequest: "error"` so any unmocked request fails the test loudly — silent passes are worse than red.
|
|
138
|
+
|
|
139
|
+
### Per-test override
|
|
140
|
+
|
|
141
|
+
```tsx
|
|
142
|
+
test("renders error on 500", async () => {
|
|
143
|
+
server.use(
|
|
144
|
+
http.get("/api/users/:id", () => new HttpResponse(null, { status: 500 })),
|
|
145
|
+
);
|
|
146
|
+
render(<UserPage id="1" />);
|
|
147
|
+
expect(await screen.findByText(/something went wrong/i)).toBeInTheDocument();
|
|
148
|
+
});
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## Provider Wrapping
|
|
152
|
+
|
|
153
|
+
Wrap providers once in a `test-utils.tsx`:
|
|
154
|
+
|
|
155
|
+
```tsx
|
|
156
|
+
// test-utils.tsx
|
|
157
|
+
import { render, RenderOptions } from "@testing-library/react";
|
|
158
|
+
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
159
|
+
|
|
160
|
+
export function renderWithProviders(
|
|
161
|
+
ui: React.ReactElement,
|
|
162
|
+
options?: RenderOptions,
|
|
163
|
+
) {
|
|
164
|
+
const queryClient = new QueryClient({
|
|
165
|
+
defaultOptions: { queries: { retry: false } },
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
return render(
|
|
169
|
+
<QueryClientProvider client={queryClient}>
|
|
170
|
+
<ThemeProvider theme={lightTheme}>
|
|
171
|
+
<MemoryRouter>{ui}</MemoryRouter>
|
|
172
|
+
</ThemeProvider>
|
|
173
|
+
</QueryClientProvider>,
|
|
174
|
+
options,
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export * from "@testing-library/react";
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
Then `import { renderWithProviders, screen } from "test-utils"` in every test file.
|
|
182
|
+
|
|
183
|
+
## Custom Hook Testing
|
|
184
|
+
|
|
185
|
+
```tsx
|
|
186
|
+
import { renderHook, act } from "@testing-library/react";
|
|
187
|
+
|
|
188
|
+
test("useCounter increments and decrements", () => {
|
|
189
|
+
const { result } = renderHook(() => useCounter(0));
|
|
190
|
+
|
|
191
|
+
expect(result.current.count).toBe(0);
|
|
192
|
+
|
|
193
|
+
act(() => result.current.increment());
|
|
194
|
+
expect(result.current.count).toBe(1);
|
|
195
|
+
|
|
196
|
+
act(() => result.current.decrement());
|
|
197
|
+
expect(result.current.count).toBe(0);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
test("useCounter accepts initial value", () => {
|
|
201
|
+
const { result } = renderHook(() => useCounter(10));
|
|
202
|
+
expect(result.current.count).toBe(10);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
test("useUser fetches user data", async () => {
|
|
206
|
+
// Instantiate QueryClient ONCE per test outside the wrapper so it survives re-renders.
|
|
207
|
+
// Creating it inside the wrapper closure resets cache state on every render, producing flaky tests.
|
|
208
|
+
const queryClient = new QueryClient({
|
|
209
|
+
defaultOptions: { queries: { retry: false } },
|
|
210
|
+
});
|
|
211
|
+
const wrapper = ({ children }: { children: React.ReactNode }) => (
|
|
212
|
+
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
|
213
|
+
);
|
|
214
|
+
|
|
215
|
+
const { result } = renderHook(() => useUser("1"), { wrapper });
|
|
216
|
+
|
|
217
|
+
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
|
218
|
+
expect(result.current.data).toEqual({ id: "1", name: "Alice" });
|
|
219
|
+
});
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
- Wrap state-changing calls in `act`
|
|
223
|
+
- Test through the hook's public API only
|
|
224
|
+
- For hooks that use context, pass a `wrapper`
|
|
225
|
+
|
|
226
|
+
## Accessibility Assertions
|
|
227
|
+
|
|
228
|
+
```tsx
|
|
229
|
+
import { axe, toHaveNoViolations } from "jest-axe"; // or vitest-axe
|
|
230
|
+
expect.extend(toHaveNoViolations);
|
|
231
|
+
|
|
232
|
+
test("UserCard has no a11y violations", async () => {
|
|
233
|
+
const { container } = render(<UserCard user={mockUser} />);
|
|
234
|
+
expect(await axe(container)).toHaveNoViolations();
|
|
235
|
+
});
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
Run axe in component tests for every interactive component. Catches:
|
|
239
|
+
|
|
240
|
+
- Missing labels on form inputs
|
|
241
|
+
- Invalid ARIA usage
|
|
242
|
+
- Poor color contrast (limited — JSDOM has no real CSS engine, so this works for inline styles only; visual contrast belongs in Playwright)
|
|
243
|
+
- Missing alt text on images
|
|
244
|
+
- Heading order violations
|
|
245
|
+
|
|
246
|
+
Cross-link: skills/accessibility for the broader a11y testing playbook when that optional skill is installed.
|
|
247
|
+
|
|
248
|
+
## When NOT to Use Snapshot Tests
|
|
249
|
+
|
|
250
|
+
Snapshots of rendered output:
|
|
251
|
+
|
|
252
|
+
- Break on every styling change
|
|
253
|
+
- Get rubber-stamped during review
|
|
254
|
+
- Test implementation detail (DOM structure), not behavior
|
|
255
|
+
|
|
256
|
+
Acceptable snapshot uses:
|
|
257
|
+
|
|
258
|
+
- Pure data serialization functions (`formatInvoice(invoice)` -> stable string)
|
|
259
|
+
- Generated config files (e.g., webpack config output)
|
|
260
|
+
|
|
261
|
+
For visual regression on components, use Playwright/Cypress screenshots or Percy/Chromatic — actual visual diffs, not DOM strings.
|
|
262
|
+
|
|
263
|
+
## When to Reach for Playwright / Cypress
|
|
264
|
+
|
|
265
|
+
JSDOM (used by Vitest/Jest) cannot:
|
|
266
|
+
|
|
267
|
+
- Render real layout (flexbox, grid, viewport queries)
|
|
268
|
+
- Run native browser animation, CSS transitions
|
|
269
|
+
- Test scrolling behavior, drag-and-drop, paste from clipboard
|
|
270
|
+
- Handle iframes, popups, downloads, cross-origin flows
|
|
271
|
+
- Run real network in a controlled environment with full DevTools support
|
|
272
|
+
|
|
273
|
+
For any of those, use Playwright Component Testing (component test in real browser) or full E2E. See the e2e-testing skill when it is installed.
|
|
274
|
+
|
|
275
|
+
Decision boundary:
|
|
276
|
+
|
|
277
|
+
- A hook, a presentational component, a form with logic -> RTL
|
|
278
|
+
- A component whose layout matters or that uses browser APIs not in JSDOM -> Playwright CT
|
|
279
|
+
- A full user flow across multiple pages -> Playwright/Cypress E2E
|
|
280
|
+
|
|
281
|
+
## Coverage Targets
|
|
282
|
+
|
|
283
|
+
| Layer | Target |
|
|
284
|
+
|---|---|
|
|
285
|
+
| Pure utilities | >=90% |
|
|
286
|
+
| Custom hooks | >=85% |
|
|
287
|
+
| Presentational components | >=80% — behavior, not lines |
|
|
288
|
+
| Container components | >=70% — golden paths + error states |
|
|
289
|
+
| Pages | E2E covered separately; smoke test minimum |
|
|
290
|
+
|
|
291
|
+
Configure via `vitest.config.ts` / `jest.config.js`:
|
|
292
|
+
|
|
293
|
+
```ts
|
|
294
|
+
// vitest.config.ts
|
|
295
|
+
test: {
|
|
296
|
+
coverage: {
|
|
297
|
+
provider: "v8",
|
|
298
|
+
reporter: ["text", "html", "lcov"],
|
|
299
|
+
thresholds: {
|
|
300
|
+
lines: 80,
|
|
301
|
+
functions: 80,
|
|
302
|
+
branches: 70,
|
|
303
|
+
statements: 80,
|
|
304
|
+
},
|
|
305
|
+
},
|
|
306
|
+
}
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
## Anti-Patterns
|
|
310
|
+
|
|
311
|
+
- `container.querySelector("...")` — bypasses accessibility queries, lets tests pass when real users would fail
|
|
312
|
+
- Asserting on number of renders — implementation detail
|
|
313
|
+
- `jest.mock("react", ...)` — never mock React. Refactor the component instead
|
|
314
|
+
- Mocking child components by default — tests the integration, not isolation. Mock only when the child has heavy side effects
|
|
315
|
+
- Ignoring `act()` warnings — they signal real bugs (state update after unmount, missing async wrapping)
|
|
316
|
+
- Sharing mutable state across tests — flakes when test order changes
|
|
317
|
+
- Tests that pass with `it.skip()` removed — your test does not actually assert what you think
|
|
318
|
+
|
|
319
|
+
## TDD Workflow
|
|
320
|
+
|
|
321
|
+
```
|
|
322
|
+
RED -> Write failing test for the next requirement
|
|
323
|
+
GREEN -> Write minimal component code to pass
|
|
324
|
+
REFACTOR -> Improve the component, tests stay green
|
|
325
|
+
REPEAT -> Next requirement
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
For new components:
|
|
329
|
+
|
|
330
|
+
1. Define the component's prop type and signature
|
|
331
|
+
2. Write the first test for the simplest case
|
|
332
|
+
3. Verify it fails for the right reason
|
|
333
|
+
4. Implement just enough to pass
|
|
334
|
+
5. Add the next test case
|
|
335
|
+
6. Refactor when the third similar test reveals a pattern
|
|
336
|
+
|
|
337
|
+
## Test Commands
|
|
338
|
+
|
|
339
|
+
```bash
|
|
340
|
+
# Vitest
|
|
341
|
+
vitest # watch
|
|
342
|
+
vitest run # one-shot
|
|
343
|
+
vitest run --coverage # with coverage
|
|
344
|
+
vitest run path/to/file.test.tsx # single file
|
|
345
|
+
|
|
346
|
+
# Jest
|
|
347
|
+
jest --watch
|
|
348
|
+
jest --coverage
|
|
349
|
+
jest path/to/file.test.tsx
|
|
350
|
+
|
|
351
|
+
# CI mode
|
|
352
|
+
CI=true vitest run --coverage
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
## Related
|
|
356
|
+
|
|
357
|
+
- Rules: [rules/react/testing.md](rules/react/testing.md)
|
|
358
|
+
- Skills: [react-patterns](../react-patterns/SKILL.md), accessibility, e2e-testing, tdd-workflow
|
|
359
|
+
- Agents: `react-reviewer` (reviews test quality during code review), `tdd-guide` (enforces TDD process)
|
|
360
|
+
- Commands: `/react-test`, `/react-review`
|
|
361
|
+
|
|
362
|
+
## Examples
|
|
363
|
+
|
|
364
|
+
### Form submission with MSW and userEvent
|
|
365
|
+
|
|
366
|
+
```tsx
|
|
367
|
+
test("submits user form and shows success", async () => {
|
|
368
|
+
server.use(
|
|
369
|
+
http.post("/api/users", () =>
|
|
370
|
+
HttpResponse.json({ id: "1", name: "Alice" }, { status: 201 }),
|
|
371
|
+
),
|
|
372
|
+
);
|
|
373
|
+
|
|
374
|
+
const user = userEvent.setup();
|
|
375
|
+
renderWithProviders(<UserForm />);
|
|
376
|
+
|
|
377
|
+
await user.type(screen.getByLabelText("Name"), "Alice");
|
|
378
|
+
await user.type(screen.getByLabelText("Email"), "alice@example.com");
|
|
379
|
+
await user.click(screen.getByRole("button", { name: /save/i }));
|
|
380
|
+
|
|
381
|
+
expect(await screen.findByText(/saved successfully/i)).toBeInTheDocument();
|
|
382
|
+
});
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
### Testing an error boundary
|
|
386
|
+
|
|
387
|
+
```tsx
|
|
388
|
+
function Broken() {
|
|
389
|
+
throw new Error("boom");
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
test("error boundary renders fallback", () => {
|
|
393
|
+
// Suppress React's console.error noise for the expected throw, then restore so
|
|
394
|
+
// the spy does not leak across tests and hide real errors elsewhere.
|
|
395
|
+
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
396
|
+
try {
|
|
397
|
+
render(
|
|
398
|
+
<ErrorBoundary fallback={<div>Something went wrong</div>}>
|
|
399
|
+
<Broken />
|
|
400
|
+
</ErrorBoundary>,
|
|
401
|
+
);
|
|
402
|
+
|
|
403
|
+
expect(screen.getByText("Something went wrong")).toBeInTheDocument();
|
|
404
|
+
} finally {
|
|
405
|
+
errorSpy.mockRestore();
|
|
406
|
+
}
|
|
407
|
+
});
|
|
408
|
+
```
|
|
409
|
+
|
|
410
|
+
### Testing a Suspense boundary
|
|
411
|
+
|
|
412
|
+
```tsx
|
|
413
|
+
test("shows loading then content", async () => {
|
|
414
|
+
renderWithProviders(
|
|
415
|
+
<Suspense fallback={<div>Loading...</div>}>
|
|
416
|
+
<UserDetail id="1" />
|
|
417
|
+
</Suspense>,
|
|
418
|
+
);
|
|
419
|
+
|
|
420
|
+
expect(screen.getByText("Loading...")).toBeInTheDocument();
|
|
421
|
+
expect(await screen.findByText("Alice")).toBeInTheDocument();
|
|
422
|
+
});
|
|
423
|
+
```
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Affaan Mustafa
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Third-party notice
|
|
2
|
+
|
|
3
|
+
This directory contains React rule references copied from Affaan Mustafa's ECC
|
|
4
|
+
repository.
|
|
5
|
+
|
|
6
|
+
- Source: https://github.com/affaan-m/ECC/tree/main/rules/react
|
|
7
|
+
- Source commit: 0f84c0e2796703fbda87d577b2636351418c7442
|
|
8
|
+
- License: MIT
|
|
9
|
+
- Copyright: Copyright (c) 2026 Affaan Mustafa
|
|
10
|
+
|
|
11
|
+
The upstream MIT license is included in `LICENSE`.
|