Skip to content
This repository was archived by the owner on Jul 30, 2020. It is now read-only.

Fix for console.error argument not being a string #97

Merged
merged 1 commit into from
Dec 13, 2019
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
43 changes: 43 additions & 0 deletions src/__tests__/old-act.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,47 @@ 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 [
Array [
Object {
"error": "some error",
},
],
],
Array [
"It looks like you're using a version of react-test-renderer 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-test-renderer', () => ({
act: () => {
return new Promise(resolve => {
console.error(
'Warning: The callback passed to TestRenderer.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 @@ -22,12 +22,15 @@ 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 TestRenderer.act') === 0
) {
// v16.8.6
isAsyncActSupported = false;
} else if (
firstArgIsString &&
args[0].indexOf(
'Warning: The callback passed to TestRenderer.act(...) function must not return anything',
) === 0
Expand Down