Skip to content

Commit

Permalink
Create reusable helper to temp disable act warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Nov 28, 2023
1 parent ea821a9 commit 2e72d53
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
15 changes: 15 additions & 0 deletions src/testing/internal/disposables/disableActWarnings.ts
Original file line number Diff line number Diff line change
@@ -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;
});
}
1 change: 1 addition & 0 deletions src/testing/internal/disposables/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { disableActWarnings } from "./disableActWarnings.js";
export { spyOnConsole } from "./spyOnConsole.js";
export { withCleanup } from "./withCleanup.js";
12 changes: 5 additions & 7 deletions src/testing/internal/profile/profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 });

Expand Down Expand Up @@ -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),
Expand All @@ -305,7 +304,6 @@ export function createProfiler<
if (!(error && error instanceof WaitForRenderTimeoutError)) {
iteratorPosition++;
}
(globalThis as any).IS_REACT_ACT_ENVIRONMENT = prevActEnv;
}
},
getCurrentRender() {
Expand Down

0 comments on commit 2e72d53

Please sign in to comment.