Skip to content

Commit 8a1a12a

Browse files
committed
test: prefer isVisible in tests
1 parent ff56447 commit 8a1a12a

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

CLAUDE.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ pnpm generate-client:nofetch # Regenerate without fetching
202202
- Loading states and skeleton screens
203203
- Accessibility (keyboard navigation, screen readers)
204204

205+
### Testing Best Practices
206+
207+
- **Prefer `toBeVisible()` over `toBeInTheDocument()`** - `toBeVisible()` checks that an element is actually visible to the user (not hidden via CSS, `aria-hidden`, etc.), while `toBeInTheDocument()` only checks DOM presence. Use `toBeVisible()` for positive assertions and `.not.toBeInTheDocument()` for absence checks.
208+
205209
### Mocking & Testing
206210

207211
- **MSW Auto-Mocker**
@@ -230,13 +234,13 @@ describe("ServerList", () => {
230234
render(<ServerList />);
231235

232236
await waitFor(() => {
233-
expect(screen.getByText("Server 1")).toBeInTheDocument();
237+
expect(screen.getByText("Server 1")).toBeVisible();
234238
});
235239

236240
const copyButton = screen.getByRole("button", { name: /copy url/i });
237241
await userEvent.click(copyButton);
238242

239-
expect(screen.getByText(/copied/i)).toBeInTheDocument();
243+
expect(screen.getByText(/copied/i)).toBeVisible();
240244
});
241245
});
242246
```

src/components/error-page/error-page.test.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ describe("ErrorPage", () => {
66
it("displays the title", () => {
77
render(<ErrorPage title="Test Error">Error description</ErrorPage>);
88

9-
expect(
10-
screen.getByRole("heading", { name: /test error/i }),
11-
).toBeInTheDocument();
9+
expect(screen.getByRole("heading", { name: /test error/i })).toBeVisible();
1210
});
1311

1412
it("displays children as description", () => {
1513
render(<ErrorPage title="Error">This is the error message</ErrorPage>);
1614

17-
expect(screen.getByText(/this is the error message/i)).toBeInTheDocument();
15+
expect(screen.getByText(/this is the error message/i)).toBeVisible();
1816
});
1917

2018
it("displays action buttons when provided", () => {
@@ -27,9 +25,7 @@ describe("ErrorPage", () => {
2725
</ErrorPage>,
2826
);
2927

30-
expect(
31-
screen.getByRole("button", { name: /click me/i }),
32-
).toBeInTheDocument();
28+
expect(screen.getByRole("button", { name: /click me/i })).toBeVisible();
3329
});
3430

3531
it("displays a decorative illustration", () => {
@@ -38,6 +34,6 @@ describe("ErrorPage", () => {
3834
);
3935

4036
const svg = container.querySelector("svg[aria-hidden='true']");
41-
expect(svg).toBeInTheDocument();
37+
expect(svg).toBeVisible();
4238
});
4339
});

0 commit comments

Comments
 (0)