Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Stop the flushSync warning in unit specs (#84027)

Romeo Van Snickefca03649451
diff --git a/frontend/test/jest-setup-env.js b/frontend/test/jest-setup-env.js
index 8ed40e55be0..93e784b9721 100644
--- a/frontend/test/jest-setup-env.js
+++ b/frontend/test/jest-setup-env.js
@@ -52,18 +52,32 @@ afterEach(() => {
// costing getComputedStyle calls and an extra re-render per position update.
jest.mock("@floating-ui/dom", () => ({
...jest.requireActual("@floating-ui/dom"),
- // Resolving synchronously keeps the position update inside the act() scope of
- // the render that asked for it. A resolved promise defers it by a microtask,
- // which can land after the test ends and makes React log an act() warning.
+ // The caller asks for a position from a layout effect and applies the result
+ // with flushSync. Answered on the spot, that flushSync runs while React is
+ // still rendering, and React logs a warning. Answered from a plain promise,
+ // the state update lands outside act(), and React logs a different one. So
+ // the answer comes a microtask later, inside an act() scope of its own.
computePosition: (_reference, _floating, options = {}) => ({
- then: (onFulfilled) =>
- onFulfilled({
+ then: (onFulfilled) => {
+ const position = {
x: 0,
y: 0,
placement: options.placement ?? "bottom",
strategy: options.strategy ?? "absolute",
middlewareData: {},
- }),
+ };
+ queueMicrotask(() => {
+ // act() itself warns where the environment is not set up for it, which
+ // is the case inside Testing Library's async helpers.
+ if (globalThis.IS_REACT_ACT_ENVIRONMENT) {
+ jest.requireActual("react-dom/test-utils").act(() => {
+ onFulfilled(position);
+ });
+ } else {
+ onFulfilled(position);
+ }
+ });
+ },
}),
autoUpdate: (_reference, _floating, update) => {
update();