Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

test(webv2): wait for zag's highlight, and collapse the act() warning flood

Lincoln Stein72493a6eb3bd
diff --git a/invokeai/frontend/webv2/scripts/browser-test-console.ts b/invokeai/frontend/webv2/scripts/browser-test-console.ts
new file mode 100644
index 0000000000..2331bd138e
--- /dev/null
+++ b/invokeai/frontend/webv2/scripts/browser-test-console.ts
@@ -0,0 +1,69 @@
+/* eslint-disable no-console -- this module exists to intercept and re-emit console output */
+import { afterAll, beforeAll } from 'vitest';
+
+/**
+ * Collapses React's "not wrapped in act(...)" warning in browser runs.
+ *
+ * The warning is ~10 lines of identical boilerplate and fires once per unwrapped update, so a full
+ * run emitted over a thousand copies of the same text -- enough to bury the actual failure at the
+ * bottom of a CI log. Nearly all of them come from third-party components whose resize-driven
+ * state updates we do not control (Chakra's ScrollArea is the bulk of them), so the individual
+ * copies carry no information a count does not.
+ *
+ * The signal is kept rather than silenced: every occurrence is counted per component and reported
+ * once per test file. Anything that is not this specific warning passes through untouched.
+ */
+const ACT_WARNING = 'was not wrapped in act(';
+const COMPONENT = /An update to (\S+)/;
+const PLACEHOLDER = /%[sdifoOc]/g;
+
+/** React logs this warning as a format string with the component in a trailing argument. */
+const interpolate = (args: unknown[]): string => {
+ const [template, ...rest] = args;
+
+ if (typeof template !== 'string') {
+ return args.map((arg) => String(arg)).join(' ');
+ }
+
+ let next = 0;
+ const filled = template.replace(PLACEHOLDER, () => (next < rest.length ? String(rest[next++]) : '%s'));
+
+ return [filled, ...rest.slice(next)].map((arg) => String(arg)).join(' ');
+};
+
+const counts = new Map<string, number>();
+let passThrough: typeof console.error | null = null;
+
+beforeAll(() => {
+ passThrough = console.error;
+ console.error = (...args: unknown[]) => {
+ const text = interpolate(args);
+
+ if (text.includes(ACT_WARNING)) {
+ const component = COMPONENT.exec(text)?.[1] ?? 'unknown component';
+ counts.set(component, (counts.get(component) ?? 0) + 1);
+ return;
+ }
+
+ passThrough?.(...args);
+ };
+});
+
+afterAll(() => {
+ const report = passThrough;
+
+ if (report) {
+ console.error = report;
+ }
+ if (counts.size === 0) {
+ return;
+ }
+
+ const summary = [...counts.entries()]
+ .sort(([, left], [, right]) => right - left)
+ .map(([component, count]) => `${component} x${String(count)}`)
+ .join(', ');
+
+ counts.clear();
+ report?.(`act() warnings collapsed: ${summary}`);
+});
diff --git a/invokeai/frontend/webv2/src/workbench/image-actions/ImageContextMenu.browser.test.tsx b/invokeai/frontend/webv2/src/workbench/image-actions/ImageContextMenu.browser.test.tsx
index 060fa1561a..7013052cc2 100644
--- a/invokeai/frontend/webv2/src/workbench/image-actions/ImageContextMenu.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/workbench/image-actions/ImageContextMenu.browser.test.tsx
@@ -77,11 +77,31 @@ const interact = (action: () => void): Promise<void> =>
});
});
+/** Polls inside `act` until the condition holds, so a wait tracks state rather than a fixed delay. */
+const settleUntil = async (isSettled: () => boolean, description: string, timeoutMs = 2000): Promise<void> => {
+ const deadline = Date.now() + timeoutMs;
+
+ while (!isSettled()) {
+ if (Date.now() > deadline) {
+ throw new Error(`Timed out after ${String(timeoutMs)}ms waiting for ${description}`);
+ }
+ await act(async () => {
+ await new Promise<void>((resolve) => {
+ globalThis.setTimeout(resolve, 10);
+ });
+ });
+ }
+};
+
/** Quick icon items select through zag, which needs the item highlighted by a hover before the click. */
const pickQuickItem = async (label: string): Promise<void> => {
const target = document.querySelector<HTMLElement>(`[aria-label="${label}"]`);
expect(target).not.toBeNull();
await interact(() => target!.dispatchEvent(new PointerEvent('pointermove', { bubbles: true, pointerType: 'mouse' })));
+ // zag applies `data-highlighted` asynchronously and ignores a click on an unhighlighted item, so
+ // a fixed wait here silently dropped the click whenever the machine needed longer than it --
+ // the action mock simply recorded no call, and only under CI load.
+ await settleUntil(() => target!.hasAttribute('data-highlighted'), `"${label}" to be highlighted`);
await interact(() => target!.click());
};
diff --git a/invokeai/frontend/webv2/vitest.browser.config.mts b/invokeai/frontend/webv2/vitest.browser.config.mts
index 076e977e94..139e972e63 100644
--- a/invokeai/frontend/webv2/vitest.browser.config.mts
+++ b/invokeai/frontend/webv2/vitest.browser.config.mts
@@ -35,6 +35,7 @@ export default mergeConfig(
provider: playwright(),
},
include: ['src/**/*.browser.test.{ts,tsx}'],
+ setupFiles: ['./scripts/browser-test-console.ts'],
},
})
);