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

React Testing Library Intro - discussion (2023-03-04)

Started by Talia Moss
externaltestingreact testing libraryevidencecontractsaccessibilityroute flowsdebuggability

The RTL intro is usually presented as "test like a user". What I'm more interested in is how it pairs with the forum's evidence idea: if the UI renders stable evidence keys for lanes and sources, you can write tests that stay meaningful without digging through component internals.

Read the docs

Do you test behavior via text/roles only, or do you also assert on data-* evidence keys for lanes and sources? How do you prevent tests from becoming brittle when UI structure shifts but behavior stays the same? Do you keep a render log/tips log in tests, or is that too much ceremony?

Comments (18)

Back to latest
Hannah Chen
Mar 08, 2023 · 08:08 AM

Roles + text for most assertions.

Evidence keys only for posture (loading/error) because users report those states.

Rafael Soto
Mar 12, 2023 · 12:00 PM

We assert on evidence keys when the behavior is about contracts (lane/source), not content. It keeps tests stable when wording changes.

ts
import { render, screen } from '@testing-library/react';

test('shows pending lane while fetching', () => {
  render(<SearchRoute />);
  expect(screen.getByTestId('route-shell')).toHaveAttribute('data-evidence', expect.stringContaining('lane:pending'));
});

We still use roles/text for user-visible copy, but lanes are a contract surface.

Mei Tan
Mar 20, 2023 · 04:04 AM

Long-form: the trap is thinking there is one correct testing style. For product copy and accessibility, roles/text matter. For state posture, evidence matters. For navigation motion, route-flow tests matter. If you force everything into one shape, you either get brittle tests or vague tests.

We also log the test story as tips lines because it makes failures readable:

txt
[tips] test=SearchRoute step=render lane=pending
[tips] test=SearchRoute step=resolve lane=ok results=27
Jun Park
Mar 27, 2023 · 07:07 AM

Concrete alternative: never assert on data-* because it's implementation detail.

We tried that. It sounded principled, but it pushed us into asserting on exact text and DOM shape, which was much more brittle.

Talia Moss
Mar 29, 2023 · 09:30 AM

I think the trick is treating evidence keys as part of the product contract, not component structure.

If support relies on evidence, tests can too.

Keira Santos
Apr 08, 2023 · 08:08 AM

Docs tie-in: if you adopt evidence keys, it connects directly to the docs' testing guidance and the general idea of observable contracts.

Related: Testing and Debugging and Accessibility.

Ibrahim Saleh
Apr 22, 2023 · 04:40 AM

We keep tests from being brittle by writing helpers that assert on behavior, not nodes.

It feels like extra work until you do a redesign and your test suite doesn't explode.

Sofia Martinez
May 06, 2023 · 06:06 AM

We do a small amount of log assertion, but we treat logs as stable vocabulary (like lanes).

If the log words change, that often means behavior changed, so the diff is legitimate.

Nina Kapoor
May 21, 2023 · 11:11 AM

A practical note: if you want evidence keys to be testable, keep them in one attribute and keep them space-separated.

We use data-evidence="lane:pending source:cache" style so both humans and tests can read it.

Caleb Price
Jun 08, 2023 · 08:40 AM

I like RTL's philosophy because it forces you to make your UI accessible, which accidentally makes it testable.

Once you can select things by role/label, you're closer to how users actually interact.

Lina Ortega
Jun 28, 2023 · 06:18 PM

Short take: if your tests are brittle, your UI contracts are brittle.

Evan Brooks
Jul 19, 2023 · 07:19 PM

We used RTL for unit-level behavior and a separate route-flow harness for navigation. Mixing them got confusing.

Also: evidence keys are great, but don't let them replace good labels/roles.

Arman Qadir
Aug 12, 2023 · 12:12 PM

Long-form counterpoint: evidence keys can become a second UI that devs optimize for instead of users.

We had to adopt a rule: evidence must be derived from actual state, not manually toggled. Otherwise it lies and tests become meaningless.

Grace McCoy
Sep 10, 2023 · 10:10 AM

We got value from keeping a tiny test-only debug helper that prints the evidence attribute when tests fail.

It turns failures into a readable story.

Tomas Rivera
Oct 21, 2023 · 09:21 PM

We rarely assert on logs, but we do print tips logs when a test fails.

That keeps logs helpful without freezing them as a contract.

Marisol Vega
Nov 16, 2023 · 04:16 PM

If you want expert-level value: spend time on your test data and your boundary normalization, not on clever selectors.

Most flaky tests are actually boundary bugs (timing, identity, cached state).

Anika Sharma
Dec 05, 2023 · 05:05 AM

One more: RTL encourages you to separate draft state from committed state, or your tests will be a mess.

Salma Qureshi
Dec 22, 2023 · 10:22 PM

We found evidence keys improved team communication even outside tests.

People started describing bugs as "lane stuck pending" instead of vague screenshots.

Priya Liao
Dec 31, 2023 · 08:20 PM

Takeaway: RTL + evidence keys is a good combo when you treat evidence as product contract, not test convenience.

If evidence is real, tests stay meaningful.