React-testing-library: Suppressing react-dom act warning

Created on 19 Aug 2019  路  5Comments  路  Source: testing-library/react-testing-library

Since upgrading to @testing-library/react 9.1.x we are seeing the following warning in nearly all our tests:

It looks like you're using a version of react-dom that supports the "act" function, but not an awaitable version of "act" which you will need. Please upgrade to at least [email protected] to remove this warning.

However, we are not yet in a position to update react and react-dom to 16.9.0 as this causes issues with some 3rd-party components we are consuming.

Is there any way to suppress this warning? It feels like something we should be able to opt out of.

Most helpful comment

Do this in your setup file:

// hack until we can upgrade to [email protected]
const originalError = console.error
beforeAll(() => {
  // this is here to silence a warning temporarily
  // we'll fix it in the next exercise
  jest.spyOn(console, 'error').mockImplementation((...args) => {
    if (typeof args[0] === 'string' && args[0].includes('Please upgrade to at least [email protected]')) {
      return
    }
    return originalError.call(console, args)
  })
})

afterAll(() => {
  console.error.mockRestore()
})

That should do it for you :) I don't think that I want to add special logic here for opting-out. I can be convinced otherwise though I guess.

All 5 comments

Do this in your setup file:

// hack until we can upgrade to [email protected]
const originalError = console.error
beforeAll(() => {
  // this is here to silence a warning temporarily
  // we'll fix it in the next exercise
  jest.spyOn(console, 'error').mockImplementation((...args) => {
    if (typeof args[0] === 'string' && args[0].includes('Please upgrade to at least [email protected]')) {
      return
    }
    return originalError.call(console, args)
  })
})

afterAll(() => {
  console.error.mockRestore()
})

That should do it for you :) I don't think that I want to add special logic here for opting-out. I can be convinced otherwise though I guess.

For now I'll close this.

Hi. I have a similar problem with the latest versions of both React and RTL. I extracted out a minimal representation of this component and left only two tests: one doesn't show the error, the other does.

Am I using this wrong? Or is there still an issue with React?

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

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
    in FacilitySelector

Edit: Not sure if this belongs here, or elsewhere. Sorry.

Hi @jktravis, it's likely that something is happening after your test finishes which is triggering the warning. Please provide some additional details (and try to make your example a bit smaller) on https://spectrum.chat/testing-library/help-react

Do this in your setup file:

After a bit of tinkering, I worked out I had to go with something a little more manual as spyOn() wasn't working with my version of Jest. Hopefully, the below saves someone some time!

const originalError = global.console.error;
beforeAll(() => {
    global.console.error = jest.fn((...args) => {
        if (typeof args[0] === 'string' && args[0].includes('Please upgrade to at least [email protected]')) {
          return
        }
        return originalError.call(console, args)
    });
});

afterAll(() => {
    global.console.error.mockRestore();
});
Was this page helpful?
0 / 5 - 0 ratings