-
Notifications
You must be signed in to change notification settings - Fork 47k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New internal testing helpers: waitFor, waitForAll, waitForPaint (#26285)
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.
- Loading branch information
Showing
8 changed files
with
215 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
// TODO: Move `internalAct` and other test helpers to this package, too | ||
|
||
import * as SchedulerMock from 'scheduler/unstable_mock'; | ||
import {diff} from 'jest-diff'; | ||
import {equals} from '@jest/expect-utils'; | ||
|
||
function assertYieldsWereCleared(Scheduler) { | ||
const actualYields = Scheduler.unstable_clearYields(); | ||
if (actualYields.length !== 0) { | ||
const error = Error( | ||
'The event log is not empty. Call assertLog(...) first.', | ||
); | ||
Error.captureStackTrace(error, assertYieldsWereCleared); | ||
throw error; | ||
} | ||
} | ||
|
||
export async function waitFor(expectedLog) { | ||
assertYieldsWereCleared(SchedulerMock); | ||
|
||
// Create the error object before doing any async work, to get a better | ||
// stack trace. | ||
const error = new Error(); | ||
Error.captureStackTrace(error, waitFor); | ||
|
||
const actualLog = []; | ||
do { | ||
// Wait until end of current task/microtask. | ||
await null; | ||
if (SchedulerMock.unstable_hasPendingWork()) { | ||
SchedulerMock.unstable_flushNumberOfYields( | ||
expectedLog.length - actualLog.length, | ||
); | ||
actualLog.push(...SchedulerMock.unstable_clearYields()); | ||
if (expectedLog.length > actualLog.length) { | ||
// Continue flushing until we've logged the expected number of items. | ||
} else { | ||
// Once we've reached the expected sequence, wait one more microtask to | ||
// flush any remaining synchronous work. | ||
await null; | ||
actualLog.push(...SchedulerMock.unstable_clearYields()); | ||
break; | ||
} | ||
} else { | ||
// There's no pending work, even after a microtask. | ||
break; | ||
} | ||
} while (true); | ||
|
||
if (equals(actualLog, expectedLog)) { | ||
return; | ||
} | ||
|
||
error.message = ` | ||
Expected sequence of events did not occur. | ||
${diff(expectedLog, actualLog)} | ||
`; | ||
throw error; | ||
} | ||
|
||
export async function waitForAll(expectedLog) { | ||
assertYieldsWereCleared(SchedulerMock); | ||
|
||
// Create the error object before doing any async work, to get a better | ||
// stack trace. | ||
const error = new Error(); | ||
Error.captureStackTrace(error, waitFor); | ||
|
||
do { | ||
// Wait until end of current task/microtask. | ||
await null; | ||
if (!SchedulerMock.unstable_hasPendingWork()) { | ||
// There's no pending work, even after a microtask. Stop flushing. | ||
break; | ||
} | ||
SchedulerMock.unstable_flushAllWithoutAsserting(); | ||
} while (true); | ||
|
||
const actualLog = SchedulerMock.unstable_clearYields(); | ||
if (equals(actualLog, expectedLog)) { | ||
return; | ||
} | ||
|
||
error.message = ` | ||
Expected sequence of events did not occur. | ||
${diff(expectedLog, actualLog)} | ||
`; | ||
throw error; | ||
} | ||
|
||
export async function waitForThrow(expectedError: mixed) { | ||
assertYieldsWereCleared(SchedulerMock); | ||
|
||
// Create the error object before doing any async work, to get a better | ||
// stack trace. | ||
const error = new Error(); | ||
Error.captureStackTrace(error, waitFor); | ||
|
||
do { | ||
// Wait until end of current task/microtask. | ||
await null; | ||
if (!SchedulerMock.unstable_hasPendingWork()) { | ||
// There's no pending work, even after a microtask. Stop flushing. | ||
error.message = 'Expected something to throw, but nothing did.'; | ||
throw error; | ||
} | ||
try { | ||
SchedulerMock.unstable_flushAllWithoutAsserting(); | ||
} catch (x) { | ||
if (equals(x, expectedError)) { | ||
return; | ||
} | ||
if (typeof x === 'object' && x !== null && x.message === expectedError) { | ||
return; | ||
} | ||
error.message = ` | ||
Expected error was not thrown. | ||
${diff(expectedError, x)} | ||
`; | ||
throw error; | ||
} | ||
} while (true); | ||
} | ||
|
||
// TODO: This name is a bit misleading currently because it will stop as soon as | ||
// React yields for any reason, not just for a paint. I've left it this way for | ||
// now because that's how untable_flushUntilNextPaint already worked, but maybe | ||
// we should split these use cases into separate APIs. | ||
export async function waitForPaint(expectedLog) { | ||
assertYieldsWereCleared(SchedulerMock); | ||
|
||
// Create the error object before doing any async work, to get a better | ||
// stack trace. | ||
const error = new Error(); | ||
Error.captureStackTrace(error, waitFor); | ||
|
||
// Wait until end of current task/microtask. | ||
await null; | ||
if (SchedulerMock.unstable_hasPendingWork()) { | ||
// Flush until React yields. | ||
SchedulerMock.unstable_flushUntilNextPaint(); | ||
// Wait one more microtask to flush any remaining synchronous work. | ||
await null; | ||
} | ||
|
||
const actualLog = SchedulerMock.unstable_clearYields(); | ||
if (equals(actualLog, expectedLog)) { | ||
return; | ||
} | ||
|
||
error.message = ` | ||
Expected sequence of events did not occur. | ||
${diff(expectedLog, actualLog)} | ||
`; | ||
throw error; | ||
} | ||
|
||
export function assertLog(expectedLog) { | ||
const actualLog = SchedulerMock.unstable_clearYields(); | ||
if (equals(actualLog, expectedLog)) { | ||
return; | ||
} | ||
|
||
const error = new Error(` | ||
Expected sequence of events did not occur. | ||
${diff(expectedLog, actualLog)} | ||
`); | ||
Error.captureStackTrace(error, assertLog); | ||
throw error; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './ReactInternalTestUtils'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"private": true, | ||
"name": "internal-test-utils", | ||
"version": "0.0.0" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters