[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
Ricky committed
Apr 10, 2024 at 14:13 UTC
3f947b1b468a2a2a419227ce26842c4b6ba31082
2 files changed
+38
packages/internal-test-utils/internalAct.js
+13
@@ -19,6 +19,7 @@ import type {Thenable} from 'shared/ReactTypes';
19
import * as Scheduler from 'scheduler/unstable_mock';
20
21
import enqueueTask from './enqueueTask';
22
+import {diff} from 'jest-diff';
23
24
export let actingUpdatesScopeDepth: number = 0;
25
@@ -45,6 +46,18 @@ export async function act<T>(scope: () => Thenable<T>): Thenable<T> {
46
);
47
}
48
49
+ const actualYields = Scheduler.unstable_clearLog();
50
+ if (actualYields.length !== 0) {
51
+ const error = Error(
52
+ 'Log of yielded values is not empty. Call assertLog first.\n\n' +
53
+ `Received:\n${diff('', actualYields.join('\n'), {
54
+ omitAnnotationLines: true,
55
+ })}`,
56
+ );
57
+ Error.captureStackTrace(error, act);
58
+ throw error;
59
+ }
60
+
61
// $FlowFixMe[cannot-resolve-name]: Flow doesn't know about global Jest object
62
if (!jest.isMockFunction(setTimeout)) {
63
throw Error(
scripts/flow/environment.js
+25
@@ -332,6 +332,31 @@ declare module 'node:worker_threads' {
332
}
333
}
334
335
+declare module 'jest-diff' {
336
+ declare type CompareKeys = ((a: string, b: string) => number) | void;
337
+ declare type DiffOptions = {
338
+ aAnnotation?: string,
339
+ aColor?: (arg: string) => string,
340
+ aIndicator?: string,
341
+ bAnnotation?: string,
342
+ bColor?: (arg: string) => string,
343
+ bIndicator?: string,
344
+ changeColor?: (arg: string) => string,
345
+ changeLineTrailingSpaceColor?: (arg: string) => string,
346
+ commonColor?: (arg: string) => string,
347
+ commonIndicator?: string,
348
+ commonLineTrailingSpaceColor?: (arg: string) => string,
349
+ contextLines?: number,
350
+ emptyFirstOrLastLinePlaceholder?: string,
351
+ expand?: boolean,
352
+ includeChangeCounts?: boolean,
353
+ omitAnnotationLines?: boolean,
354
+ patchColor?: (arg: string) => string,
355
+ compareKeys?: CompareKeys,
356
+ };
357
+ declare function diff(a: any, b: any, options?: DiffOptions): string;
358
+}
359
+
360
declare const Bun: {
361
hash(
362
input: string | $TypedArray | DataView | ArrayBuffer | SharedArrayBuffer,