Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do we need to use react-dom/test-utils act() function? #278

Merged
merged 1 commit into from Feb 6, 2019

Conversation

kentcdodds
Copy link
Member

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

@threepointone
Copy link
Contributor

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)

@kentcdodds
Copy link
Member Author

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.

@threepointone
Copy link
Contributor

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

@kentcdodds
Copy link
Member Author

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 manually. Is that right? So something like:

act(() => {
  socketMock.fireChanges(fakeChanges)
})

Let me know if that's not right. react-testing-library will re-export act for this reason and will have docs/examples for this kind of scenario.

@threepointone
Copy link
Contributor

threepointone commented Feb 6, 2019

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)

@kentcdodds
Copy link
Member Author

Perfect. Thanks friend 馃

@danielkcz
Copy link
Contributor

danielkcz commented Feb 6, 2019

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 act does not solve most of the failing tests. It's something else related to new React version 馃槙

@threepointone
Copy link
Contributor

Afk, so cant look at the logs. Could you show me an example of the failures/corresponding error/warning/log messages?

@danielkcz
Copy link
Contributor

@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.

When testing, code that causes React state updates should be wrapped into act(...):
      
      act(() => {
        /* fire events that update state */
      });
      /* assert on the output */
      
This ensures that you're testing the behavior the user would see in the browser. Learn more at https://fb.me/react-wrap-tests-with-act

@kentcdodds kentcdodds force-pushed the pr/test-utils-act branch 7 times, most recently from 570e2d8 to 2cf65fe Compare February 6, 2019 15:34
This also removes `flushEffects` which is no longer necessary!
@kentcdodds kentcdodds merged commit 373294d into next Feb 6, 2019
@kentcdodds kentcdodds deleted the pr/test-utils-act branch February 6, 2019 15:35
kentcdodds pushed a commit that referenced this pull request Feb 6, 2019
This also removes `flushEffects` which is no longer necessary!
@threepointone
Copy link
Contributor

now every test is spilling this warning

just to clarify, does this mean you were already using hooks in production?

@danielkcz
Copy link
Contributor

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants