diff --git a/src/__tests__/old-act.js b/src/__tests__/old-act.js index fcd531b4..b3de9377 100644 --- a/src/__tests__/old-act.js +++ b/src/__tests__/old-act.js @@ -98,4 +98,45 @@ test('async act recovers from sync errors', async () => { `) }) +test('async act can handle any sort of console.error', async () => { + await asyncAct(async () => { + console.error({error: 'some error'}) + await null + }) + + expect(console.error).toHaveBeenCalledTimes(2) + expect(console.error.mock.calls).toMatchInlineSnapshot(` + Array [ + Array [ + Object { + "error": "some error", + }, + ], + Array [ + "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 react-dom@16.9.0 to remove this warning.", + ], + ] + `) +}) + +test('async act should not show an error when ReactTestUtils.act returns something', async () => { + jest.resetModules() + jest.mock('react-dom/test-utils', () => ({ + act: () => { + return new Promise(resolve => { + console.error( + 'Warning: The callback passed to ReactTestUtils.act(...) function must not return anything', + ) + resolve() + }) + }, + })) + asyncAct = require('../act-compat').asyncAct + await asyncAct(async () => { + await null + }) + + expect(console.error).toHaveBeenCalledTimes(0) +}) + /* eslint no-console:0 */ diff --git a/src/act-compat.js b/src/act-compat.js index dd5f1d96..d758a97c 100644 --- a/src/act-compat.js +++ b/src/act-compat.js @@ -28,7 +28,9 @@ function asyncAct(cb) { console.error = function error(...args) { /* if console.error fired *with that specific message* */ /* istanbul ignore next */ + const firstArgIsString = typeof args[0] === 'string' if ( + firstArgIsString && args[0].indexOf( 'Warning: Do not await the result of calling ReactTestUtils.act', ) === 0 @@ -36,6 +38,7 @@ function asyncAct(cb) { // v16.8.6 isAsyncActSupported = false } else if ( + firstArgIsString && args[0].indexOf( 'Warning: The callback passed to ReactTestUtils.act(...) function must not return anything', ) === 0