question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Show stack-trace for failed findBy or waitFor

See original GitHub issue

Describe the Feature

Currently, if your test has a lot of await findBy, if one of them fails, it’s hard to know which one from the error message. It’d be great if the error message would include the line of the findBy that fails, the same way it does for getBy

with findBy:

  ● Testing react navigation › clicking on one item takes you to the details screen

    No instances found

with getBy:

 ● Testing react navigation › clicking on one item takes you to the details screen

    No instances found

      24 |     fireEvent(toClick, 'press');
      25 |     const newHeader = await findByText('Showing details for 5');
    > 26 |     const newBody = getByText('the number you have chosen is 6');

Possible Implementations

I’d be happy to look into the code and see if I can provide a PR if nobody is currently working on this.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
thymikeecommented, Sep 17, 2020

None that I know of

0reactions
mdjastrzebskicommented, Oct 19, 2022

@kevinbror KCD also recommends using only single assertion for waitFor queries in his Common mistakes with RTL article. The reasoning is as follows:

// ❌
await waitFor(() => {
  expect(window.fetch).toHaveBeenCalledWith('foo')
  expect(window.fetch).toHaveBeenCalledTimes(1)
})

// ✅
await waitFor(() => expect(window.fetch).toHaveBeenCalledWith('foo'))
expect(window.fetch).toHaveBeenCalledTimes(1)

Let’s say that for the example above, window.fetch was called twice. So the waitFor call will fail, however, we’ll have to wait for the timeout before we see that test failure. By putting a single assertion in there, we can both wait for the UI to settle to the state we want to assert on, and also fail faster if one of the assertions do end up failing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - Why does a test fail when using findBy but succeed ...
By the look of it, seems fine (except for using the find query inside waitFor). Try adding logs at every step of the...
Read more >
[Solved]-Why does a test fail when using findBy but succeed ...
I think its better to use waitFor than findBy which is in my opinion is more self explanatory that it is async/needs to...
Read more >
Async Methods - Testing Library
findBy methods are a combination of getBy queries and waitFor . They accept the waitFor options as the last argument (e.g. await screen....
Read more >
Using React Testing Library - Sentry Developer Documentation
We are in the process of converting our tests from Enzyme to React Testing Library. In this guide, you'll find tips to follow...
Read more >
ERROR-STACK-TRACE attribute - Progress Documentation
This attribute controls whether or not the CallStack property on error objects can be used. Because the CallStack feature is for debugging and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found