testing-library / testing-library/react-testing-library

post-upgrade, unexpected assertion failures on error-handling function calls (includes repro)

Open
#1,127 5 comments 8 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
19.7k
Forks
1.2k
Avg merge
3d 16h
Merged PRs (30d)
1

Description

  • @testing-library/react version: 13.4.0
  • Testing Framework and version: jest@27.5.1
  • DOM Environment: jsdom@16.7.0
  • node: 16.14.2
  • react: 18.2.0
Relevant code or config:
  render(<App />);

  await screen.findByText('Failed!');

  //assertion fails unless we `waitFor` it
  expect(mockLog).toHaveBeenCalledWith('kaboom!')
What you did:

Upgraded existing application to react 18+ and RTL 13+.

What happened:

A number of tests starting failing assertions after upgrading.

In this case, the component is rendered with a simulated fetch failure. The component renders a child component that is responsible for error presentation and minor logging (in an effect hook). The test assertion on the logging function call fails unless it is awaited.

Reproduction:

I was able to reproduce the issue with a simple CRA repo, here: https://github.com/trv-wcaneira/fetch-err-waitfor-mcve

Problem description:

Pre-upgrade, it was sufficient to await the error message text rendered by the child component. Now it seems additional waiting is needed, and before I plaster waitFors all over our tests 😄, I'd just like to understand why.

Suggested solution:

I could obviously change all the tests to waitFor the logging function call, but would like to understand the change in behavior.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked CRA reproduction and inspect the render, findByText, and mockLog assertion sequence shown in the issue. Confirm the post-upgrade timing behavior and document why waiting for the rendered error does not establish that the effect-based logging call has completed.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
frontend, testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.