From e5507326c8b5ff59ee66d4919681e848819dd09e Mon Sep 17 00:00:00 2001 From: Andrew Clark Date: Thu, 2 Mar 2023 16:18:52 -0500 Subject: [PATCH] New internal testing helpers: waitFor, waitForAll MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- packages/jest-react/src/JestReact.js | 131 +++++++++++++++++- .../ReactSuspenseWithNoopRenderer-test.js | 16 ++- .../jest/matchers/schedulerTestMatchers.js | 5 +- scripts/rollup/bundles.js | 2 +- 4 files changed, 142 insertions(+), 12 deletions(-) diff --git a/packages/jest-react/src/JestReact.js b/packages/jest-react/src/JestReact.js index f67ddd8636f1a..708f4a4a49924 100644 --- a/packages/jest-react/src/JestReact.js +++ b/packages/jest-react/src/JestReact.js @@ -8,6 +8,8 @@ import {REACT_ELEMENT_TYPE, REACT_FRAGMENT_TYPE} from 'shared/ReactSymbols'; import isArray from 'shared/isArray'; +import * as SchedulerMock from 'scheduler/unstable_mock'; +import {diff} from 'jest-diff'; export {act} from './internalAct'; @@ -27,19 +29,20 @@ function captureAssertion(fn) { return {pass: true}; } -function assertYieldsWereCleared(root) { - const Scheduler = root._Scheduler; +function assertYieldsWereCleared(Scheduler) { const actualYields = Scheduler.unstable_clearYields(); if (actualYields.length !== 0) { - throw new Error( + const error = Error( 'Log of yielded values is not empty. ' + 'Call expect(ReactTestRenderer).unstable_toHaveYielded(...) first.', ); + Error.captureStackTrace(error, assertYieldsWereCleared); + throw error; } } export function unstable_toMatchRenderedOutput(root, expectedJSX) { - assertYieldsWereCleared(root); + assertYieldsWereCleared(root._Scheduler); const actualJSON = root.toJSON(); let actualJSX; @@ -121,3 +124,123 @@ function jsonChildrenToJSXChildren(jsonChildren) { } return null; } + +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; + break; + } + } else { + // There's no pending work, even after a microtask. + break; + } + } while (true); + + if (areArraysEqual(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 (areArraysEqual(actualLog, expectedLog)) { + return; + } + + error.message = ` +Expected sequence of events did not occur. + +${diff(expectedLog, actualLog)} +`; + throw error; +} + +// 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 (areArraysEqual(actualLog, expectedLog)) { + return; + } + + error.message = ` +Expected sequence of events did not occur. + +${diff(expectedLog, actualLog)} +`; + throw error; +} + +function areArraysEqual(a, b) { + if (a.length !== b.length) { + return false; + } + for (let i = 0; i < a.length; i++) { + if (a[i] !== b[i]) { + return false; + } + } + return true; +} diff --git a/packages/react-reconciler/src/__tests__/ReactSuspenseWithNoopRenderer-test.js b/packages/react-reconciler/src/__tests__/ReactSuspenseWithNoopRenderer-test.js index c73cd296b8695..29b908ce18d2c 100644 --- a/packages/react-reconciler/src/__tests__/ReactSuspenseWithNoopRenderer-test.js +++ b/packages/react-reconciler/src/__tests__/ReactSuspenseWithNoopRenderer-test.js @@ -3,6 +3,9 @@ let Fragment; let ReactNoop; let Scheduler; let act; +let waitFor; +let waitForAll; +let waitForPaint; let Suspense; let getCacheForType; @@ -19,6 +22,10 @@ describe('ReactSuspenseWithNoopRenderer', () => { Scheduler = require('scheduler'); act = require('jest-react').act; Suspense = React.Suspense; + const JestReact = require('jest-react'); + waitFor = JestReact.waitFor; + waitForAll = JestReact.waitForAll; + waitForPaint = JestReact.waitForPaint; getCacheForType = React.unstable_getCacheForType; @@ -208,7 +215,7 @@ describe('ReactSuspenseWithNoopRenderer', () => { React.startTransition(() => { ReactNoop.render(); }); - expect(Scheduler).toFlushAndYieldThrough([ + await waitFor([ 'Foo', 'Bar', // A suspends @@ -226,7 +233,7 @@ describe('ReactSuspenseWithNoopRenderer', () => { // Even though the promise has resolved, we should now flush // and commit the in progress render instead of restarting. - expect(Scheduler).toFlushAndYield(['D']); + await waitForPaint(['D']); expect(ReactNoop).toMatchRenderedOutput( <> @@ -235,11 +242,8 @@ describe('ReactSuspenseWithNoopRenderer', () => { , ); - // Await one micro task to attach the retry listeners. - await null; - // Next, we'll flush the complete content. - expect(Scheduler).toFlushAndYield(['Bar', 'A', 'B']); + await waitForAll(['Bar', 'A', 'B']); expect(ReactNoop).toMatchRenderedOutput( <> diff --git a/scripts/jest/matchers/schedulerTestMatchers.js b/scripts/jest/matchers/schedulerTestMatchers.js index f18ccfc548093..645d8a58cc59f 100644 --- a/scripts/jest/matchers/schedulerTestMatchers.js +++ b/scripts/jest/matchers/schedulerTestMatchers.js @@ -18,11 +18,14 @@ function captureAssertion(fn) { function assertYieldsWereCleared(Scheduler) { const actualYields = Scheduler.unstable_clearYields(); + if (actualYields.length !== 0) { - throw new Error( + const error = Error( 'Log of yielded values is not empty. ' + 'Call expect(Scheduler).toHaveYielded(...) first.' ); + Error.captureStackTrace(error, assertYieldsWereCleared); + throw error; } } diff --git a/scripts/rollup/bundles.js b/scripts/rollup/bundles.js index 0372304a2016d..5a60b35e54f51 100644 --- a/scripts/rollup/bundles.js +++ b/scripts/rollup/bundles.js @@ -960,7 +960,7 @@ const bundles = [ global: 'JestReact', minifyWithProdErrorCodes: false, wrapWithModuleBoundaries: false, - externals: ['react', 'scheduler', 'scheduler/unstable_mock'], + externals: ['react', 'scheduler', 'scheduler/unstable_mock', 'jest-diff'], }, /******* ESLint Plugin for Hooks *******/