Skip to content

Commit

Permalink
[tests] Assert scheduler log empty in internalAct (#28737)
Browse files Browse the repository at this point in the history
We should force `assertLog` to be called before each `act` block to
ensure the queue is empty.

Requires fixing tests:
- #28745
- #28758
- #28759
- #28760
- #28761
- #28762
- #28763
- #28812
  • Loading branch information
rickhanlonii authored Apr 10, 2024
1 parent 870e404 commit 3f947b1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/internal-test-utils/internalAct.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -45,6 +46,18 @@ export async function act<T>(scope: () => Thenable<T>): Thenable<T> {
);
}

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(
Expand Down
25 changes: 25 additions & 0 deletions scripts/flow/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 3f947b1

Please sign in to comment.