From 8af02aa39e100e75def953aa3c67336a2066dfb3 Mon Sep 17 00:00:00 2001 From: Ricky Date: Wed, 10 Apr 2024 14:13:46 -0400 Subject: [PATCH] [tests] Assert scheduler log empty in internalAct (#28737) We should force `assertLog` to be called before each `act` block to ensure the queue is empty. Requires fixing tests: - https://github.com/facebook/react/pull/28745 - https://github.com/facebook/react/pull/28758 - https://github.com/facebook/react/pull/28759 - https://github.com/facebook/react/pull/28760 - https://github.com/facebook/react/pull/28761 - https://github.com/facebook/react/pull/28762 - https://github.com/facebook/react/pull/28763 - https://github.com/facebook/react/pull/28812 --- packages/internal-test-utils/internalAct.js | 13 +++++++++++ scripts/flow/environment.js | 25 +++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/packages/internal-test-utils/internalAct.js b/packages/internal-test-utils/internalAct.js index 7b22c44697e36..8f8667ce0cfd2 100644 --- a/packages/internal-test-utils/internalAct.js +++ b/packages/internal-test-utils/internalAct.js @@ -19,6 +19,7 @@ import type {Thenable} from 'shared/ReactTypes'; import * as Scheduler from 'scheduler/unstable_mock'; import enqueueTask from './enqueueTask'; +import {diff} from 'jest-diff'; export let actingUpdatesScopeDepth: number = 0; @@ -45,6 +46,18 @@ export async function act(scope: () => Thenable): Thenable { ); } + const actualYields = Scheduler.unstable_clearLog(); + if (actualYields.length !== 0) { + const error = Error( + 'Log of yielded values is not empty. Call assertLog first.\n\n' + + `Received:\n${diff('', actualYields.join('\n'), { + omitAnnotationLines: true, + })}`, + ); + Error.captureStackTrace(error, act); + throw error; + } + // $FlowFixMe[cannot-resolve-name]: Flow doesn't know about global Jest object if (!jest.isMockFunction(setTimeout)) { throw Error( diff --git a/scripts/flow/environment.js b/scripts/flow/environment.js index cba4dd5e61566..0b6be17a1b1d7 100644 --- a/scripts/flow/environment.js +++ b/scripts/flow/environment.js @@ -332,6 +332,31 @@ declare module 'node:worker_threads' { } } +declare module 'jest-diff' { + declare type CompareKeys = ((a: string, b: string) => number) | void; + declare type DiffOptions = { + aAnnotation?: string, + aColor?: (arg: string) => string, + aIndicator?: string, + bAnnotation?: string, + bColor?: (arg: string) => string, + bIndicator?: string, + changeColor?: (arg: string) => string, + changeLineTrailingSpaceColor?: (arg: string) => string, + commonColor?: (arg: string) => string, + commonIndicator?: string, + commonLineTrailingSpaceColor?: (arg: string) => string, + contextLines?: number, + emptyFirstOrLastLinePlaceholder?: string, + expand?: boolean, + includeChangeCounts?: boolean, + omitAnnotationLines?: boolean, + patchColor?: (arg: string) => string, + compareKeys?: CompareKeys, + }; + declare function diff(a: any, b: any, options?: DiffOptions): string; +} + declare const Bun: { hash( input: string | $TypedArray | DataView | ArrayBuffer | SharedArrayBuffer,