-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Do we need to use react-dom/test-utils act() function? #278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
bit pooped today, so I'll write you failing tests tomorrow. but your test just got lucky :) if you hadn't fired the click, then the effect wouldn't have flushed, and effectCb wouldn't have been called. also, if you look closely, effectCb is supposed to fire twice (once on initial render, once after clicking the button). if render and fireEvent were wrapped with act(), then you'd see effectCb called twice. (and only once if you didn't click the button) |
Oh of course! My bad. Thanks @threepointone! I'll have this ready to go by the time y'all release hooks I think. Thanks for all the work you do man. Get some rest Sunil, you look tired. |
btw, the reason you didn't see a warning, is because event handlers are batched by default in react, so the setState 'happened' within a batch. but it doesn't flush effects early by default. hence the need to wrap fireEvent with act |
OOoooh, that makes a lot of sense, thanks! What about callbacks? How are people supposed to handle things like: useEffect(() => {
const unsubscribe = socket.subscribe(changes => setSomeState(changes.someState))
return unsubscribe
}) My inclination is to assume that in this situation people would mock the thing that's calling their callback and make sure they trigger a change within an act(() => {
socketMock.fireChanges(fakeChanges)
}) Let me know if that's not right. react-testing-library will re-export |
that's right! also, fake timers are super useful, so you could run // expect(a spinner to be shown)
act(() => {
jest.runAllTimers() // or jest.advanceTimersByTime(period)
});
// expect(data to be on screen) |
Perfect. Thanks friend 🤗 |
Just tried to update mobx-react-lite package to React 16.8 and there is a bunch of failing tests now. I want to have a closer look later, but it might be actually related to this issue. Leaving this here as a case study for now. Update: Seems that |
Afk, so cant look at the logs. Could you show me an example of the failures/corresponding error/warning/log messages? |
@threepointone Turns out it was a mistake on our side (see more in mobxjs/mobx-react-lite#57 (comment)) This PR should be merged I think because now every test is spilling this warning. I wanted to try it out, but I am failing to build RTL on Windows.
|
570e2d8
to
2cf65fe
Compare
This also removes `flushEffects` which is no longer necessary!
2cf65fe
to
5c353ab
Compare
This also removes `flushEffects` which is no longer necessary!
just to clarify, does this mean you were already using hooks in production? |
@threepointone Well, that package is built solely around the hooks since they became a thing :) And I know a bunch of people that were using in production already. It's working just fine now, except those tests spilling warnings. I've opened a new issue about it: #281 |
I'm not sure what's up here, but it looks like we don't need it after all. The test here is passing and I'm not seeing any warnings 🤔 I would expect this to fail.
It .... just works™️ ???
Help appreciated @threepointone @gaearon