Enzyme: wrapper.simulate() should be wrapped in ReactTestUtils.act()

Created on 5 Apr 2019  Â·  13Comments  Â·  Source: enzymejs/enzyme

Is your feature request related to a problem? Please describe.
React 16.8 introduced act() to allow component interactions to be tested in a synchronous way.

Describe the solution you'd like
wrapper.simulate() should be wrapped in act() to ensure any state changes are resolved before proceeding.

Describe alternatives you've considered

act(() => {
  wrapper.simulate();
});

Though it's easy to forget act.

Additional context
I have a feeling this is already on your roadmap, but I couldn't find an issue about it.

All 13 comments

This is already addressed by #2034.

Out of curiosity, does that PR mean that any time a mounted component is rendered, it's wrapped in act()? Or only on mount()?

Only mount mounts components, so yes to both?

Shallow rendering does not yet have an “act”, so that’s still unaddressed.

@ljharb sorry how does #2034 address this? It seems like that only wraps mount() calls in act()? simulate() should _also_ wrap calls, which is not what's happening on the latest published enzyme

@jquense there's a bunch of unpublished stuff in enzyme, for one, but you're right that simulate probably isn't covered. A PR to fix that would be great.

act requires a DOM, and there's no act that works in a non-browser environment, so it's simply not an option for shallow at this time.

Could you re-open this issue then?

It’s not fixable by enzyme until the shallow renderer provides an act, but sure.

But it's not fixable for simulate in full DOM rendering?

Ah, true, that is fixable. PRs welcome :-) even if they only have test cases.

Thanks! I'll try to find time to contribute. I think this will be a huge improvement for users who need to force wrappers to update after simulating events to ensure that state updates are complete.

@mikesteele let me know if you don't have any time . i'd be happy to put something together as well

Feel free to race each other to get something up :-p thanks in advance! <3

@jquense I probably won't have time for a few weeks, feel free to take this

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nelsonchen90 picture nelsonchen90  Â·  3Comments

mattkauffman23 picture mattkauffman23  Â·  3Comments

heikkimu picture heikkimu  Â·  3Comments

potapovDim picture potapovDim  Â·  3Comments

modemuser picture modemuser  Â·  3Comments