Good tests verify behaviour, not implementation, so they survive refactors and fail only when something users or callers care about breaks. They're deterministic (no flakiness), independent, fast, and readable: the name states the expectation, the body shows Arrange/Act/Assert with explicit data. Put most effort where it buys the most confidence (integration tests for UI, unit tests for tricky logic), mock only at boundaries, and treat test code with the same care as production code.
// brittle: tied to internals
expect(wrapper.state('isOpen')).toBe(true);
expect(container.querySelector('.dropdown__menu--visible')).toBeTruthy();
// resilient: what the user experiences
await user.click(screen.getByRole('button', { name: /filters/i }));
expect(screen.getByRole('menu')).toBeTruthy();
Query by role, label and text (Testing Library's priority order). This also nudges you toward accessible markup. Use data-testid only when there's no accessible handle.
setSystemTime), randomness (seeded or stubbed), locale/time zone (set TZ in CI), and network (MSW).afterEach (or config: restoreMocks: true).describe('formatDuration', () => {
it('shows minutes and seconds under an hour', () => {
expect(formatDuration(125)).toBe('2:05');
});
it('adds hours when needed', () => {
expect(formatDuration(3725)).toBe('1:02:05');
});
});
buildUser({ role: 'admin' })) with sensible defaults keeps tests short while showing what matters.it.each for tables of cases.expects are fine if they describe one behaviour; split tests that verify unrelated things.toEqual with the exact object beats toBeDefined().Mock network, clock, browser APIs and third-party SDKs. Use real implementations of your own modules wherever practical. Over-mocked tests pass while the app is broken and break when it isn't.
Watch each new test fail at least once (write it first, or temporarily break the code). Mutation testing (Stryker) reveals tests that execute code without really checking it.
render with providers, MSW handlers, data builders) live in one place.act, console errors) instead of silencing them; fail tests on unexpected console.error.Spotted a mistake or a rights concern? Report an issue