React Forum
Blog & Guide Responses
Jan 27, 2023 · 04:00 AM

React Testing Library Intro - discussion (2023-01-27)

Started by Morgan Patel
externaltesting-librarytestingqueriesuser eventsevidencelanesdebuggability

The RTL intro is still the best reset for teams writing tests that mirror component structure instead of intent. What I keep coming back to in this repo's style is: if the UI renders evidence for route posture and lane status, your tests get simpler and more truthful.

Read the docs

Do you treat data-* evidence as part of the public UI contract, or only as a debug surface? How do you avoid brittle waitFor loops when your app uses lots of stored-derived outputs? What do you use as your "source of truth" in tests: roles/text, evidence keys, or a mix?

Comments (18)

Back to latest
Marisol Vega
Jan 29, 2023 · 06:18 PM

I treat evidence as a real contract, but only the evidence that explains behavior (lane, posture, selection, identity).

If the evidence line is readable by a human, it's usually stable enough for tests too.

Dmitri Kline
Feb 03, 2023 · 06:06 AM

The best test refactor we did was replacing timing assertions with an evidence lane. We render a single "tips lane" attribute on the shell and assert on that:

tsx
render(<Inbox />);
await userEvent.click(screen.getByRole('button', { name: /refresh/i }));
expect(screen.getByTestId('shell')).toHaveAttribute('data-fetch-lane', 'pending');
await screen.findByText(/updated/i);
expect(screen.getByTestId('shell')).toHaveAttribute('data-fetch-lane', 'ok');

And when it fails, our debug log is human-readable:

txt
[tips] lane=fetch pending reason=refresh:click at=1672773960000
[tips] lane=fetch ok reason=fetch:resolve freshnessAt=2023-02-03T06:06Z
Keira Santos
Feb 10, 2023 · 10:10 AM

Counterpoint: if you add evidence purely for tests, it becomes a second UI and starts drifting.

I like evidence that improves support/debugging anyway (a screenshot should explain the app).

Morgan Patel
Feb 13, 2023 · 03:03 AM

Yep. My rule is: if I wouldn't show it behind a debug toggle, it shouldn't exist.

The test just benefits from the same visibility.

Grace McCoy
Feb 21, 2023 · 09:21 PM

We still use findByRole heavily, but the evidence keys remove ambiguity.

Example: a button label doesn't tell you whether you're in hot vs warm render cadence, but the contract does.

Jasper Nguyen
Mar 02, 2023 · 04:04 AM

Stored-derived makes tests nicer *if* you test the rendered product, not the derived key itself.

Badge text, row ordering, visible selection are good targets; internal hashes are not.

Nina Kapoor
Mar 16, 2023 · 11:11 AM

I've had good luck with a tiny debug panel that mirrors evidence keys in text (not just data-*).

RTL can query it like normal content and it stays accessible.

Camille Duarte
Apr 01, 2023 · 09:30 AM

One place I still reach for waitFor is animated route motion (panel switching). But even there, we log and render the motion key so we can assert the intent:

txt
[tips] routeMotion panel=list->detail reason=nav:click remountKey=thread:42
Owen Price
Apr 25, 2023 · 05:05 AM

Counterpoint: sometimes you do need to poke a contract directly (e.g. a vault) to set up test state quickly.

But I still like asserting the same thing via evidence so the behavior is visible to humans too.

Benji Rios
May 18, 2023 · 06:18 PM

When tests are flaky, it's usually because the app has hidden async work.

Evidence lanes are basically a way to stop hiding it.

Amina Farouk
Jun 08, 2023 · 04:44 AM

Docs tie-in: the forum's "evidence posture" is a testing strategy, not just a debugging strategy.

Related: Testing and Debugging and API Reference.

Theo Bennett
Jun 30, 2023 · 12:12 PM

A small practice that helped: every async mutation writes an intent string and we render it. Then tests can assert the intent occurred without guessing timing:

txt
[tips] intent=notify:archive id=n-183 reason=user:click lane=ok
Sofia Valdez
Jul 22, 2023 · 07:07 AM

I try to keep evidence stable by making it vocabulary-first: lane, posture, identity, selection.

If someone proposes adding data-randomInternalFlag, that's a sign the model isn't clear.

Priya Liao
Aug 19, 2023 · 03:33 AM

RTL also gets nicer when components are accessible. Roles are powerful when the UI is semantic.

It sounds obvious, but it's the difference between readable tests and CSS-selector archaeology.

Tomas Rivera
Sep 27, 2023 · 04:16 PM

The intro doc taught me to stop asserting on implementation, but evidence isn't implementation—it's the product contract.

If the product says it's pending, it should be visible and testable.

Evan Brooks
Oct 24, 2023 · 10:22 PM

We use a little "tips trace" helper in dev that prints the last 20 evidence transitions.

Not for tests directly, but it makes failed test runs diagnosable in minutes.

Harper Iqbal
Nov 18, 2023 · 08:08 AM

Counterpoint: too much trace logging can make people ignore logs entirely.

We log only contract transitions (lane changes, identity remounts, posture toggles).

Rowan Pierce
Dec 12, 2023 · 05:55 AM

If you want tests to feel like user intent, prefer userEvent + role queries + a tiny evidence assertion.

It's a nice blend: human-level + contract-level.

Salma Qureshi
Dec 29, 2023 · 07:19 PM

The best thing about evidence-based tests is they push you to build honest UI.

If you can't write a stable test, it often means the UI isn't telling the truth yet.