diff --git a/src/testing/internal/disposables/disableActWarnings.ts b/src/testing/internal/disposables/disableActWarnings.ts new file mode 100644 index 00000000000..c5254c8dc1d --- /dev/null +++ b/src/testing/internal/disposables/disableActWarnings.ts @@ -0,0 +1,15 @@ +import { withCleanup } from "./withCleanup.js"; + +/** + * Temporarily disable act warnings. + * + * https://github.com/reactwg/react-18/discussions/102 + */ +export function disableActWarnings() { + const prev = { prevActEnv: (globalThis as any).IS_REACT_ACT_ENVIRONMENT }; + (globalThis as any).IS_REACT_ACT_ENVIRONMENT = false; + + return withCleanup(prev, ({ prevActEnv }) => { + (globalThis as any).IS_REACT_ACT_ENVIRONMENT = prevActEnv; + }); +} diff --git a/src/testing/internal/disposables/index.ts b/src/testing/internal/disposables/index.ts index 6d232565db4..9895d129589 100644 --- a/src/testing/internal/disposables/index.ts +++ b/src/testing/internal/disposables/index.ts @@ -1,2 +1,3 @@ +export { disableActWarnings } from "./disableActWarnings.js"; export { spyOnConsole } from "./spyOnConsole.js"; export { withCleanup } from "./withCleanup.js"; diff --git a/src/testing/internal/profile/profile.tsx b/src/testing/internal/profile/profile.tsx index 542a6eae940..d74e6cae47f 100644 --- a/src/testing/internal/profile/profile.tsx +++ b/src/testing/internal/profile/profile.tsx @@ -10,6 +10,7 @@ import { RenderInstance } from "./Render.js"; import { applyStackTrace, captureStackTrace } from "./traces.js"; import type { ProfilerContextValue } from "./context.js"; import { ProfilerContextProvider, useProfilerContext } from "./context.js"; +import { disableActWarnings } from "../disposables/index.js"; type ValidSnapshot = void | (object & { /* not a function */ call?: never }); @@ -285,14 +286,12 @@ export function createProfiler< }, async takeRender(options: NextRenderOptions = {}) { // In many cases we do not control the resolution of the suspended - // promise which results in noisy tests when using this utility. Instead, - // we disable act warnings when using this utility. - // - // https://github.com/reactwg/react-18/discussions/102 - const prevActEnv = (globalThis as any).IS_REACT_ACT_ENVIRONMENT; - (globalThis as any).IS_REACT_ACT_ENVIRONMENT = false; + // promise which results in noisy tests when the profiler due to + // repeated act warnings. + using _disabledActWarnings = disableActWarnings(); let error: unknown = undefined; + try { return await Profiler.peekRender({ [_stackTrace]: captureStackTrace(Profiler.takeRender), @@ -305,7 +304,6 @@ export function createProfiler< if (!(error && error instanceof WaitForRenderTimeoutError)) { iteratorPosition++; } - (globalThis as any).IS_REACT_ACT_ENVIRONMENT = prevActEnv; } }, getCurrentRender() {