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: deflake hog-chart tooltip hover race (#61176)

Raúl Negrón-Otero0f3ac068f712
diff --git a/frontend/src/lib/hog-charts/testing/index.ts b/frontend/src/lib/hog-charts/testing/index.ts
index 9922d3f3ea3a..343d918e3084 100644
--- a/frontend/src/lib/hog-charts/testing/index.ts
+++ b/frontend/src/lib/hog-charts/testing/index.ts
@@ -1,5 +1,5 @@
export { dimensions, ensureJsdom, makeSeries, mockRect, setupJsdom, setupSyncRaf } from './jsdom'
-export { clickAtIndex, hoverAtIndex } from './interactions'
+export { clickAtIndex, hoverAtIndex, hoverUntilTooltip } from './interactions'
export { getHogChart } from './accessor'
export type { GetHogChartOptions, HogChart, TooltipSnapshot } from './accessor'
export { renderHogChart } from './render'
diff --git a/frontend/src/lib/hog-charts/testing/interactions.ts b/frontend/src/lib/hog-charts/testing/interactions.ts
index 1cc47affb888..cf63a4331682 100644
--- a/frontend/src/lib/hog-charts/testing/interactions.ts
+++ b/frontend/src/lib/hog-charts/testing/interactions.ts
@@ -15,10 +15,24 @@ export function hoverAtIndex(wrapper: HTMLElement, index: number, totalLabels: n
})
}
+/** Hover at the given label index, re-dispatching the mouseMove until the chart's
+ * tooltip portal mounts. The chart commits its scales/dimensions in a post-render
+ * effect (useChartCanvas), and onMouseMove is a no-op until that commit lands — so
+ * a single hover fired before the chart settles is silently dropped and the tooltip
+ * never appears. Polling the *trigger* (not just the result) makes the hover
+ * deterministic regardless of when the chart settles, without inflating timeouts. */
+export async function hoverUntilTooltip(
+ wrapper: HTMLElement,
+ index: number,
+ totalLabels: number,
+ timeout = 3000
+): Promise<HTMLElement> {
+ return waitForHogChartTooltip(timeout, () => hoverAtIndex(wrapper, index, totalLabels))
+}
+
export async function clickAtIndex(wrapper: HTMLElement, index: number, totalLabels: number): Promise<void> {
- hoverAtIndex(wrapper, index, totalLabels)
- // Wait for the hover state to flush — onClick reads tooltipCtx synchronously
- // to decide between pinning and onPointClick, and a stale null takes the wrong branch.
- await waitForHogChartTooltip()
+ // 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)
fireEvent.click(wrapper)
}
diff --git a/frontend/src/lib/hog-charts/testing/tooltip.ts b/frontend/src/lib/hog-charts/testing/tooltip.ts
index 631fb0eb4590..9765d6945709 100644
--- a/frontend/src/lib/hog-charts/testing/tooltip.ts
+++ b/frontend/src/lib/hog-charts/testing/tooltip.ts
@@ -29,14 +29,17 @@ export function getHogChartTooltip(): HTMLElement | null {
return document.querySelector(HOG_CHARTS_TOOLTIP_SELECTOR)
}
-/** Wait until a chart tooltip is present in the document and return it. */
-export async function waitForHogChartTooltip(timeout = 3000): Promise<HTMLElement> {
+/** Wait until a chart tooltip is present in the document and return it. `beforePoll`, if given,
+ * runs at the start of each poll attempt — used to re-dispatch a triggering event (e.g. a hover)
+ * that the chart may have dropped before it became interactive. */
+export async function waitForHogChartTooltip(timeout = 3000, beforePoll?: () => void): Promise<HTMLElement> {
// Flush pending microtasks so React portal commits complete before polling.
await new Promise((r) => setTimeout(r, 0))
let tooltip!: HTMLElement
await waitFor(
() => {
+ beforePoll?.()
const el = getHogChartTooltip()
if (!el) {
throw new Error('tooltip not yet rendered')
diff --git a/frontend/src/test/insight-testing/interactions.ts b/frontend/src/test/insight-testing/interactions.ts
index 307bd601e2ec..c0052bedc775 100644
--- a/frontend/src/test/insight-testing/interactions.ts
+++ b/frontend/src/test/insight-testing/interactions.ts
@@ -1,7 +1,7 @@
import { fireEvent, screen, waitFor, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
-import { clickAtIndex, getHogChartTooltip, hoverAtIndex, waitForHogChartTooltip } from 'lib/hog-charts/testing'
+import { clickAtIndex, getHogChartTooltip, hoverUntilTooltip, waitForHogChartTooltip } from 'lib/hog-charts/testing'
import { insightVizDataLogic } from 'scenes/insights/insightVizDataLogic'
import { trendsDataLogic } from 'scenes/trends/trendsDataLogic'
import { IndexedTrendResult } from 'scenes/trends/types'
@@ -144,8 +144,7 @@ export const chart = {
): Promise<InsightTooltipAccessor> {
const canvas = await screen.findByRole('img', { name: /chart with/i }, { timeout: DEBOUNCE_TIMEOUT })
const wrapper = canvas.parentElement!
- hoverAtIndex(wrapper, index, totalLabels)
- const tooltip = await waitForHogChartTooltip()
+ const tooltip = await hoverUntilTooltip(wrapper, index, totalLabels)
return createInsightTooltipAccessor(tooltip)
},
async clickAtIndex(index: number, totalLabels = trendsSeries.pageviews.labels.length): Promise<void> {