diff --git a/src/__tests__/old-act.js b/src/__tests__/old-act.js index 5414334..bcd296e 100644 --- a/src/__tests__/old-act.js +++ b/src/__tests__/old-act.js @@ -100,4 +100,47 @@ 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 [ + Array [ + Object { + "error": "some error", + }, + ], + ], + Array [ + "It looks like you're using a version of react-test-renderer that supports the \\"act\\" function, but not an awaitable version of \\"act\\" which you will need. Please upgrade to at least react-test-renderer@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-test-renderer', () => ({ + act: () => { + return new Promise(resolve => { + console.error( + 'Warning: The callback passed to TestRenderer.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 5de7829..403fab6 100644 --- a/src/act-compat.js +++ b/src/act-compat.js @@ -22,12 +22,15 @@ 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 TestRenderer.act') === 0 ) { // v16.8.6 isAsyncActSupported = false; } else if ( + firstArgIsString && args[0].indexOf( 'Warning: The callback passed to TestRenderer.act(...) function must not return anything', ) === 0