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,