: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(taxonomic-filter): deflake component tests with fake timers (#70307)
Paul D'Ambra386e5d05a4df
diff --git a/frontend/jest.config.ts b/frontend/jest.config.tsindex 86824a7c4d92..5ccbdff06f80 100644--- a/frontend/jest.config.ts+++ b/frontend/jest.config.ts@@ -140,9 +140,11 @@ const config: Config = {// Faking queueMicrotask starves the web-streams pump that MSW v2 response bodies ride on:// each pump microtask lands in the fake queue and respawns the next one, so any// advanceTimersByTimeAsync allocates unboundedly until the worker OOMs. Keep microtasks real.+ // setImmediate drives MSW v2's interceptor response pump the same way — faking it deadlocks+ // any test that awaits a mocked request under fake timers (upstream stance: mswjs/msw#1830).// Merged into per-test `jest.useFakeTimers({...})` configs unless they pass their own doNotFake.fakeTimers: {- doNotFake: ['queueMicrotask'],+ doNotFake: ['queueMicrotask', 'setImmediate'],},// Force coverage collection from ignored files using an array of glob patternsdiff --git a/frontend/src/lib/components/PropertyFilters/PropertyFilters.component.test.tsx b/frontend/src/lib/components/PropertyFilters/PropertyFilters.component.test.tsxindex 5f44a6d63b2f..323a83117fe6 100644--- a/frontend/src/lib/components/PropertyFilters/PropertyFilters.component.test.tsx+++ b/frontend/src/lib/components/PropertyFilters/PropertyFilters.component.test.tsx@@ -1,6 +1,6 @@import '@testing-library/jest-dom'-import { cleanup, render, screen, waitFor } from '@testing-library/react'+import { act, cleanup, render, screen, waitFor } from '@testing-library/react'import userEvent from '@testing-library/user-event'import { Provider } from 'kea'@@ -92,8 +92,22 @@ describe('PropertyFilters recent selections', () => {await userEvent.click(screen.getByTestId(tabTestId))}+ // Typing here pays taxonomicFilterLogic's real 500ms search breakpoint (plus stacked 100ms+ // ones). Fake timers skip that wait; real timers resume immediately after so the resulting+ // MSW round trip settles normally instead of fighting fake-timer polling. setImmediate is+ // excluded like queueMicrotask (see jest.config.ts) — it also drives MSW v2's response pump.async function searchFor(query: string): Promise<void> {- await userEvent.type(screen.getByTestId('taxonomic-filter-searchfield'), query)+ jest.useFakeTimers({ doNotFake: ['queueMicrotask', 'setImmediate'] })+ try {+ await userEvent+ .setup({ advanceTimers: jest.advanceTimersByTime })+ .type(screen.getByTestId('taxonomic-filter-searchfield'), query)+ await act(async () => {+ jest.advanceTimersByTime(600)+ })+ } finally {+ jest.useRealTimers()+ }}async function selectItem(itemTestId: string, onChange: jest.Mock): Promise<void> {diff --git a/frontend/src/lib/components/TaxonomicFilter/TaxonomicFilter.test.tsx b/frontend/src/lib/components/TaxonomicFilter/TaxonomicFilter.test.tsxindex 2ef3e39b7613..89da1af414f3 100644--- a/frontend/src/lib/components/TaxonomicFilter/TaxonomicFilter.test.tsx+++ b/frontend/src/lib/components/TaxonomicFilter/TaxonomicFilter.test.tsx@@ -1,6 +1,6 @@import '@testing-library/jest-dom'-import { cleanup, render, screen, waitFor } from '@testing-library/react'+import { act, cleanup, render, screen, waitFor } from '@testing-library/react'import userEvent from '@testing-library/user-event'import { Provider } from 'kea'import posthog from 'posthog-js'@@ -95,6 +95,27 @@ describe('TaxonomicFilter', () => {}}+ // Search interactions pay taxonomicFilterLogic's real 500ms breakpoint (plus stacked+ // 100ms ones), which is what pushed these tests over CI's per-test timeout. Fake timers+ // skip that wait; real timers resume immediately after so the resulting MSW round trip+ // (and any waitFor built on it) settles normally instead of fighting fake-timer polling.+ async function withoutDebounceDelay(+ action: (user: ReturnType<typeof userEvent.setup>) => Promise<void>+ ): Promise<void> {+ // setImmediate also drives MSW v2's response-body pump (like queueMicrotask, see+ // jest.config.ts) — faking it makes advanceTimersByTime re-run that pump once per+ // virtual ms for every matched row, turning a fast search into a slow one.+ jest.useFakeTimers({ doNotFake: ['queueMicrotask', 'setImmediate'] })+ try {+ await action(userEvent.setup({ advanceTimers: jest.advanceTimersByTime }))+ await act(async () => {+ jest.advanceTimersByTime(600)+ })+ } finally {+ jest.useRealTimers()+ }+ }+describe('rendering', () => {it('renders search input and loads results from the API', async () => {renderFilter()@@ -220,7 +241,9 @@ describe('TaxonomicFilter', () => {expect(screen.getByTestId('prop-filter-events-0')).toBeInTheDocument()})- await userEvent.type(screen.getByTestId('taxonomic-filter-searchfield'), 'test event')+ await withoutDebounceDelay((user) =>+ user.type(screen.getByTestId('taxonomic-filter-searchfield'), 'test event')+ )await waitFor(() => {expect(screen.getAllByText('test event').length).toBeGreaterThanOrEqual(1)@@ -270,13 +293,13 @@ describe('TaxonomicFilter', () => {})const searchInput = screen.getByTestId('taxonomic-filter-searchfield')- await userEvent.type(searchInput, 'xyznonexistent')+ await withoutDebounceDelay((user) => user.type(searchInput, 'xyznonexistent'))await waitFor(() => {expect(screen.queryByTestId('prop-filter-events-0')).not.toBeInTheDocument()})- await userEvent.clear(searchInput)+ await withoutDebounceDelay((user) => user.clear(searchInput))await waitFor(() => {expect(screen.getByTestId('prop-filter-events-0')).toBeInTheDocument()@@ -309,7 +332,11 @@ describe('TaxonomicFilter', () => {})await activateGroupWithResults('taxonomic-tab-person_properties')- // Search for something that only matches events, leaving person properties empty+ // Search for something that only matches events, leaving person properties empty.+ // Real timers here: this scenario includes SuggestedFilters, whose reveal-barrier+ // state doesn't survive the fake->real timer switch withoutDebounceDelay performs+ // (a pending fake timer is dropped rather than carried over), so the button never+ // appears. See withoutDebounceDelay's other uses for the debounce-skip that's safe.await userEvent.type(screen.getByTestId('taxonomic-filter-searchfield'), 'test event')// The empty state offers a shortcut to the aggregated all/suggested-filters section@@ -337,6 +364,8 @@ describe('TaxonomicFilter', () => {})await activateGroupWithResults('taxonomic-tab-person_properties')+ // Real timers: SuggestedFilters is present here too — see the comment in the+ // preceding test for why withoutDebounceDelay isn't safe for this scenario.await userEvent.type(screen.getByTestId('taxonomic-filter-searchfield'), 'xyznonexistent')await waitFor(() => {@@ -354,7 +383,9 @@ describe('TaxonomicFilter', () => {await activateGroupWithResults('taxonomic-tab-events')// `purchase_value` exists only as a property, so the active Events tab comes up empty- await userEvent.type(screen.getByTestId('taxonomic-filter-searchfield'), 'purchase_value')+ await withoutDebounceDelay((user) =>+ user.type(screen.getByTestId('taxonomic-filter-searchfield'), 'purchase_value')+ )let switchButton: HTMLElement | undefinedawait waitFor(() => {@@ -383,7 +414,9 @@ describe('TaxonomicFilter', () => {})await activateGroupWithResults('taxonomic-tab-events')- await userEvent.type(screen.getByTestId('taxonomic-filter-searchfield'), 'purchase_value')+ await withoutDebounceDelay((user) =>+ user.type(screen.getByTestId('taxonomic-filter-searchfield'), 'purchase_value')+ )// The genuinely-matching tab is still offered...await waitFor(() => {@@ -893,12 +926,14 @@ describe('TaxonomicFilter', () => {expect(screen.getByTestId('prop-filter-events-0')).toBeInTheDocument()})- await userEvent.type(screen.getByTestId('taxonomic-filter-searchfield'), 'zzznonexistentevent12345')+ await withoutDebounceDelay((user) =>+ user.type(screen.getByTestId('taxonomic-filter-searchfield'), 'zzznonexistentevent12345')+ )await waitFor(() => {expect(screen.queryAllByTestId(/^prop-filter-events-/)).toHaveLength(0)})- }, 10000)+ })})it.each([@@ -1116,7 +1151,6 @@ describe('TaxonomicFilter', () => {}it('collapses the matching URL list to a single "URL contains" shortcut row', async () => {- const user = userEvent.setup()useMockPageviewUrls(['https://example.com/pricing', 'https://example.com/pricing/teams'])renderFilter({taxonomicGroupTypes: [TaxonomicFilterGroupType.PageviewUrls],@@ -1124,7 +1158,7 @@ describe('TaxonomicFilter', () => {})const searchInput = await waitFor(() => screen.getByTestId('taxonomic-filter-searchfield'))- await user.type(searchInput, 'pricing')+ await withoutDebounceDelay((fakeTimerUser) => fakeTimerUser.type(searchInput, 'pricing'))const firstRow = await waitFor(() => screen.getByTestId('prop-filter-pageview_urls-0'))// The two matching URLs collapse into one row, which is the contains shortcut.@@ -1141,7 +1175,7 @@ describe('TaxonomicFilter', () => {})const searchInput = await waitFor(() => screen.getByTestId('taxonomic-filter-searchfield'))- await user.type(searchInput, 'pricing')+ await withoutDebounceDelay((fakeTimerUser) => fakeTimerUser.type(searchInput, 'pricing'))const row = await waitFor(() => {const el = document.querySelector('[data-attr="taxonomic-shortcut-pricing-property"]')@@ -1167,6 +1201,11 @@ describe('TaxonomicFilter', () => {})it('collapses URLs in the aggregated Suggested filters tab too', async () => {+ // Real timers: this scenario includes SuggestedFilters, whose reveal-barrier state+ // doesn't survive the fake->real timer switch withoutDebounceDelay performs (a+ // pending fake timer is dropped rather than carried over), so the aggregated row+ // never appears. See withoutDebounceDelay's other uses in this describe for the+ // debounce-skip that's safe when SuggestedFilters isn't part of the group list.const user = userEvent.setup()useMockPageviewUrls(['https://example.com/pricing', 'https://example.com/pricing/teams'])renderFilter({@@ -1192,14 +1231,13 @@ describe('TaxonomicFilter', () => {})it('lists individual URLs (no collapse) when the prop is omitted', async () => {- const user = userEvent.setup()useMockPageviewUrls(['https://example.com/pricing'])renderFilter({taxonomicGroupTypes: [TaxonomicFilterGroupType.PageviewUrls],})const searchInput = await waitFor(() => screen.getByTestId('taxonomic-filter-searchfield'))- await user.type(searchInput, 'pricing')+ await withoutDebounceDelay((fakeTimerUser) => fakeTimerUser.type(searchInput, 'pricing'))await waitFor(() => {expect(screen.getByTestId('prop-filter-pageview_urls-0')).toBeInTheDocument()@@ -1208,7 +1246,6 @@ describe('TaxonomicFilter', () => {})it('shows no shortcut row when no URL matches the query', async () => {- const user = userEvent.setup()// Flag set when the URL values endpoint actually responds (empty), so the negative// assertions below aren't vacuously true before the async fetch path runs.let valuesFetched = false@@ -1230,7 +1267,7 @@ describe('TaxonomicFilter', () => {const searchInput = await waitFor(() => screen.getByTestId('taxonomic-filter-searchfield'))// A query unique to this test so the module-level `apiCache` in infiniteListLogic// can't serve a non-empty response cached by an earlier test under the same URL.- await user.type(searchInput, 'nomatchquery')+ await withoutDebounceDelay((user) => user.type(searchInput, 'nomatchquery'))await waitFor(() => expect(valuesFetched).toBe(true))await waitFor(() => {