Sub topic 13
Where tests live and what they cover
This chapter assumes you can write React components and hooks, and that you have used a test runner such as Vitest or Jest with React Testing Library. It builds on the feature-module layout from earlier chapters.
Colocate tests with the code they test
Put a test file next to the file it tests. CartList.tsx and CartList.test.tsx sit in the same folder. When you move, rename, or delete a component, its test goes with it. A separate top-level tests/ folder that mirrors src/ drifts out of sync, and people stop looking there.
The feature layout then looks like this:
src/features/cart/
components/
CartList.tsx
CartList.test.tsx
hooks/
useCart.ts
useCart.test.ts
api.ts
types.ts
index.ts
src/test/
setup.ts
renderWithProviders.tsx
e2e/
checkout.spec.tsTest helpers shared by many features, such as a render function that adds providers, go in src/test/. End-to-end specs go in a top-level e2e/ folder because they cover the whole app, not one feature.
Choose what each level covers
Each level of test answers a different question. Pick the lowest level that can answer the question you have.
| Level | Location | What it checks |
|---|---|---|
| Unit | Next to the file | One function or hook with plain inputs and outputs |
| Component | Next to the component | What the user sees and can do with one component |
| Feature | Inside the feature folder | Several components, hooks, and a mocked `api.ts` working together |
| End to end | `e2e/` | Real browser, real routes, the whole app |
Test through the public API
Test a component through what the user sees, not through its internal state. Test a feature from the outside by importing from its index.ts. Inside the feature, a colocated test may import its sibling file directly. A test in another feature or in e2e/ should not import internals, for the same reason application code should not.
Here is a component and its colocated test:
// src/features/cart/components/CartList.tsx
import type { CartItem } from "../types";
export function CartList({ items }: { items: CartItem[] }) {
if (items.length === 0) return <p>Your cart is empty.</p>;
return (
<ul>
{items.map((item) => (
<li key={item.id}>{`${item.name} x ${item.quantity}`}</li>
))}
</ul>
);
}// src/features/cart/components/CartList.test.tsx
import { render, screen } from "@testing-library/react";
import { describe, expect, it } from "vitest";
import { CartList } from "./CartList";
describe("CartList", () => {
it("shows a message when the cart is empty", () => {
render(<CartList items={[]} />);
expect(screen.getByText("Your cart is empty.")).toBeTruthy();
});
it("lists each item with its quantity", () => {
render(<CartList items={[{ id: "1", name: "Mug", quantity: 2 }]} />);
expect(screen.getByText("Mug x 2")).toBeTruthy();
});
});The test needs a DOM environment such as jsdom. Set it in the Vitest config, and register shared setup in src/test/setup.ts with setupFiles.
Mock at the data boundary
Components call hooks, and hooks call api.ts. Mock api.ts or the network layer, not the hook. That way the hook, the query cache, and the component all run for real. Mocking at the lowest layer gives the most realistic test with the least setup.
Common errors
- Testing implementation details. Tests that check state values or call counts break on every refactor. Query by role or text instead.
- Duplicated provider setup. If every test wraps its own providers, move the wrapper into
src/test/renderWithProviders.tsx. - Cross-feature imports in tests. A test that reaches into another feature's internals hides a boundary violation. The ESLint rule from the previous chapter catches it if the rule's
filespattern covers test files. - Too many end-to-end tests. They are slow and flaky. Keep them for critical paths such as checkout and sign-in.
Exercise
A test in features/orders imports @/features/cart/hooks/useCart to set up cart data. What is wrong, and what is the fix?
Show answer
The test reaches past the cart feature's public API into its internals, so a cart refactor can break the orders tests. Import useCart from @/features/cart, or better, mock the cart's api.ts or the network so the orders test does not depend on cart internals.