Skip to content

The open alternative to Salesforce, designed for AI.

Fix the flaky front-sb-test storybook shards (#24412)

Raphaël Bosic7a4001eaac6
diff --git a/packages/twenty-front/.storybook/vitest.setup.ts b/packages/twenty-front/.storybook/vitest.setup.ts
index de1f68906dd..757baca50da 100644
--- a/packages/twenty-front/.storybook/vitest.setup.ts
+++ b/packages/twenty-front/.storybook/vitest.setup.ts
@@ -2,6 +2,7 @@ import { Globals } from '@react-spring/web';
import { setProjectAnnotations } from '@storybook/react-vite';
import { MotionGlobalConfig } from 'framer-motion';
import * as projectAnnotations from './preview';
+import { waitForServiceWorkerToControlPage } from './waitForServiceWorkerToControlPage';
MotionGlobalConfig.skipAnimations = true;
Globals.assign({ skipAnimation: true });
@@ -15,6 +16,14 @@ disableCssAnimationsStyle.innerHTML = `*, *::before, *::after {
}`;
document.head.appendChild(disableCssAnimationsStyle);
+// msw registers its service worker while ./preview evaluates, and it reloads
+// any page that has a registration without being controlled by it, which
+// vitest reports as "the iframe was reloaded during a test" and which fails
+// the whole shard. Holding every file here until the worker controls its page
+// (including files that run zero tests, which otherwise complete instantly)
+// means no test iframe is ever created while the worker is still activating.
+await waitForServiceWorkerToControlPage();
+
// Pre-warm the dynamic imports used by lazy() story components so the
// modules are cached before any test runs (avoids flaky timeouts and Argos
// screenshots that capture a Suspense skeleton instead of the loaded content).
diff --git a/packages/twenty-front/.storybook/waitForServiceWorkerToControlPage.ts b/packages/twenty-front/.storybook/waitForServiceWorkerToControlPage.ts
new file mode 100644
index 00000000000..3d1f1c3167b
--- /dev/null
+++ b/packages/twenty-front/.storybook/waitForServiceWorkerToControlPage.ts
@@ -0,0 +1,38 @@
+import { isDefined } from 'twenty-shared/utils';
+
+// The worker takes tens of seconds to activate on CI, because its script
+// request queues behind the story module transforms. serviceWorker.ready is
+// used rather than getRegistration() because msw registers the worker
+// asynchronously while ./preview evaluates, so the registration may not exist
+// yet when this runs. The backstop covers a worker that never activates.
+const SERVICE_WORKER_CONTROL_BACKSTOP_MS = 60_000;
+
+export const waitForServiceWorkerToControlPage = async () => {
+ if (isDefined(navigator.serviceWorker.controller)) {
+ return;
+ }
+
+ const backstop = new Promise<void>((resolve) => {
+ setTimeout(resolve, SERVICE_WORKER_CONTROL_BACKSTOP_MS);
+ });
+
+ await Promise.race([
+ navigator.serviceWorker.ready.then(() => undefined),
+ backstop,
+ ]);
+
+ if (isDefined(navigator.serviceWorker.controller)) {
+ return;
+ }
+
+ await Promise.race([
+ new Promise<void>((resolve) => {
+ navigator.serviceWorker.addEventListener(
+ 'controllerchange',
+ () => resolve(),
+ { once: true },
+ );
+ }),
+ backstop,
+ ]);
+};