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.

fix(storybook): enforce configured async test imports (#111502)

posthog[bot]d842b946e416
diff --git a/.claude/commands/conventions.md b/.claude/commands/conventions.md
index b80ca3325973..b00d1ad87047 100644
--- a/.claude/commands/conventions.md
+++ b/.claude/commands/conventions.md
@@ -81,6 +81,7 @@ Hence the explicit separation between the data and view layers.
- Write [logic tests](https://keajs.org/docs/intro/testing) for all logic files.
- [react testing library](https://testing-library.com/docs/react-testing-library/intro/) tests are particularly useful for components with complex interactions or to guide future humans or agents when they're changing components without full context of the uses and edge cases
- Add all new presentational elements and scenes to [our storybook](https://storybook.posthog.net/). Run `pnpm storybook` locally.
+ - In web Storybook play functions, import `within` and `waitFor` from `@testing-library/dom`. The preview configures their async timeout; `storybook/test` uses a separate default.
---
diff --git a/.oxlintrc.json b/.oxlintrc.json
index 1fe96b0cb0c9..6e065c7655b6 100644
--- a/.oxlintrc.json
+++ b/.oxlintrc.json
@@ -128,6 +128,11 @@
{
"name": "storybook/preview-api",
"message": "The withKea decorator renders a story after Storybook clears its hook context, so Storybook preview hooks such as useArgs throw or never fire, and the story renders blank in visual regression runs. Keep story state in React's useState."
+ },
+ {
+ "name": "storybook/test",
+ "importNames": ["within", "waitFor"],
+ "message": "Import within and waitFor from @testing-library/dom to use the configured Storybook async timeout."
}
],
"patterns": [
diff --git a/common/storybook/.storybook/main.ts b/common/storybook/.storybook/main.ts
index 713ea28e470d..3a507dbd8f4d 100644
--- a/common/storybook/.storybook/main.ts
+++ b/common/storybook/.storybook/main.ts
@@ -30,6 +30,7 @@ const SINGLETON_PACKAGES = [
'kea-subscriptions',
'kea-waitfor',
'kea-window-values',
+ '@testing-library/dom',
]
const createStoriesPathFor = (storyPath: string): string => `../../../${storyPath}/**/*.stories.@(js|jsx|ts|tsx)`
diff --git a/docs/published/handbook/engineering/conventions/frontend-coding.md b/docs/published/handbook/engineering/conventions/frontend-coding.md
index 1af51705fa67..a9f95afa0c0b 100644
--- a/docs/published/handbook/engineering/conventions/frontend-coding.md
+++ b/docs/published/handbook/engineering/conventions/frontend-coding.md
@@ -73,5 +73,6 @@ Hence the explicit separation between the data and view layers.
- Write [logic tests](https://keajs.org/docs/intro/testing) for all logic files.
- [react testing library](https://testing-library.com/docs/react-testing-library/intro/) tests are particularly useful for components with complex interactions or to guide future humans or agents when they're changing components without full context of the uses and edge cases
- Add all new presentational elements and scenes to [our storybook](https://storybook.dev.posthog.dev/). Run `pnpm storybook` locally.
+ - In web Storybook play functions, import `within` and `waitFor` from `@testing-library/dom`. The preview configures their async timeout; `storybook/test` uses a separate default.
> Sync note: This file is also copied to posthog/posthog/.claude/commands/conventions.md for Claude Code. When updating this file, please also update the copy there.
diff --git a/frontend/src/scenes/data-warehouse/editor/SQLEditorScene.stories.tsx b/frontend/src/scenes/data-warehouse/editor/SQLEditorScene.stories.tsx
index dc37ec4465b0..4b6fbacaf5fb 100644
--- a/frontend/src/scenes/data-warehouse/editor/SQLEditorScene.stories.tsx
+++ b/frontend/src/scenes/data-warehouse/editor/SQLEditorScene.stories.tsx
@@ -1,4 +1,5 @@
import { Decorator, Meta, StoryObj } from '@storybook/react'
+import { waitFor, within } from '@testing-library/dom'
import { BindLogic } from 'kea'
import { delay } from 'msw'
import { useEffect, useRef } from 'react'
@@ -11,7 +12,7 @@ import { mswDecorator } from '~/mocks/browser'
import type { DataWarehouseSavedQuery } from '~/types'
import { AccessControlLevel, AccessControlResourceType, ChartDisplayType } from '~/types'
-import { expect, userEvent, waitFor, within } from 'storybook/test'
+import { expect, userEvent } from 'storybook/test'
import { BIConfig, BIField, buildBIQuery } from './bi/biEditorTypes'
import { QueryInfo } from './output-pane-tabs/QueryInfo'
@@ -304,7 +305,7 @@ export const EditedInsight: Story = {
await waitFor(() =>
expect(canvas.getByRole('button', { name: 'Discard changes' })).toHaveAttribute('aria-disabled', 'false')
)
- await expect(canvas.getByText('Edited')).toBeVisible()
+ await waitFor(() => expect(canvas.getByText('Edited')).toBeVisible())
await expect(canvas.getByRole('button', { name: 'Update insight' })).toHaveAttribute('aria-disabled', 'false')
await userEvent.click(canvasElement.querySelector('[data-attr="sql-editor-save-options-button"]')!)
const menu = within(canvasElement.ownerDocument.body)
diff --git a/frontend/src/scenes/notebooks/Notebook/NotebookJupyterMode.stories.tsx b/frontend/src/scenes/notebooks/Notebook/NotebookJupyterMode.stories.tsx
index 0d745cf1f051..ae883afb3434 100644
--- a/frontend/src/scenes/notebooks/Notebook/NotebookJupyterMode.stories.tsx
+++ b/frontend/src/scenes/notebooks/Notebook/NotebookJupyterMode.stories.tsx
@@ -1,4 +1,5 @@
import { Meta, StoryObj } from '@storybook/react'
+import { waitFor } from '@testing-library/dom'
import { FEATURE_FLAGS } from 'lib/constants'
import { App } from 'scenes/App'
@@ -6,8 +7,6 @@ import { urls } from 'scenes/urls'
import { mswDecorator } from '~/mocks/browser'
-import { waitFor } from 'storybook/test'
-
import { notebookTestTemplate } from './__mocks__/notebook-template-for-snapshot'
import { buildMarkdownNotebookContent, serializeMarkdownNotebookComponent } from './markdownNotebookV2'
import { notebookJupyterLogic } from './notebookJupyterLogic'
diff --git a/frontend/src/scenes/notebooks/Notebook/NotebookWidgetViews.stories.tsx b/frontend/src/scenes/notebooks/Notebook/NotebookWidgetViews.stories.tsx
index e16071cdd37a..dc8e8185b64e 100644
--- a/frontend/src/scenes/notebooks/Notebook/NotebookWidgetViews.stories.tsx
+++ b/frontend/src/scenes/notebooks/Notebook/NotebookWidgetViews.stories.tsx
@@ -1,6 +1,7 @@
import { MOCK_DEFAULT_BASIC_USER } from 'lib/api.mock'
import { Meta, StoryObj } from '@storybook/react'
+import { waitFor } from '@testing-library/dom'
import { HttpResponse } from 'msw'
import { FEATURE_FLAGS } from 'lib/constants'
@@ -35,7 +36,7 @@ import {
} from 'products/error_tracking/frontend/__mocks__/error_tracking_query'
import { NEW_WORKFLOW } from 'products/workflows/frontend/Workflows/workflowLogic'
-import { expect, waitFor } from 'storybook/test'
+import { expect } from 'storybook/test'
import featureFlags from '../../feature-flags/__mocks__/feature_flags.json'
import { notebookWidgetCatalog, NotebookWidgetTagName } from '../notebookWidgetCatalog'
diff --git a/frontend/src/scenes/terminal/TerminalScene.stories.tsx b/frontend/src/scenes/terminal/TerminalScene.stories.tsx
index 99bba197bb4c..1af50fac5d57 100644
--- a/frontend/src/scenes/terminal/TerminalScene.stories.tsx
+++ b/frontend/src/scenes/terminal/TerminalScene.stories.tsx
@@ -1,4 +1,5 @@
import type { Meta, StoryObj } from '@storybook/react'
+import { waitFor, within } from '@testing-library/dom'
import { useValues } from 'kea'
import { router } from 'kea-router'
@@ -15,7 +16,7 @@ import { ScenePanel } from '~/layout/scenes/SceneLayout'
import { useStorybookMocks } from '~/mocks/browser'
import { SidePanelTab } from '~/types'
-import { expect, spyOn, userEvent, waitFor, within } from 'storybook/test'
+import { expect, spyOn, userEvent } from 'storybook/test'
import { TerminalDock } from './TerminalDock'
import { terminalDockLogic } from './terminalDockLogic'
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 9056db2dd933..f9e78109211c 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -2775,6 +2775,9 @@ importers:
'@storybook/react':
specifier: 'catalog:'
+ '@testing-library/dom':
+ specifier: '*'
+ version: 10.4.0
'@types/react':
specifier: 18.3.27
version: 18.3.27
@@ -4053,6 +4056,9 @@ importers:
'@posthog/quill-charts':
specifier: workspace:*
version: link:../../packages/quill/packages/charts
+ '@testing-library/dom':
+ specifier: '*'
+ version: 10.4.0
'@types/react':
specifier: 18.3.27
version: 18.3.27
@@ -4254,6 +4260,9 @@ importers:
'@posthog/icons':
specifier: 'catalog:'
+ '@testing-library/dom':
+ specifier: '*'
+ version: 10.4.0
'@types/react':
specifier: 18.3.27
version: 18.3.27
@@ -4357,6 +4366,9 @@ importers:
'@storybook/react':
specifier: 'catalog:'
+ '@testing-library/dom':
+ specifier: '*'
+ version: 10.4.0
'@tanstack/react-virtual':
specifier: '*'
@@ -4704,6 +4716,9 @@ importers:
'@posthog/icons':
specifier: 'catalog:'
+ '@testing-library/dom':
+ specifier: '*'
+ version: 10.4.0
'@types/react':
specifier: 18.3.27
version: 18.3.27
diff --git a/products/canvas/frontend/scene/CanvasScene.stories.tsx b/products/canvas/frontend/scene/CanvasScene.stories.tsx
index e561414a928d..b3dc4ac89952 100644
--- a/products/canvas/frontend/scene/CanvasScene.stories.tsx
+++ b/products/canvas/frontend/scene/CanvasScene.stories.tsx
@@ -1,4 +1,5 @@
import { Meta, StoryObj } from '@storybook/react'
+import { waitFor } from '@testing-library/dom'
import { FEATURE_FLAGS } from 'lib/constants'
import { App } from 'scenes/App'
@@ -6,7 +7,7 @@ import { urls } from 'scenes/urls'
import { mswDecorator } from '~/mocks/browser'
-import { expect, userEvent, waitFor } from 'storybook/test'
+import { expect, userEvent } from 'storybook/test'
import type { CanvasApi, CanvasBuildApi, CanvasVersionApi, CanvasViewResponseApi } from '../generated/api.schemas'
diff --git a/products/canvas/package.json b/products/canvas/package.json
index b5ecac34ae2e..055514156d02 100644
--- a/products/canvas/package.json
+++ b/products/canvas/package.json
@@ -20,6 +20,7 @@
"@posthog/icons": "catalog:",
"@posthog/lemon-ui": "*",
"@storybook/react": "catalog:",
+ "@testing-library/dom": "*",
"@types/react": "*",
"@types/react-dom": "*",
"react": "*",
diff --git a/products/marketing_analytics/frontend/components/AddIntegrationButton.stories.tsx b/products/marketing_analytics/frontend/components/AddIntegrationButton.stories.tsx
index b9ebaed08884..f9c365d3b8bd 100644
--- a/products/marketing_analytics/frontend/components/AddIntegrationButton.stories.tsx
+++ b/products/marketing_analytics/frontend/components/AddIntegrationButton.stories.tsx
@@ -1,6 +1,7 @@
import { MOCK_DEFAULT_USER } from 'lib/api.mock'
import type { Meta, StoryObj } from '@storybook/react'
+import { waitFor, within } from '@testing-library/dom'
import { useValues } from 'kea'
import { useEffect } from 'react'
@@ -10,7 +11,7 @@ import { userLogic } from 'scenes/userLogic'
import { mswDecorator } from '~/mocks/browser'
-import { expect, userEvent, waitFor, within } from 'storybook/test'
+import { expect, userEvent } from 'storybook/test'
import { AddIntegrationButton } from './AddIntegrationButton'
import { NEW_AD_SOURCES_SEEN_KEY } from './newAdSourcesLogic'
diff --git a/products/marketing_analytics/package.json b/products/marketing_analytics/package.json
index 22d13e290a2d..e2992c47e3fa 100644
--- a/products/marketing_analytics/package.json
+++ b/products/marketing_analytics/package.json
@@ -11,6 +11,7 @@
"peerDependencies": {
"@posthog/brand": "catalog:",
"@posthog/icons": "catalog:",
+ "@testing-library/dom": "*",
"@types/react": "*",
"clsx": "*",
"react": "*"
diff --git a/products/notebooks/frontend/NotebookDashboardWidget/NotebookDashboardWidget.stories.tsx b/products/notebooks/frontend/NotebookDashboardWidget/NotebookDashboardWidget.stories.tsx
index ef17d4dcb92c..1c0b60fca1e8 100644
--- a/products/notebooks/frontend/NotebookDashboardWidget/NotebookDashboardWidget.stories.tsx
+++ b/products/notebooks/frontend/NotebookDashboardWidget/NotebookDashboardWidget.stories.tsx
@@ -1,4 +1,5 @@
import type { Meta, StoryObj } from '@storybook/react'
+import { waitFor, within } from '@testing-library/dom'
import { useActions } from 'kea'
import { LemonButton } from '@posthog/lemon-ui'
@@ -8,7 +9,7 @@ import { AccessControlLevel, DashboardBasicType, DashboardPlacement, DashboardTi
import { DashboardWidgetItem } from 'products/dashboards/frontend/components/DashboardWidgetItem/DashboardWidgetItem'
-import { expect, userEvent, waitFor, within } from 'storybook/test'
+import { expect, userEvent } from 'storybook/test'
import { notebookWidgetDashboardLogic } from '../NotebookNodeGeneratedWidget/notebookWidgetDashboardLogic'
import { NotebookWidgetDashboardModal } from '../NotebookNodeGeneratedWidget/NotebookWidgetDashboardModal'
diff --git a/products/notebooks/package.json b/products/notebooks/package.json
index 19a68b1cc8f9..55f18f110292 100644
--- a/products/notebooks/package.json
+++ b/products/notebooks/package.json
@@ -17,6 +17,7 @@
"peerDependencies": {
"@posthog/brand": "catalog:",
"@posthog/icons": "catalog:",
+ "@testing-library/dom": "*",
"@types/react": "*",
"@types/react-dom": "*",
"clsx": "*",
diff --git a/products/posthog_ai/frontend/scenes/TaskTracker/TaskRunArtifacts.stories.tsx b/products/posthog_ai/frontend/scenes/TaskTracker/TaskRunArtifacts.stories.tsx
index 8a2fde3632ff..791524923402 100644
--- a/products/posthog_ai/frontend/scenes/TaskTracker/TaskRunArtifacts.stories.tsx
+++ b/products/posthog_ai/frontend/scenes/TaskTracker/TaskRunArtifacts.stories.tsx
@@ -1,4 +1,5 @@
import type { Meta, StoryObj } from '@storybook/react'
+import { waitFor } from '@testing-library/dom'
import { useActions, useValues } from 'kea'
import { HttpResponse } from 'msw'
import { ReactNode, useEffect } from 'react'
@@ -19,7 +20,7 @@ import type {
} from 'products/tasks/frontend/generated/api.schemas'
import { TaskRuntimeEnumApi } from 'products/tasks/frontend/generated/api.schemas'
-import { expect, userEvent, waitFor } from 'storybook/test'
+import { expect, userEvent } from 'storybook/test'
import { OriginProduct, Task, TaskRun, TaskRunEnvironment, TaskRunStatus } from '../../types/taskTypes'
import { TaskDetailPage } from './components/TaskDetailPage'
diff --git a/products/posthog_ai/package.json b/products/posthog_ai/package.json
index eefeaa837d67..dcd546722d66 100644
--- a/products/posthog_ai/package.json
+++ b/products/posthog_ai/package.json
@@ -23,6 +23,7 @@
"peerDependencies": {
"@posthog/icons": "catalog:",
"@storybook/react": "catalog:",
+ "@testing-library/dom": "*",
"@types/react": "*",
"@types/react-dom": "*",
"clsx": "*",
diff --git a/products/replay_vision/frontend/components/PlayerSidebarObservationsTab.stories.tsx b/products/replay_vision/frontend/components/PlayerSidebarObservationsTab.stories.tsx
index b5ce466643d7..5c52786567b4 100644
--- a/products/replay_vision/frontend/components/PlayerSidebarObservationsTab.stories.tsx
+++ b/products/replay_vision/frontend/components/PlayerSidebarObservationsTab.stories.tsx
@@ -1,4 +1,5 @@
import { Meta, StoryObj } from '@storybook/react'
+import { waitFor } from '@testing-library/dom'
import { combineUrl } from 'kea-router'
import { App } from 'scenes/App'
@@ -12,8 +13,6 @@ import { mswDecorator } from '~/mocks/browser'
import { sessionFrameResponse } from '~/mocks/fixtures/sessionFrame'
import { SessionRecordingSidebarTab } from '~/types'
-import { waitFor } from 'storybook/test'
-
import {
MEDIUM,
MEDIUM_INACTIVE,
diff --git a/products/replay_vision/frontend/replay_scanners/ReplayScannersScene.stories.tsx b/products/replay_vision/frontend/replay_scanners/ReplayScannersScene.stories.tsx
index 48e29196091f..d0a3aab96e80 100644
--- a/products/replay_vision/frontend/replay_scanners/ReplayScannersScene.stories.tsx
+++ b/products/replay_vision/frontend/replay_scanners/ReplayScannersScene.stories.tsx
@@ -1,5 +1,5 @@
import type { Meta, StoryObj } from '@storybook/react'
-import { within } from '@testing-library/dom'
+import { waitFor, within } from '@testing-library/dom'
import userEvent from '@testing-library/user-event'
import { FEATURE_FLAGS } from 'lib/constants'
@@ -15,6 +15,8 @@ import { sessionFrameResponse } from '~/mocks/fixtures/sessionFrame'
import { RecordingsQuery } from '~/queries/schema/schema-general'
import { StartupProgramLabel } from '~/types'
+import { expect } from 'storybook/test'
+
import { LONG, LONG_INACTIVE, summary as timelineSummary } from '../__mocks__/recordingTimelineObservations'
import type {
BackfillEstimateResponseApi,
@@ -1242,7 +1244,12 @@ const failedObservationDetail = observation({
scanner_result: null,
})
-export const ObservationDetailFailed: StoryObj = observationDetailStory(failedObservationDetail)
+export const ObservationDetailFailed: StoryObj = {
+ ...observationDetailStory(failedObservationDetail),
+ play: async ({ canvasElement }) => {
+ await waitFor(() => expect(canvasElement.querySelector('[data-attr="recording-play"]')).toBeVisible())
+ },
+}
// The session had no screen data to watch, so no model ran and a later retry may still succeed.
const notScannedObservationDetail = observation({
diff --git a/products/review_hog/frontend/CodeReviewScene.stories.tsx b/products/review_hog/frontend/CodeReviewScene.stories.tsx
index 92110ba3ce73..f65f6d0dd555 100644
--- a/products/review_hog/frontend/CodeReviewScene.stories.tsx
+++ b/products/review_hog/frontend/CodeReviewScene.stories.tsx
@@ -1,6 +1,7 @@
import { MOCK_DEFAULT_USER } from 'lib/api.mock'
import { Meta, StoryObj } from '@storybook/react'
+import { waitFor, within } from '@testing-library/dom'
import { FEATURE_FLAGS } from 'lib/constants'
import { userLogic } from 'scenes/userLogic'
@@ -12,7 +13,7 @@ import type {
ReviewUserSettingsApi,
} from 'products/review_hog/frontend/generated/api.schemas'
-import { expect, waitFor, within } from 'storybook/test'
+import { expect } from 'storybook/test'
import { CodeReviewScene } from './CodeReviewScene'
diff --git a/products/review_hog/package.json b/products/review_hog/package.json
index 42eae7e332f6..d5bd9abaa07c 100644
--- a/products/review_hog/package.json
+++ b/products/review_hog/package.json
@@ -13,6 +13,7 @@
},
"peerDependencies": {
"@posthog/icons": "catalog:",
+ "@testing-library/dom": "*",
"@types/react": "*",
"react": "*"
}
diff --git a/products/stamphog/frontend/scenes/StamphogDigestsScene/StamphogDigestsScene.stories.tsx b/products/stamphog/frontend/scenes/StamphogDigestsScene/StamphogDigestsScene.stories.tsx
index cac78f291163..50ae65204d8c 100644
--- a/products/stamphog/frontend/scenes/StamphogDigestsScene/StamphogDigestsScene.stories.tsx
+++ b/products/stamphog/frontend/scenes/StamphogDigestsScene/StamphogDigestsScene.stories.tsx
@@ -1,11 +1,12 @@
import type { Meta, StoryObj } from '@storybook/react'
+import { waitFor } from '@testing-library/dom'
import { App } from 'scenes/App'
import { urls } from 'scenes/urls'
import { mswDecorator } from '~/mocks/browser'
-import { expect, userEvent, waitFor } from 'storybook/test'
+import { expect, userEvent } from 'storybook/test'
import type { DigestRunApi } from '../../generated/api.schemas'