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

Commit 99c3f6b

Browse files
AEganbcarroll22
authored andcommitted
fix(act): account for console.error argument not being a string (#97)
prevents asyncAct throwing an error if the item being logged isn't a string. Coped mostly from testing-library/react-testing-library#476
1 parent a1f9f85 commit 99c3f6b

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/__tests__/old-act.js

+43
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,47 @@ test('async act recovers from sync errors', async () => {
100100
`);
101101
});
102102

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

src/act-compat.js

+3
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ function asyncAct(cb) {
2222
console.error = function error(...args) {
2323
/* if console.error fired *with that specific message* */
2424
/* istanbul ignore next */
25+
const firstArgIsString = typeof args[0] === 'string';
2526
if (
27+
firstArgIsString &&
2628
args[0].indexOf('Warning: Do not await the result of calling TestRenderer.act') === 0
2729
) {
2830
// v16.8.6
2931
isAsyncActSupported = false;
3032
} else if (
33+
firstArgIsString &&
3134
args[0].indexOf(
3235
'Warning: The callback passed to TestRenderer.act(...) function must not return anything',
3336
) === 0

0 commit comments

Comments
 (0)