From bb45efaafc58f9d5ac6f8abfa18446cea1cf8fd7 Mon Sep 17 00:00:00 2001 From: Ely Alamillo Date: Wed, 28 Aug 2019 20:12:37 -0700 Subject: [PATCH 1/3] add type checking to asyncAct args --- src/__tests__/old-act.js | 18 ++++++++++++++++++ src/act-compat.js | 32 +++++++++++++++++--------------- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/__tests__/old-act.js b/src/__tests__/old-act.js index fcd531b4..7119ab65 100644 --- a/src/__tests__/old-act.js +++ b/src/__tests__/old-act.js @@ -98,4 +98,22 @@ test('async act recovers from sync errors', async () => { `) }) +test('async act handles values that are not strings', async () => { + try { + await asyncAct(() => { + throw new Error({error: 'test error'}) + }) + } catch (err) { + console.error('call console.error') + } + expect(console.error).toHaveBeenCalledTimes(1) + expect(console.error.mock.calls).toMatchInlineSnapshot(` + Array [ + Array [ + "call console.error", + ], + ] + `) +}) + /* eslint no-console:0 */ diff --git a/src/act-compat.js b/src/act-compat.js index dd5f1d96..9fad213f 100644 --- a/src/act-compat.js +++ b/src/act-compat.js @@ -28,21 +28,23 @@ function asyncAct(cb) { console.error = function error(...args) { /* if console.error fired *with that specific message* */ /* istanbul ignore next */ - if ( - args[0].indexOf( - 'Warning: Do not await the result of calling ReactTestUtils.act', - ) === 0 - ) { - // v16.8.6 - isAsyncActSupported = false - } else if ( - args[0].indexOf( - 'Warning: The callback passed to ReactTestUtils.act(...) function must not return anything', - ) === 0 - ) { - // no-op - } else { - originalConsoleError.apply(console, args) + if (typeof args[0] === 'string') { + if ( + args[0].indexOf( + 'Warning: Do not await the result of calling ReactTestUtils.act', + ) === 0 + ) { + // v16.8.6 + isAsyncActSupported = false + } else if ( + args[0].indexOf( + 'Warning: The callback passed to ReactTestUtils.act(...) function must not return anything', + ) === 0 + ) { + // no-op + } else { + originalConsoleError.apply(console, args) + } } } let cbReturn, result From 9d6677454fb471a0f7bd50a11a97c735c9026a13 Mon Sep 17 00:00:00 2001 From: Ely Alamillo Date: Thu, 29 Aug 2019 19:06:39 -0700 Subject: [PATCH 2/3] handle case where args[o] is not a string --- src/__tests__/old-act.js | 33 +++++++++++++++++++-------------- src/act-compat.js | 4 ++-- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/__tests__/old-act.js b/src/__tests__/old-act.js index 7119ab65..35a895f9 100644 --- a/src/__tests__/old-act.js +++ b/src/__tests__/old-act.js @@ -32,18 +32,15 @@ test('async act works even when the act is an old one', async () => { console.error('sigil') }) expect(console.error.mock.calls).toMatchInlineSnapshot(` - Array [ - Array [ - "sigil", - ], - 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.", - ], - Array [ - "sigil", - ], - ] - `) + Array [ + 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.", + ], + Array [ + "sigil", + ], + ] + `) expect(callback).toHaveBeenCalledTimes(1) // and it doesn't warn you twice @@ -101,14 +98,22 @@ test('async act recovers from sync errors', async () => { test('async act handles values that are not strings', async () => { try { await asyncAct(() => { - throw new Error({error: 'test error'}) + console.error({message: 'some message'}) }) } catch (err) { console.error('call console.error') } - expect(console.error).toHaveBeenCalledTimes(1) + expect(console.error).toHaveBeenCalledTimes(3) expect(console.error.mock.calls).toMatchInlineSnapshot(` Array [ + Array [ + Object { + "message": "some message", + }, + ], + 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.", + ], Array [ "call console.error", ], diff --git a/src/act-compat.js b/src/act-compat.js index 9fad213f..3c0c2395 100644 --- a/src/act-compat.js +++ b/src/act-compat.js @@ -42,9 +42,9 @@ function asyncAct(cb) { ) === 0 ) { // no-op - } else { - originalConsoleError.apply(console, args) } + } else { + originalConsoleError.apply(console, args) } } let cbReturn, result From c16decddf2db5fd5f182b9687bd67541dfcf601e Mon Sep 17 00:00:00 2001 From: Ely Alamillo Date: Mon, 2 Sep 2019 15:27:55 -0700 Subject: [PATCH 3/3] fixed to address changing of test snapshots --- src/__tests__/old-act.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/__tests__/old-act.js b/src/__tests__/old-act.js index 35a895f9..0116ba7c 100644 --- a/src/__tests__/old-act.js +++ b/src/__tests__/old-act.js @@ -97,13 +97,14 @@ test('async act recovers from sync errors', async () => { test('async act handles values that are not strings', async () => { try { - await asyncAct(() => { + await asyncAct(async () => { console.error({message: 'some message'}) + await null }) } catch (err) { console.error('call console.error') } - expect(console.error).toHaveBeenCalledTimes(3) + expect(console.error).toHaveBeenCalledTimes(2) expect(console.error.mock.calls).toMatchInlineSnapshot(` Array [ Array [ @@ -114,9 +115,6 @@ test('async act handles values that are not strings', async () => { 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.", ], - Array [ - "call console.error", - ], ] `) })