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(test): resolve TrendsBarChart flaky test race condition (#58642)

mendral-app[bot]f0c443ca3175
diff --git a/frontend/src/lib/hog-charts/testing/interactions.ts b/frontend/src/lib/hog-charts/testing/interactions.ts
index 1b743b2d186c..1cc47affb888 100644
--- a/frontend/src/lib/hog-charts/testing/interactions.ts
+++ b/frontend/src/lib/hog-charts/testing/interactions.ts
@@ -1,4 +1,4 @@
-import { fireEvent } from '@testing-library/react'
+import { act, fireEvent } from '@testing-library/react'
import { dimensions } from './jsdom'
import { waitForHogChartTooltip } from './tooltip'
@@ -7,9 +7,11 @@ import { waitForHogChartTooltip } from './tooltip'
* corresponding to the given label index. */
export function hoverAtIndex(wrapper: HTMLElement, index: number, totalLabels: number): void {
const step = dimensions.plotWidth / (totalLabels - 1)
- fireEvent.mouseMove(wrapper, {
- clientX: dimensions.plotLeft + step * index,
- clientY: dimensions.plotTop + dimensions.plotHeight / 2,
+ act(() => {
+ fireEvent.mouseMove(wrapper, {
+ clientX: dimensions.plotLeft + step * index,
+ clientY: dimensions.plotTop + dimensions.plotHeight / 2,
+ })
})
}
diff --git a/frontend/src/scenes/trends/viz/trends-bar-chart/TrendsBarChart.test.tsx b/frontend/src/scenes/trends/viz/trends-bar-chart/TrendsBarChart.test.tsx
index bbd8f00bd674..6b6babf5262a 100644
--- a/frontend/src/scenes/trends/viz/trends-bar-chart/TrendsBarChart.test.tsx
+++ b/frontend/src/scenes/trends/viz/trends-bar-chart/TrendsBarChart.test.tsx
@@ -3,7 +3,7 @@ import '@testing-library/jest-dom'
import { cleanup, screen, waitFor } from '@testing-library/react'
import { FEATURE_FLAGS } from 'lib/constants'
-import { setupJsdom } from 'lib/hog-charts/testing'
+import { setupJsdom, setupSyncRaf } from 'lib/hog-charts/testing'
import { NodeKind } from '~/queries/schema/schema-general'
import { buildTrendsQuery, chart, getHogChart, personsModal, renderInsight } from '~/test/insight-testing'
@@ -11,13 +11,16 @@ import { buildAnnotation } from '~/test/insight-testing/test-data'
import { AnnotationScope, ChartDisplayType } from '~/types'
let cleanupJsdom: () => void
+let cleanupRaf: () => void
beforeEach(() => {
cleanupJsdom = setupJsdom()
+ cleanupRaf = setupSyncRaf()
})
afterEach(() => {
personsModal.cleanupAll()
+ cleanupRaf()
cleanupJsdom()
cleanup()
})