Skip to content

add type checking to asyncAct args #469

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

Closed
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
45 changes: 33 additions & 12 deletions src/__tests__/old-act.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this log is no longer happening. Could you fix your changes so you don't have to change this snapshot?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed implementation to revert back to this snapshot.

],
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.",
],
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 [email protected] to remove this warning.",
],
Array [
"sigil",
],
]
`)
expect(callback).toHaveBeenCalledTimes(1)

// and it doesn't warn you twice
Expand Down Expand Up @@ -98,4 +95,28 @@ test('async act recovers from sync errors', async () => {
`)
})

test('async act handles values that are not strings', async () => {
try {
await asyncAct(async () => {
console.error({message: 'some message'})
await null
})
} catch (err) {
console.error('call console.error')
}
expect(console.error).toHaveBeenCalledTimes(2)
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 [email protected] to remove this warning.",
],
]
`)
})

/* eslint no-console:0 */
28 changes: 15 additions & 13 deletions src/act-compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,21 @@ 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
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
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
} else {
originalConsoleError.apply(console, args)
}

I think that should do it.

} else {
originalConsoleError.apply(console, args)
}
Expand Down