Skip to content

Commit 8aadd4f

Browse files
committed
New internal testing helpers: waitFor, waitForAll
Over the years, we've gradually aligned on a set of best practices for for testing concurrent React features in this repo. The default in most cases is to use `act`, the same as you would do when testing a real React app. However, because we're testing React itself, as opposed to an app that uses React, our internal tests sometimes need to make assertions on intermediate states that `act` intentionally disallows. For those cases, we built a custom set of Jest assertion matchers that provide greater control over the concurrent work queue. It works by mocking the Scheduler package. (When we eventually migrate to using native postTask, it would probably work by stubbing that instead.) A problem with these helpers that we recently discovered is, because they are synchronous function calls, they aren't sufficient if the work you need to flush is scheduled in a microtask — we don't control the microtask queue, and can't mock it. `act` addresses this problem by encouraging you to await the result of the `act` call. (It's not currently required to await, but in future versions of React it likely will be.) It will then continue flushing work until both the microtask queue and the Scheduler queue is exhausted. We can follow a similar strategy for our custom test helpers, by replacing the current set of synchronous helpers with a corresponding set of async ones: - `expect(Scheduler).toFlushAndYield(log)` -> `await waitForAll(log)` - `expect(Scheduler).toFlushAndYieldThrough(log)` -> `await waitFor(log)` - `expect(Scheduler).toFlushUntilNextPaint(log)` -> `await waitForPaint(log)` These APIs are inspired by the existing best practice for writing e2e React tests. Rather than mock all task queues, in an e2e test you set up a timer loop and wait for the UI to match an expecte condition. Although we are mocking _some_ of the task queues in our tests, the general principle still holds: it makes it less likely that our tests will diverge from real world behavior in an actual browser. In this commit, I've implemented the new testing helpers and converted one of the Suspense tests to use them. In subsequent steps, I'll codemod the rest of our test suite.
1 parent c600974 commit 8aadd4f

File tree

4 files changed

+195
-10
lines changed

4 files changed

+195
-10
lines changed

packages/internal-test-utils/ReactInternalTestUtils.js

Lines changed: 175 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,178 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
// TODO: Move `internalAct` and other test helpers to this package
8+
// TODO: Move `internalAct` and other test helpers to this package, too
9+
10+
import * as SchedulerMock from 'scheduler/unstable_mock';
11+
import {diff} from 'jest-diff';
12+
import {equals} from '@jest/expect-utils';
13+
14+
function assertYieldsWereCleared(Scheduler) {
15+
const actualYields = Scheduler.unstable_clearYields();
16+
if (actualYields.length !== 0) {
17+
const error = Error(
18+
'The event log is not empty. Call assertLog(...) first.',
19+
);
20+
Error.captureStackTrace(error, assertYieldsWereCleared);
21+
throw error;
22+
}
23+
}
24+
25+
export async function waitFor(expectedLog) {
26+
assertYieldsWereCleared(SchedulerMock);
27+
28+
// Create the error object before doing any async work, to get a better
29+
// stack trace.
30+
const error = new Error();
31+
Error.captureStackTrace(error, waitFor);
32+
33+
const actualLog = [];
34+
do {
35+
// Wait until end of current task/microtask.
36+
await null;
37+
if (SchedulerMock.unstable_hasPendingWork()) {
38+
SchedulerMock.unstable_flushNumberOfYields(
39+
expectedLog.length - actualLog.length,
40+
);
41+
actualLog.push(...SchedulerMock.unstable_clearYields());
42+
if (expectedLog.length > actualLog.length) {
43+
// Continue flushing until we've logged the expected number of items.
44+
} else {
45+
// Once we've reached the expected sequence, wait one more microtask to
46+
// flush any remaining synchronous work.
47+
await null;
48+
actualLog.push(...SchedulerMock.unstable_clearYields());
49+
break;
50+
}
51+
} else {
52+
// There's no pending work, even after a microtask.
53+
break;
54+
}
55+
} while (true);
56+
57+
if (equals(actualLog, expectedLog)) {
58+
return;
59+
}
60+
61+
error.message = `
62+
Expected sequence of events did not occur.
63+
64+
${diff(expectedLog, actualLog)}
65+
`;
66+
throw error;
67+
}
68+
69+
export async function waitForAll(expectedLog) {
70+
assertYieldsWereCleared(SchedulerMock);
71+
72+
// Create the error object before doing any async work, to get a better
73+
// stack trace.
74+
const error = new Error();
75+
Error.captureStackTrace(error, waitFor);
76+
77+
do {
78+
// Wait until end of current task/microtask.
79+
await null;
80+
if (!SchedulerMock.unstable_hasPendingWork()) {
81+
// There's no pending work, even after a microtask. Stop flushing.
82+
break;
83+
}
84+
SchedulerMock.unstable_flushAllWithoutAsserting();
85+
} while (true);
86+
87+
const actualLog = SchedulerMock.unstable_clearYields();
88+
if (equals(actualLog, expectedLog)) {
89+
return;
90+
}
91+
92+
error.message = `
93+
Expected sequence of events did not occur.
94+
95+
${diff(expectedLog, actualLog)}
96+
`;
97+
throw error;
98+
}
99+
100+
export async function waitForThrow(expectedError: mixed) {
101+
assertYieldsWereCleared(SchedulerMock);
102+
103+
// Create the error object before doing any async work, to get a better
104+
// stack trace.
105+
const error = new Error();
106+
Error.captureStackTrace(error, waitFor);
107+
108+
do {
109+
// Wait until end of current task/microtask.
110+
await null;
111+
if (!SchedulerMock.unstable_hasPendingWork()) {
112+
// There's no pending work, even after a microtask. Stop flushing.
113+
error.message = 'Expected something to throw, but nothing did.';
114+
throw error;
115+
}
116+
try {
117+
SchedulerMock.unstable_flushAllWithoutAsserting();
118+
} catch (x) {
119+
if (equals(x, expectedError)) {
120+
return;
121+
}
122+
if (typeof x === 'object' && x !== null && x.message === expectedError) {
123+
return;
124+
}
125+
error.message = `
126+
Expected error was not thrown.
127+
128+
${diff(expectedError, x)}
129+
`;
130+
throw error;
131+
}
132+
} while (true);
133+
}
134+
135+
// TODO: This name is a bit misleading currently because it will stop as soon as
136+
// React yields for any reason, not just for a paint. I've left it this way for
137+
// now because that's how untable_flushUntilNextPaint already worked, but maybe
138+
// we should split these use cases into separate APIs.
139+
export async function waitForPaint(expectedLog) {
140+
assertYieldsWereCleared(SchedulerMock);
141+
142+
// Create the error object before doing any async work, to get a better
143+
// stack trace.
144+
const error = new Error();
145+
Error.captureStackTrace(error, waitFor);
146+
147+
// Wait until end of current task/microtask.
148+
await null;
149+
if (SchedulerMock.unstable_hasPendingWork()) {
150+
// Flush until React yields.
151+
SchedulerMock.unstable_flushUntilNextPaint();
152+
// Wait one more microtask to flush any remaining synchronous work.
153+
await null;
154+
}
155+
156+
const actualLog = SchedulerMock.unstable_clearYields();
157+
if (equals(actualLog, expectedLog)) {
158+
return;
159+
}
160+
161+
error.message = `
162+
Expected sequence of events did not occur.
163+
164+
${diff(expectedLog, actualLog)}
165+
`;
166+
throw error;
167+
}
168+
169+
export function assertLog(expectedLog) {
170+
const actualLog = SchedulerMock.unstable_clearYields();
171+
if (equals(actualLog, expectedLog)) {
172+
return;
173+
}
174+
175+
const error = new Error(`
176+
Expected sequence of events did not occur.
177+
178+
${diff(expectedLog, actualLog)}
179+
`);
180+
Error.captureStackTrace(error, assertLog);
181+
throw error;
182+
}

packages/jest-react/src/JestReact.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ function assertYieldsWereCleared(root) {
3131
const Scheduler = root._Scheduler;
3232
const actualYields = Scheduler.unstable_clearYields();
3333
if (actualYields.length !== 0) {
34-
throw new Error(
34+
const error = Error(
3535
'Log of yielded values is not empty. ' +
3636
'Call expect(ReactTestRenderer).unstable_toHaveYielded(...) first.',
3737
);
38+
Error.captureStackTrace(error, assertYieldsWereCleared);
39+
throw error;
3840
}
3941
}
4042

packages/react-reconciler/src/__tests__/ReactSuspenseWithNoopRenderer-test.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ let Fragment;
33
let ReactNoop;
44
let Scheduler;
55
let act;
6+
let waitFor;
7+
let waitForAll;
8+
let assertLog;
9+
let waitForPaint;
610
let Suspense;
711
let getCacheForType;
812

@@ -19,6 +23,11 @@ describe('ReactSuspenseWithNoopRenderer', () => {
1923
Scheduler = require('scheduler');
2024
act = require('jest-react').act;
2125
Suspense = React.Suspense;
26+
const InternalTestUtils = require('internal-test-utils');
27+
waitFor = InternalTestUtils.waitFor;
28+
waitForAll = InternalTestUtils.waitForAll;
29+
waitForPaint = InternalTestUtils.waitForPaint;
30+
assertLog = InternalTestUtils.assertLog;
2231

2332
getCacheForType = React.unstable_getCacheForType;
2433

@@ -208,7 +217,7 @@ describe('ReactSuspenseWithNoopRenderer', () => {
208217
React.startTransition(() => {
209218
ReactNoop.render(<Foo />);
210219
});
211-
expect(Scheduler).toFlushAndYieldThrough([
220+
await waitFor([
212221
'Foo',
213222
'Bar',
214223
// A suspends
@@ -226,7 +235,7 @@ describe('ReactSuspenseWithNoopRenderer', () => {
226235

227236
// Even though the promise has resolved, we should now flush
228237
// and commit the in progress render instead of restarting.
229-
expect(Scheduler).toFlushAndYield(['D']);
238+
await waitForPaint(['D']);
230239
expect(ReactNoop).toMatchRenderedOutput(
231240
<>
232241
<span prop="Loading..." />
@@ -235,11 +244,8 @@ describe('ReactSuspenseWithNoopRenderer', () => {
235244
</>,
236245
);
237246

238-
// Await one micro task to attach the retry listeners.
239-
await null;
240-
241247
// Next, we'll flush the complete content.
242-
expect(Scheduler).toFlushAndYield(['Bar', 'A', 'B']);
248+
await waitForAll(['Bar', 'A', 'B']);
243249

244250
expect(ReactNoop).toMatchRenderedOutput(
245251
<>
@@ -544,7 +550,7 @@ describe('ReactSuspenseWithNoopRenderer', () => {
544550
ReactNoop.flushSync(() => {
545551
ReactNoop.render(<App highPri="B" lowPri="1" />);
546552
});
547-
expect(Scheduler).toHaveYielded(['B', '1']);
553+
assertLog(['B', '1']);
548554
expect(ReactNoop).toMatchRenderedOutput(
549555
<>
550556
<span prop="B" />

scripts/jest/matchers/schedulerTestMatchers.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ function captureAssertion(fn) {
1818

1919
function assertYieldsWereCleared(Scheduler) {
2020
const actualYields = Scheduler.unstable_clearYields();
21+
2122
if (actualYields.length !== 0) {
22-
throw new Error(
23+
const error = Error(
2324
'Log of yielded values is not empty. ' +
2425
'Call expect(Scheduler).toHaveYielded(...) first.'
2526
);
27+
Error.captureStackTrace(error, assertYieldsWereCleared);
28+
throw error;
2629
}
2730
}
2831

0 commit comments

Comments
 (0)