Skip to content

fix: console.error doesn't have to be a string in act.compat.js #476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/__tests__/old-act.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected] 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 */
3 changes: 3 additions & 0 deletions src/act-compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ 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
) {
// v16.8.6
isAsyncActSupported = false
} else if (
firstArgIsString &&
args[0].indexOf(
'Warning: The callback passed to ReactTestUtils.act(...) function must not return anything',
) === 0
Expand Down