React: Warning: An update to SearchPage inside a test was not wrapped in act(...).

Created on 3 Jan 2020  路  6Comments  路  Source: facebook/react

Using Jest and Enzyme.

Screenshot 2020-01-02 at 17 10 58

The Bug is happening for this test.

Screenshot 2020-01-03 at 09 52 39

Needs More Information

Most helpful comment

When you use synchronous act function, because the API call is asynchronous, the submit handler is completed; however, that doesn't mean that the API call is finished within a single execution cycle. So, if you set state after the API response, it is going to be executed outside the act call. This is still the case when you mock your API calls because the function itself is asynchronous. I think you need to use asynchronous version of act:

it(..., async () => {
  // mock implementation etc
  await act(async () => {
    form.simulate('submit');
  });
}

Testing Recipe for Data Fetching

All 6 comments

@threepointone @gaearon
Please any input into why this is the case. Thank you.

@SarpongAbasimi Did you try wrapping the thing that causes the update (form.simulate('submit')) in act like the warning message says?

@bvaughn Yes but that didn't work.

Share your test code so one of us can run it? Screenshots of code are of limited use :smile:

When you use synchronous act function, because the API call is asynchronous, the submit handler is completed; however, that doesn't mean that the API call is finished within a single execution cycle. So, if you set state after the API response, it is going to be executed outside the act call. This is still the case when you mock your API calls because the function itself is asynchronous. I think you need to use asynchronous version of act:

it(..., async () => {
  // mock implementation etc
  await act(async () => {
    form.simulate('submit');
  });
}

Testing Recipe for Data Fetching

Thank you @GasimGasimzada you are a legend 馃憫.
And thanks for explaining it to me, really helpful.

I will now close this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kib357 picture kib357  路  103Comments

gaearon picture gaearon  路  126Comments

gaearon picture gaearon  路  227Comments

gabegreenberg picture gabegreenberg  路  264Comments

sophiebits picture sophiebits  路  107Comments