test: React 18 act() adjustments#6943
Conversation
c72d471 to
fcc0e7e
Compare
| "@svgr/core": "8.0.0", | ||
| "@svgr/plugin-jsx": "^8.0.1", | ||
| "@svgr/plugin-svgo": "^8.0.1", | ||
| "@testing-library/dom": "^8.12.0", |
There was a problem hiding this comment.
@testing-library/dom is unused at this point as this PR switches to using fireEvent imported from @testing-library/react (and automatically wrapped in act() so we don't need to do that manually)
There was a problem hiding this comment.
Nice - was that a recent change in @testing-library/react? Or did I just add a library I didn't need to when I was originally setting up RTL? 😅
There was a problem hiding this comment.
I believe it existed for quite some time now, but it was never well documented and people get confused all the time!
There was a problem hiding this comment.
Doh! Thanks a million for the cleanup in that case! 💯
|
|
||
| component.setState({ isListOpen: true }); | ||
| act(() => { | ||
| component.setState({ isListOpen: true }); |
There was a problem hiding this comment.
React 17 act() can only return void or Promise so all one-liners like this one need to be inserted in a block
… latter exports are wrapped in act()
…tAdvanceTimersByTime utility function
fcc0e7e to
d119980
Compare
|
FYI I switched to importing |
Nice, that totally makes sense! |
cee-chen
left a comment
There was a problem hiding this comment.
Thanks for taking a little longer to yarn about code abstraction and answer my various questions Tomasz! Changes look great!
* test: replace @testing-library/dom with @testing-library/react as the latter exports are wrapped in act() * test: wrap function calls with act() that need it * test: use act() wrapper for jest.advanceTimersByTime calls and add actAdvanceTimersByTime utility function * test: wait for element to be rendered before taking a snapshot to fix act() warning
* test: replace @testing-library/dom with @testing-library/react as the latter exports are wrapped in act() * test: wrap function calls with act() that need it * test: use act() wrapper for jest.advanceTimersByTime calls and add actAdvanceTimersByTime utility function * test: wait for element to be rendered before taking a snapshot to fix act() warning
* test: replace @testing-library/dom with @testing-library/react as the latter exports are wrapped in act() * test: wrap function calls with act() that need it * test: use act() wrapper for jest.advanceTimersByTime calls and add actAdvanceTimersByTime utility function * test: wait for element to be rendered before taking a snapshot to fix act() warning
* test: replace @testing-library/dom with @testing-library/react as the latter exports are wrapped in act() * test: wrap function calls with act() that need it * test: use act() wrapper for jest.advanceTimersByTime calls and add actAdvanceTimersByTime utility function * test: wait for element to be rendered before taking a snapshot to fix act() warning
Summary
This PR adds act() wrappers in a few places to ensure correct execution when running on React 18 and earlier versions.
QA
yarnto install dependenciesREACT_VERSION=17 yarn test-unitand ensure there'sRunning tests on React v17printed out and there are no failing testsyarn test-unitand confirmRunning tests on React v18is printed out and there are some failing tests, but none are caused byact()warnings.