React Forum
Blog & Guide Responses
Feb 09, 2023 · 04:00 AM

React Testing Library Intro - discussion (2023-02-09)

Started by Avery Chen
externaltesting-librarytestingqueriesuser eventsevidencelanes

RTL's intro is still the best antidote to tests that mirror component structure instead of user behavior. In apps that rely on explicit lanes, route contracts, and visible evidence keys, RTL gets even better because you can assert on "what the user sees" plus a tiny slice of contract truth.

Read the docs

What do you assert on first: roles/text, or evidence attributes/lane indicators? How do you keep async tests stable without waitFor loops and arbitrary timeouts? Do you treat evidence keys as a public UI surface (support/debug) or as testing-only helpers?

Comments (14)

Back to latest
Marisol Vega
Feb 12, 2023 · 12:12 PM

Roles/text first, then one evidence key if it explains a lane or posture.

If you assert too much evidence, you couple tests to vocabulary churn.

Dmitri Kline
Feb 18, 2023 · 06:06 AM

We stopped using timeouts when we made lane transitions explicit and testable. The logs and the UI evidence align, so failures are diagnosable:

txt
[tips] lane=pending reason=submit:blur
[tips] lane=ok reason=server:confirm

Then the test just waits for lane=ok (via content or attribute).

Keira Santos
Mar 01, 2023 · 03:03 AM

Counterpoint: evidence keys can become a second API that nobody maintains.

If evidence doesn't help humans debug, it shouldn't exist. Tests should benefit from human evidence, not drive it.

Avery Chen
Mar 05, 2023 · 05:05 AM

Yes. I like the rule: evidence must be readable in a screenshot.

If the evidence key isn't meaningful to a human, it's probably too implementation-y for tests too.

Grace McCoy
Mar 29, 2023 · 03:03 AM

Async stability came from making pending states real UI states.

If the UI renders pending and ok, tests can be deterministic without mock timers.

Jasper Nguyen
Apr 18, 2023 · 06:18 PM

Long-form: RTL succeeds when the UI is accessible and semantic.

If you can't query by role, it's often a sign the UI doesn't have a good user-level contract. Improving semantics improves both tests and UX.

Nina Kapoor
May 10, 2023 · 10:10 AM

We also like a visually-hidden debug element that prints a compact contract summary (lane/posture/identity).

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

Camille Duarte
Jun 01, 2023 · 06:06 AM

Counterpoint: sometimes tests should reach into explicit contracts (like a vault) to set up a state quickly.

But assertions should still be user-level plus evidence-level, not internal-level.

Owen Price
Jun 25, 2023 · 10:22 PM

We treat contract logs as the companion to tests.

When a test fails, a stable log line like [tips] lane=... reason=... is faster than stepping through a debugger.

Benji Rios
Jul 21, 2023 · 07:07 AM

Long-form counterpoint: if your tests are flaky, it might be because your app has hidden async work.

The fix isn't more retries; it's exposing the work as a lane and making the lane observable in UI.

Lina Ortega
Aug 19, 2023 · 07:19 PM

Docs tie-in: the forum's evidence posture is basically a testing posture.

Related: Testing and Debugging and Performance and Rendering (Best Practices).

Tomas Rivera
Sep 18, 2023 · 12:12 PM

We also test "posture" changes (hot/warm/cool) by asserting on the evidence key rather than trying to measure performance in unit tests.

E2E can validate performance; unit tests validate contract decisions.

Salma Qureshi
Oct 15, 2023 · 03:15 PM

Counterpoint: evidence keys can tempt teams to stop writing good assertions (they assert on one attribute and call it a day).

We require one user-level assertion per test so we don't drift into contract-only testing.

Evan Brooks
Dec 20, 2023 · 06:18 PM

RTL is still the best way to keep tests from turning into a UI implementation mirror.

If you pair it with a small evidence layer, you get both user truth and contract truth in a stable way.

Harper Iqbal
Dec 31, 2023 · 09:09 AM

A good test suite reads like the contract story: intent -> lane -> result.

RTL helps you keep it user-shaped, and evidence/logs help you keep it debuggable.