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(insights): fix flaky chart test timeouts from deferred module load (#74407)

posthog[bot]36e70aa11564
diff --git a/frontend/src/test/insight-testing/interactions.ts b/frontend/src/test/insight-testing/interactions.ts
index e97a292b1c09..dbf6d72c5990 100644
--- a/frontend/src/test/insight-testing/interactions.ts
+++ b/frontend/src/test/insight-testing/interactions.ts
@@ -23,6 +23,22 @@ import { type InsightTooltipAccessor, createInsightTooltipAccessor } from './too
const DEBOUNCE_TIMEOUT = 3000
+/* Budget for one chart interaction, shared between its two waits (canvas render, then
+ * tooltip poll) rather than given to each in full. Two full DEBOUNCE_TIMEOUTs chained
+ * together outlast Jest's 5s per-test budget, so on a loaded shard the test dies before
+ * either wait reaches its own deadline, reporting "Exceeded timeout of 5000 ms" against
+ * the `it(` line instead of naming the phase that never completed. */
+const CHART_INTERACTION_TIMEOUT = 4000
+
+/** Milliseconds left until `deadline`, floored so the wait still gets one poll. */
+const budgetUntil = (deadline: number): number => Math.max(50, deadline - Date.now())
+
+/** The chart's event-handling wrapper, once its canvas has rendered. */
+async function findChartWrapper(deadline: number): Promise<HTMLElement> {
+ const canvas = await screen.findByLabelText(/chart with/i, {}, { timeout: budgetUntil(deadline) })
+ return canvas.parentElement!
+}
+
function getLogic(): ReturnType<typeof insightVizDataLogic.build> {
const props: InsightLogicProps = { dashboardItemId: INSIGHT_TEST_ID }
return insightVizDataLogic(props)
@@ -150,15 +166,15 @@ export const chart = {
index: number,
totalLabels = trendsSeries.pageviews.labels.length
): Promise<InsightTooltipAccessor> {
- const canvas = await screen.findByLabelText(/chart with/i, {}, { timeout: DEBOUNCE_TIMEOUT })
- const wrapper = canvas.parentElement!
- const tooltip = await hoverUntilTooltip(wrapper, index, totalLabels)
+ const deadline = Date.now() + CHART_INTERACTION_TIMEOUT
+ const wrapper = await findChartWrapper(deadline)
+ const tooltip = await hoverUntilTooltip(wrapper, index, totalLabels, budgetUntil(deadline))
return createInsightTooltipAccessor(tooltip)
},
async clickAtIndex(index: number, totalLabels = trendsSeries.pageviews.labels.length): Promise<void> {
- const canvas = await screen.findByLabelText(/chart with/i, {}, { timeout: DEBOUNCE_TIMEOUT })
- const wrapper = canvas.parentElement!
- await clickAtIndex(wrapper, index, totalLabels)
+ const deadline = Date.now() + CHART_INTERACTION_TIMEOUT
+ const wrapper = await findChartWrapper(deadline)
+ await clickAtIndex(wrapper, index, totalLabels, budgetUntil(deadline))
},
/** Click a row inside the pinned tooltip by matching its label text. Use
* after `clickAtIndex` has pinned a multi-series tooltip. */
@@ -178,9 +194,9 @@ export const chart = {
* `totalLabels` (the x-axis label count) is required: there's no canonical default series. */
export const sqlChart = {
async hoverTooltip(index: number, totalLabels: number): Promise<DefaultTooltipAccessor> {
- const canvas = await screen.findByLabelText(/chart with/i, {}, { timeout: DEBOUNCE_TIMEOUT })
- const wrapper = canvas.parentElement!
- const tooltip = await hoverUntilTooltip(wrapper, index, totalLabels)
+ const deadline = Date.now() + CHART_INTERACTION_TIMEOUT
+ const wrapper = await findChartWrapper(deadline)
+ const tooltip = await hoverUntilTooltip(wrapper, index, totalLabels, budgetUntil(deadline))
return createDefaultTooltipAccessor(tooltip)
},
}
diff --git a/frontend/src/test/insight-testing/render-insight.tsx b/frontend/src/test/insight-testing/render-insight.tsx
index 2e2dd25ae69e..a76276d8157d 100644
--- a/frontend/src/test/insight-testing/render-insight.tsx
+++ b/frontend/src/test/insight-testing/render-insight.tsx
@@ -16,6 +16,15 @@ import { setupInsightMocks, type SetupMocksOptions } from './mocks'
export const INSIGHT_TEST_KEY = 'test-harness'
export const INSIGHT_TEST_ID = `new-AdHoc.InsightViz.${INSIGHT_TEST_KEY}`
+// Loaded with `require` rather than a static import because it has to run after this
+// module's own imports are bound, or the circular dependency through InsightViz leaves the
+// binding undefined. Keep the call at module scope: it transforms and evaluates the entire
+// InsightViz graph on first use, costing ~1.9s against a cold Jest transform cache (which
+// CI always has, since the jest job restores no cache). From inside a render body that
+// cost lands on whichever test renders first and consumes most of Jest's 5s test budget.
+// eslint-disable-next-line @typescript-eslint/no-var-requires
+const { InsightViz } = require('~/queries/nodes/InsightViz/InsightViz')
+
export type InsightQuery = TrendsQuery | FunnelsQuery | StickinessQuery
export function buildTrendsQuery(overrides?: Partial<TrendsQuery>): TrendsQuery {
@@ -111,11 +120,6 @@ function InsightWrapper({
full: showFilters,
})
- // Dynamic require to break a circular-dependency cycle that causes Jest to fail
- // with static imports. Node's module cache means this is only resolved once.
- // eslint-disable-next-line @typescript-eslint/no-var-requires
- const { InsightViz } = require('~/queries/nodes/InsightViz/InsightViz')
-
return (
<InsightViz
uniqueKey={INSIGHT_TEST_KEY}
diff --git a/packages/quill/packages/charts/AGENTS.md b/packages/quill/packages/charts/AGENTS.md
index 36cee3a0bde2..865bf084f3b2 100644
--- a/packages/quill/packages/charts/AGENTS.md
+++ b/packages/quill/packages/charts/AGENTS.md
@@ -103,6 +103,7 @@ Import test helpers from `@posthog/quill-charts/testing` (jsdom-only).
- `getHogChart(scope?)` reads rendered overlays via stable `data-attr` hooks — `yTicks()`, `xTicks()`, `valueLabels()`, `referenceLines()`, axis titles, `seriesCount`, etc. Its `waitForTooltip()` returns a structured `TooltipSnapshot` but only when the chart was mounted via `renderHogChart` (it needs the captured tooltip context). `canvas` is the static (data) canvas, for asserting the backing store or dispatching a canvas-level event — use it instead of `container.querySelector('canvas')`.
- For a chart rendered outside `renderHogChart` (e.g. a real product scene), read the tooltip from the DOM: `getHogChartTooltip()` / `waitForHogChartTooltip()` return the portal element; `createHogChartTooltip(el)` wraps it as `{ element, isPinned }`. If the chart renders the built-in `DefaultTooltip`, use `createDefaultTooltipAccessor(el)` for `label()`, `rows()`, `value(seriesLabel)`, `swatchColors()`, and `total()` — it reads `DefaultTooltip`'s `data-attr="hog-chart-tooltip-*"` hooks (a stable testing contract; keep the accessor and the component's attrs in sync).
+- `hoverUntilTooltip(wrapper, index, totalLabels, timeout?)` and `clickAtIndex(wrapper, index, totalLabels, timeout?)` re-dispatch the hover until the tooltip portal mounts, since the chart drops events fired before it commits its scales. Both default to a 3000ms budget. Pass an explicit `timeout` whenever the call sits behind another wait in the same test (typically waiting for the canvas to render): two full-length waits chained together exceed Jest's 5000ms per-test budget, so the test dies before either helper can report which phase never completed. Give the pair one shared deadline and hand each the time remaining.
## Maintenance
diff --git a/packages/quill/packages/charts/src/testing/interactions.ts b/packages/quill/packages/charts/src/testing/interactions.ts
index b1436ad5d55d..2c6e193edd39 100644
--- a/packages/quill/packages/charts/src/testing/interactions.ts
+++ b/packages/quill/packages/charts/src/testing/interactions.ts
@@ -26,10 +26,15 @@ export async function hoverUntilTooltip(
return waitForHogChartTooltip(timeout, () => hoverAtIndex(wrapper, index, totalLabels))
}
-export async function clickAtIndex(wrapper: HTMLElement, index: number, totalLabels: number): Promise<void> {
+export async function clickAtIndex(
+ wrapper: HTMLElement,
+ index: number,
+ totalLabels: number,
+ timeout?: number
+): Promise<void> {
// Re-hover until the tooltip flushes — onClick reads tooltipCtx synchronously to decide
// between pinning and onPointClick, and a stale null takes the wrong branch.
- await hoverUntilTooltip(wrapper, index, totalLabels)
+ await hoverUntilTooltip(wrapper, index, totalLabels, timeout)
fireEvent.click(wrapper)
}