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): wait for a quiet page instead of fixed sleeps in snapshots (#128668)

Frank Hamand9943054b6b59
diff --git a/common/storybook/.storybook/test-runner.ts b/common/storybook/.storybook/test-runner.ts
index 95171145c88c..af53b881bba5 100644
--- a/common/storybook/.storybook/test-runner.ts
+++ b/common/storybook/.storybook/test-runner.ts
@@ -41,6 +41,9 @@ 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
@@ -84,6 +87,43 @@ 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,
@@ -391,7 +431,7 @@ async function expectStoryToMatchSnapshot(
window.dispatchEvent(new Event('resize'))
})
// Allow ResizeObserver callbacks to fire and React to re-render with updated dimensions
- await page.waitForTimeout(300)
+ await waitForQuiet(page, RESIZE_SETTLE_TIMEOUT_MS)
const {
waitForLoadersToDisappear = true,
@@ -441,6 +481,62 @@ 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
@@ -568,45 +664,7 @@ async function takeSnapshotWithTheme(
})
})
- // 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 waitForQuiet(page, CONTENT_SETTLE_TIMEOUT_MS)
await waitForChartCanvasesPainted(page)
diff --git a/frontend/src/scenes/session-recordings/playlist/sessionRecordingsPlaylistLogic.test.ts b/frontend/src/scenes/session-recordings/playlist/sessionRecordingsPlaylistLogic.test.ts
index dcc5218d3b1c..6c016a71ce95 100644
--- a/frontend/src/scenes/session-recordings/playlist/sessionRecordingsPlaylistLogic.test.ts
+++ b/frontend/src/scenes/session-recordings/playlist/sessionRecordingsPlaylistLogic.test.ts
@@ -172,8 +172,6 @@ describe('sessionRecordingsPlaylistLogic', () => {
})
initKeaTests()
featureFlagLogic.mount()
- // The list memo is module state, so it survives the kea context these tests reset
- clearMemoizedListResponses()
})
afterEach(() => {
@@ -2672,7 +2670,21 @@ describe('sessionRecordingsPlaylistLogic', () => {
// An embedded playlist unmounts with its tab, so most repeats arrive on a fresh logic
// instance. The memo has to outlive the one that issued the read.
- it('reads once when a remount inside the memo window repeats the request', async () => {
+ it.each([
+ { when: 'a remount inside the memo window', elapsedMs: LIST_MEMO_WINDOW_MS - 1, resetKea: false, reads: 1 },
+ {
+ when: 'a remount the memo window no longer covers',
+ elapsedMs: LIST_MEMO_WINDOW_MS + 1,
+ resetKea: false,
+ reads: 2,
+ },
+ {
+ when: 'a reset kea context inside the memo window',
+ elapsedMs: LIST_MEMO_WINDOW_MS - 1,
+ resetKea: true,
+ reads: 2,
+ },
+ ])('reads $reads time(s) when $when repeats the request', async ({ elapsedMs, resetKea, reads }) => {
const advanceClock = mockClock()
const listSpy = jest
.spyOn(api.recordings, 'list')
@@ -2685,40 +2697,21 @@ describe('sessionRecordingsPlaylistLogic', () => {
await expectLogic(firstMount).toDispatchActions(['loadSessionRecordingsSuccess']).toFinishAllListeners()
firstMount.unmount()
- advanceClock(LIST_MEMO_WINDOW_MS - 1)
+ advanceClock(elapsedMs)
+ if (resetKea) {
+ initKeaTests()
+ featureFlagLogic.mount()
+ }
const secondMount = sessionRecordingsPlaylistLogic({ logicKey: 'dedupe-remount' })
secondMount.mount()
await expectLogic(secondMount).toDispatchActions(['loadSessionRecordingsSuccess']).toFinishAllListeners()
- expect(listSpy).toHaveBeenCalledTimes(1)
+ expect(listSpy).toHaveBeenCalledTimes(reads)
expect(secondMount.values.sessionRecordings).toEqual([aRecording])
secondMount.unmount()
})
- it('reads again when a remount repeats a request the memo window no longer covers', async () => {
- const advanceClock = mockClock()
- const listSpy = jest
- .spyOn(api.recordings, 'list')
- .mockImplementation(
- () => Promise.resolve(listResponse as unknown) as ReturnType<typeof api.recordings.list>
- )
-
- const firstMount = sessionRecordingsPlaylistLogic({ logicKey: 'dedupe-remount-expired' })
- firstMount.mount()
- await expectLogic(firstMount).toDispatchActions(['loadSessionRecordingsSuccess']).toFinishAllListeners()
- firstMount.unmount()
-
- advanceClock(LIST_MEMO_WINDOW_MS + 1)
- const secondMount = sessionRecordingsPlaylistLogic({ logicKey: 'dedupe-remount-expired' })
- secondMount.mount()
- await expectLogic(secondMount).toDispatchActions(['loadSessionRecordingsSuccess']).toFinishAllListeners()
-
- expect(listSpy).toHaveBeenCalledTimes(2)
-
- secondMount.unmount()
- })
-
it('reads again for a changed filter inside the memo window', async () => {
const advanceClock = mockClock()
const listSpy = jest
diff --git a/frontend/src/scenes/session-recordings/playlist/sessionRecordingsPlaylistLogic.ts b/frontend/src/scenes/session-recordings/playlist/sessionRecordingsPlaylistLogic.ts
index c1d2fe1b42ac..48f9c633e9c3 100644
--- a/frontend/src/scenes/session-recordings/playlist/sessionRecordingsPlaylistLogic.ts
+++ b/frontend/src/scenes/session-recordings/playlist/sessionRecordingsPlaylistLogic.ts
@@ -4,6 +4,7 @@ import {
actions,
afterMount,
connect,
+ getContext,
kea,
key,
listeners,
@@ -185,26 +186,41 @@ export const LIST_MEMO_WINDOW_MS = 3000
* holds it, so most repeats arrive on a fresh logic instance and a per-instance memo would miss
* them. The team id is part of the key because the request parameters do not carry it, and a team
* switch must never answer from another team's rows.
+ *
+ * It does not outlive the kea context. Tests and stories reset the context between cases, and a
+ * response that one case mocked must not answer the next case.
*/
-const landedListResponses = new Map<string, { response: RecordingsQueryResponse; completedAt: number }>()
+type LandedListResponses = Map<string, { response: RecordingsQueryResponse; completedAt: number }>
+
+const landedListResponsesByContext = new WeakMap<object, LandedListResponses>()
+
+const landedListResponses = (): LandedListResponses => {
+ const context = getContext()
+ let landed = landedListResponsesByContext.get(context)
+ if (!landed) {
+ landed = new Map()
+ landedListResponsesByContext.set(context, landed)
+ }
+ return landed
+}
const memoizedListResponse = (memoKey: string): RecordingsQueryResponse | undefined => {
- const landed = landedListResponses.get(memoKey)
+ const landed = landedListResponses().get(memoKey)
return landed && performance.now() - landed.completedAt < LIST_MEMO_WINDOW_MS ? landed.response : undefined
}
const memoizeListResponse = (memoKey: string, response: RecordingsQueryResponse, completedAt: number): void => {
// Drop what the window no longer covers, so a long session cannot grow the map.
- for (const [key, landed] of landedListResponses) {
+ const landedResponses = landedListResponses()
+ for (const [key, landed] of landedResponses) {
if (completedAt - landed.completedAt >= LIST_MEMO_WINDOW_MS) {
- landedListResponses.delete(key)
+ landedResponses.delete(key)
}
}
- landedListResponses.set(memoKey, { response, completedAt })
+ landedResponses.set(memoKey, { response, completedAt })
}
-/** The memo outlives a kea context, so a test has to start from an empty one. */
-export const clearMemoizedListResponses = (): void => landedListResponses.clear()
+export const clearMemoizedListResponses = (): void => landedListResponses().clear()
const getDefaultFilterTestAccounts = (): boolean => {
const stored = localStorage.getItem('default_filter_test_accounts')