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.

Tests with async useEffect don't seem to complete

See original GitHub issue

Describe the bug

So I guess this is on the back of my question (https://github.com/callstack/react-native-testing-library/issues/1066) where I was trying to reproduce and isolate the issue. However, I have come up against something similar.

Repo: https://github.com/leepowelldev/rntl-async-bug

I have created a new component MyComponent which has three useEffects which consume async methods (each has appropriate cleanup functions). The test waits for these effects to run, and then assert the final rendered text. However, this never succeeds and the text can never be found. It seems to hang on the second useEffect and the test times out - resulting in the component being unmounted.

Expected behavior

Test should pass and should be able to find ‘Hello world.’ - this works as expected in simulator with no errors.

Steps to Reproduce

Clone repo, install deps and run npm test or yarn test

Screenshots

Screenshot 2022-08-18 at 16 51 14

Versions

npmPackages: @testing-library/react-native: ^11.0.0 => 11.0.0 react: 18.0.0 => 18.0.0 react-native: 0.69.4 => 0.69.4 react-test-renderer: 18.0.0 => 18.0.0

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
leepowellnbscommented, Sep 30, 2022

@mdjastrzebski sorry, this was on my “todo” list, but it’s been a busy few days and it slipped my mind. Thanks for the fix and for testing 🙇

0reactions
mdjastrzebskicommented, Sep 30, 2022

Closing as fixed.

@leepowelldev If the issue still occurs for you on the latest version of RNTL, please provide update the repo with latest RNTL, React, etc deps so that we are able to reproduce it on our end.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing asynchronous useEffect - Stack Overflow
I want to be able to test that the fetched data is displayed correctly. While this works fine in the browser, the tests...
Read more >
useEffect with async function call causes act(...) warning #667
When rendering and testing a component with a useEffect that calls an async function that modifies the component's state, then an act() ...
Read more >
Testing-library: avoid these mistakes in async tests
Conclusion. Never forget to await for async functions or return promises from the test (jest will wait for this promise to be resolved...
Read more >
How the hell am I supposed to use act() properly with async ...
I'm using enzyme and jest for my tests. I'm just hoping someone can explain how to properly handle an async useEffect immediately after...
Read more >
Async waits in React Testing Library - Reflect.run
React applications often perform asynchronous actions, like making calls to APIs to fetch data from a backend server.
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