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 1 commit
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
18 changes: 18 additions & 0 deletions src/__tests__/old-act.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'})
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you have to do a console.error({ message: 'some message' } insteadof throwing it, to mimic the problem

Copy link
Author

Choose a reason for hiding this comment

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

Changed the test to not throw error.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is correct 👍

but you had to change the assertion of another test, which is an indication of a wrong implementation (you are still not calling the original console.error)

check this implementation for inpiration :-):
rvdkooy@9020b83

@kentcdodds can you have a look if we're on the right track with this?

})
} 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 */
32 changes: 17 additions & 15 deletions src/act-compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
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.

}
}
let cbReturn, result
Expand Down