Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(storybook): restore capture waits and two jest workers per shard (#130782)

Julian Beza4cf02859580
diff --git a/common/storybook/.storybook/test-runner.ts b/common/storybook/.storybook/test-runner.ts
index 48f4beed1ec0..4c424cbfc8ce 100644
--- a/common/storybook/.storybook/test-runner.ts
+++ b/common/storybook/.storybook/test-runner.ts
@@ -41,9 +41,6 @@ console.log('[test-runner] Storybook snapshots will be saved to', customSnapshot
const JEST_TIMEOUT_MS = 60000 // Multi-viewport snapshots can take substantially longer in CI
const PLAYWRIGHT_TIMEOUT_MS = 10000 // Must be shorter than JEST_TIMEOUT_MS
const VIEWPORT_SETTLE_TIMEOUT_MS = 5000
-const QUIET_WINDOW_MS = 250
-const RESIZE_SETTLE_TIMEOUT_MS = 300
-const CONTENT_SETTLE_TIMEOUT_MS = 1300
// Each story file gets a fresh browser context, so `prepare` reloads the whole preview bundle with a
// cold HTTP cache. Under CI contention that tail overruns Playwright's 30s default. RETRY_TIMES can't
@@ -87,43 +84,6 @@ export default {
// making the marker visible from the very first module evaluation.
async prepare({ page, browserContext, testRunnerConfig }) {
await page.addInitScript(() => {
- // A pending timer or request can still change the page, so waitForQuiet waits for them.
- const pendingTimers = new Map<number, number>()
- let pendingFetches = 0
- const untrackedSetTimeout = window.setTimeout.bind(window)
- const untrackedClearTimeout = window.clearTimeout.bind(window)
- const untrackedFetch = window.fetch.bind(window)
- window.setTimeout = ((handler: TimerHandler, timeout?: number, ...args: unknown[]) => {
- if (typeof handler !== 'function') {
- return untrackedSetTimeout(handler, timeout, ...args)
- }
- const id = untrackedSetTimeout(
- (...callbackArgs: unknown[]) => {
- pendingTimers.delete(id)
- handler(...callbackArgs)
- },
- timeout,
- ...args
- )
- pendingTimers.set(id, performance.now() + (Number(timeout) || 0))
- return id
- }) as typeof window.setTimeout
- window.clearTimeout = ((id?: number) => {
- if (id !== undefined) {
- pendingTimers.delete(id)
- }
- untrackedClearTimeout(id)
- }) as typeof window.clearTimeout
- window.fetch = (...fetchArgs: Parameters<typeof fetch>) => {
- pendingFetches++
- return untrackedFetch(...fetchArgs).finally(() => pendingFetches--)
- }
- ;(window as unknown as { __testRunnerWork: unknown }).__testRunnerWork = {
- pendingBefore: (time: number): boolean =>
- pendingFetches > 0 || Array.from(pendingTimers.values()).some((due) => due <= time),
- setTimeout: untrackedSetTimeout,
- }
-
const patchedUserAgent = `${navigator.userAgent} StorybookTestRunner`
Object.defineProperty(navigator, 'userAgent', {
get: () => patchedUserAgent,
@@ -431,7 +391,7 @@ async function expectStoryToMatchSnapshot(
window.dispatchEvent(new Event('resize'))
})
// Allow ResizeObserver callbacks to fire and React to re-render with updated dimensions
- await waitForQuiet(page, RESIZE_SETTLE_TIMEOUT_MS)
+ await page.waitForTimeout(300)
const {
waitForLoadersToDisappear = true,
@@ -481,62 +441,6 @@ async function waitForPossiblyFramedSelector(page: Page, selector: string, timeo
await page.frameLocator(frameSelector.trim()).locator(innerSelector.trim()).first().waitFor({ timeout })
}
-/** Resolves when the DOM is quiet for QUIET_WINDOW_MS and no timer or request is due before `timeout`, or at `timeout`. */
-async function waitForQuiet(page: Page, timeout: number): Promise<void> {
- await page
- .evaluate(
- ([quietWindowMs, timeoutMs]) =>
- new Promise<void>((resolve) => {
- const work = (
- window as unknown as {
- __testRunnerWork?: {
- pendingBefore: (time: number) => boolean
- setTimeout: typeof setTimeout
- }
- }
- ).__testRunnerWork
- const deadlineAt = performance.now() + timeoutMs
- let lastChange = performance.now()
- let lastSize = ''
- const observer = new MutationObserver(() => {
- lastChange = performance.now()
- })
- let resolved = false
- const finish = (): void => {
- if (!resolved) {
- resolved = true
- observer.disconnect()
- resolve()
- }
- }
- ;(work?.setTimeout ?? setTimeout)(finish, timeoutMs)
- observer.observe(document.documentElement, {
- subtree: true,
- childList: true,
- attributes: true,
- characterData: true,
- })
- const check = (): void => {
- const size = `${document.body.scrollWidth}:${document.body.scrollHeight}`
- if (size !== lastSize) {
- lastSize = size
- lastChange = performance.now()
- }
- if (performance.now() - lastChange >= quietWindowMs && !work?.pendingBefore(deadlineAt)) {
- finish()
- return
- }
- if (!resolved) {
- requestAnimationFrame(check)
- }
- }
- requestAnimationFrame(check)
- }),
- [QUIET_WINDOW_MS, timeout]
- )
- .catch(() => undefined)
-}
-
/** A resize wipes a quill chart canvas before its repaint, and the body-size check cannot see a chart resizing inside a fixed-height card. */
async function waitForChartCanvasesPainted(page: Page): Promise<void> {
await page
@@ -657,7 +561,45 @@ async function takeSnapshotWithTheme(
}
})
- await waitForQuiet(page, CONTENT_SETTLE_TIMEOUT_MS)
+ // wait for content to stabilize - detects when DOM stops changing
+ await page
+ .waitForFunction(
+ () => {
+ return new Promise<boolean>((resolve) => {
+ let lastHeight = document.body.scrollHeight
+ let lastWidth = document.body.scrollWidth
+ let stableCount = 0
+
+ const checkStability = (): void => {
+ const currentHeight = document.body.scrollHeight
+ const currentWidth = document.body.scrollWidth
+
+ if (currentHeight === lastHeight && currentWidth === lastWidth) {
+ stableCount++
+ if (stableCount >= 3) {
+ resolve(true)
+ return
+ }
+ } else {
+ stableCount = 0
+ lastHeight = currentHeight
+ lastWidth = currentWidth
+ }
+
+ setTimeout(checkStability, 100)
+ }
+
+ checkStability()
+ })
+ },
+ { timeout: 3000 }
+ )
+ .catch(() => {
+ // if content keeps changing, that's okay - we'll proceed anyway
+ })
+
+ // final wait for any remaining renders
+ await page.waitForTimeout(1000)
await waitForChartCanvasesPainted(page)
diff --git a/common/storybook/package.json b/common/storybook/package.json
index 38ab11babf85..1341477cefdf 100644
--- a/common/storybook/package.json
+++ b/common/storybook/package.json
@@ -5,8 +5,8 @@
"build:workers": "cd ../.. && node frontend/bin/watch-workers.mjs --outdir common/storybook/.workers",
"build": "pnpm build:products && pnpm build:workers && DEBUG=0 NODE_OPTIONS=--max-old-space-size=8192 storybook build -o dist",
"start": "pnpm build:products && pnpm build:workers && DEBUG=0 NODE_OPTIONS=--max-old-space-size=8192 storybook dev -p 6006 --no-open",
- "test:visual:ci:update": "test-storybook -u --junit --no-index-json --maxWorkers=3",
- "test:visual:ci:verify": "test-storybook --ci --junit --no-index-json --maxWorkers=3",
+ "test:visual:ci:update": "test-storybook -u --junit --no-index-json --maxWorkers=2",
+ "test:visual:ci:verify": "test-storybook --ci --junit --no-index-json --maxWorkers=2",
"test:visual:debug": "pnpm build:products && PWDEBUG=1 NODE_OPTIONS=--max-old-space-size=16384 test-storybook --browsers chromium webkit --no-index-json",
"test:visual:update": "pnpm build:products && cd ../.. && rm -rf frontend/__snapshots__/__failures__/ && docker compose -f docker-compose.playwright.yml run --rm -it --build playwright pnpm test:visual:update:docker --url http://host.docker.internal:6006",
"test:visual:update:docker": "pnpm build:products && NODE_OPTIONS=--max-old-space-size=16384 test-storybook -u --browsers chromium webkit --no-index-json"