react-testing-examples icon indicating copy to clipboard operation
react-testing-examples copied to clipboard

Async but does it need to be?

Open harkinj opened this issue 6 years ago • 2 comments

Hi, Thanks for this great site. This 1st simple test uses
await waitForElement(() => getByText(/hello Satoshi/i))

does it need to use waitForElement? Would getByText alone work? Thanks for the info. John

harkinj avatar Apr 18 '19 14:04 harkinj

Good question @harkinj,

Not sure what the right answer is. On one hand waitForElement is more future proof if you use this test as a template for more complicated tests where the text is rendered asynchronously, but on the other hand it makes the simple test look more complicated than it is. There are a few more reasons why I'd keep it:

  • getByText by itself doesn't look like an assertion. Someone who doesn't understand how RTL works under the hood (I don't!) might think the test is lacking an assertion.
  • Consistency between tests, not having to think when text is rendered from the start or after async event (especially since component can evolve over time and go from sync to async).

ovidiuch avatar Apr 18 '19 14:04 ovidiuch

Hi, Thanks for your quick response. As u say its not clear which is better but I take on your points. Thanks again John

harkinj avatar Apr 18 '19 15:04 harkinj