From 9020b83ca047c6dadaaa1e70407b6146caf5cff9 Mon Sep 17 00:00:00 2001 From: Ronald van der Kooij Date: Thu, 29 Aug 2019 23:15:35 +0200 Subject: [PATCH 1/3] checking the type of args in act-compat before doing an indexOf --- src/__tests__/old-act.js | 24 ++++++++++++++++++++++++ src/act-compat.js | 3 +++ 2 files changed, 27 insertions(+) diff --git a/src/__tests__/old-act.js b/src/__tests__/old-act.js index fcd531b4..22a35275 100644 --- a/src/__tests__/old-act.js +++ b/src/__tests__/old-act.js @@ -98,4 +98,28 @@ test('async act recovers from sync errors', async () => { `) }) +test('async act can handle any sort of console.error', async () => { + try { + await asyncAct(async () => { + console.error({error: 'some error'}) + await null + }) + } catch (err) { + console.log(err) + } + 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.", + ], + ] + `) +}) + /* 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 From 011b255643b3604b31433942e4f864a8f45374ae Mon Sep 17 00:00:00 2001 From: Ronald van der Kooij Date: Sun, 1 Sep 2019 11:16:42 +0200 Subject: [PATCH 2/3] removed unused code --- src/__tests__/old-act.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/__tests__/old-act.js b/src/__tests__/old-act.js index 22a35275..3168ab61 100644 --- a/src/__tests__/old-act.js +++ b/src/__tests__/old-act.js @@ -99,14 +99,11 @@ test('async act recovers from sync errors', async () => { }) test('async act can handle any sort of console.error', async () => { - try { - await asyncAct(async () => { - console.error({error: 'some error'}) - await null - }) - } catch (err) { - console.log(err) - } + await asyncAct(async () => { + console.error({error: 'some error'}) + await null + }) + expect(console.error).toHaveBeenCalledTimes(2) expect(console.error.mock.calls).toMatchInlineSnapshot(` Array [ From 75ac5ab84f27dd10879bc8e9be00fe639b7fb8c9 Mon Sep 17 00:00:00 2001 From: Ronald van der Kooij Date: Tue, 3 Sep 2019 22:21:19 +0200 Subject: [PATCH 3/3] noop scenario for console.error was not tested in act-compat --- src/__tests__/old-act.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/__tests__/old-act.js b/src/__tests__/old-act.js index 3168ab61..b3de9377 100644 --- a/src/__tests__/old-act.js +++ b/src/__tests__/old-act.js @@ -119,4 +119,24 @@ test('async act can handle any sort of console.error', async () => { `) }) +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 */