Skip to content

Commit 139df62

Browse files
rvdkooyKent C. Dodds
authored and
Kent C. Dodds
committed
fix: console.error doesn't have to be a string in act.compat.js (#476)
* checking the type of args in act-compat before doing an indexOf * removed unused code * noop scenario for console.error was not tested in act-compat
1 parent 7db67b3 commit 139df62

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/__tests__/old-act.js

+41
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,45 @@ test('async act recovers from sync errors', async () => {
9898
`)
9999
})
100100

101+
test('async act can handle any sort of console.error', async () => {
102+
await asyncAct(async () => {
103+
console.error({error: 'some error'})
104+
await null
105+
})
106+
107+
expect(console.error).toHaveBeenCalledTimes(2)
108+
expect(console.error.mock.calls).toMatchInlineSnapshot(`
109+
Array [
110+
Array [
111+
Object {
112+
"error": "some error",
113+
},
114+
],
115+
Array [
116+
"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.",
117+
],
118+
]
119+
`)
120+
})
121+
122+
test('async act should not show an error when ReactTestUtils.act returns something', async () => {
123+
jest.resetModules()
124+
jest.mock('react-dom/test-utils', () => ({
125+
act: () => {
126+
return new Promise(resolve => {
127+
console.error(
128+
'Warning: The callback passed to ReactTestUtils.act(...) function must not return anything',
129+
)
130+
resolve()
131+
})
132+
},
133+
}))
134+
asyncAct = require('../act-compat').asyncAct
135+
await asyncAct(async () => {
136+
await null
137+
})
138+
139+
expect(console.error).toHaveBeenCalledTimes(0)
140+
})
141+
101142
/* eslint no-console:0 */

src/act-compat.js

+3
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,17 @@ function asyncAct(cb) {
2828
console.error = function error(...args) {
2929
/* if console.error fired *with that specific message* */
3030
/* istanbul ignore next */
31+
const firstArgIsString = typeof args[0] === 'string'
3132
if (
33+
firstArgIsString &&
3234
args[0].indexOf(
3335
'Warning: Do not await the result of calling ReactTestUtils.act',
3436
) === 0
3537
) {
3638
// v16.8.6
3739
isAsyncActSupported = false
3840
} else if (
41+
firstArgIsString &&
3942
args[0].indexOf(
4043
'Warning: The callback passed to ReactTestUtils.act(...) function must not return anything',
4144
) === 0

0 commit comments

Comments
 (0)