Skip to content

Commit 3f6466e

Browse files
gonzaloriestraRiver
authored andcommitted
Fix flaky Dev component polling tests
Two timing-dependent test failures are fixed: 1. 'polls for preview mode': Replaced sleep-based timing with vi.waitFor() to reliably wait for the polling condition instead of hoping 50ms is enough. 2. 'doesn't poll for preview mode when the app does not support it': Used a locally-scoped mock to isolate from leaked polling intervals from previous tests that call the shared module-level developerPreview mock.
1 parent a21a899 commit 3f6466e

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

packages/app/src/cli/services/dev/ui/components/Dev.test.tsx

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -502,13 +502,16 @@ describe('Dev', () => {
502502
)
503503

504504
// Then
505-
// Wait long enough for multiple polling cycles
506-
await sleep(0.05)
505+
// Wait for polling to call fetchMode multiple times (avoids timing-dependent flakiness)
506+
await vi.waitFor(
507+
() => {
508+
expect(developerPreview.fetchMode.mock.calls.length).toBeGreaterThanOrEqual(2)
509+
},
510+
{timeout: 2000, interval: 10},
511+
)
507512

508513
// enable should be called once at startup
509514
expect(developerPreview.enable).toHaveBeenCalledTimes(1)
510-
// fetchMode should be called multiple times due to polling
511-
expect(developerPreview.fetchMode.mock.calls.length).toBeGreaterThanOrEqual(2)
512515

513516
// unmount so that polling is cleared after every test
514517
renderInstance.unmount()
@@ -525,9 +528,14 @@ describe('Dev', () => {
525528
},
526529
}
527530

528-
vi.mocked(developerPreview.fetchMode).mockReset()
529-
vi.mocked(developerPreview.fetchMode).mockResolvedValue(true)
530-
vi.mocked(developerPreview.enable).mockReset()
531+
// Use a locally-scoped mock to isolate from any leaked polling intervals
532+
// from previous tests that might call the module-level developerPreview mock
533+
const localDeveloperPreview = {
534+
fetchMode: vi.fn(async () => true),
535+
enable: vi.fn(async () => true),
536+
disable: vi.fn(async () => {}),
537+
update: vi.fn(async (_state: boolean) => true),
538+
}
531539

532540
// When
533541
const renderInstance = render(
@@ -543,13 +551,13 @@ describe('Dev', () => {
543551
}}
544552
// Set a very short polling time for tests
545553
pollingTime={10}
546-
developerPreview={developerPreview}
554+
developerPreview={localDeveloperPreview}
547555
shopFqdn="mystore.shopify.io"
548556
/>,
549557
)
550558

551559
// Then
552-
// Wait long enough for multiple polling cycles
560+
// Wait to ensure no polling occurs
553561
await sleep(0.05)
554562
expect(unstyled(renderInstance.lastFrame()!).replace(/\d/g, '0')).toMatchInlineSnapshot(`
555563
"00:00:00 │ backend │ first backend message
@@ -566,13 +574,13 @@ describe('Dev', () => {
566574
GraphiQL URL: http://localhost:0000/graphiql
567575
"
568576
`)
569-
expect(developerPreview.enable).not.toHaveBeenCalled()
570-
expect(developerPreview.fetchMode).not.toHaveBeenCalled()
577+
expect(localDeveloperPreview.enable).not.toHaveBeenCalled()
578+
expect(localDeveloperPreview.fetchMode).not.toHaveBeenCalled()
571579

572580
// Verify 'd' input doesn't trigger update when app doesn't support preview
573581
await waitForInputsToBeReady()
574582
await sendInputAndWait(renderInstance, 10, 'd')
575-
expect(developerPreview.update).not.toHaveBeenCalled()
583+
expect(localDeveloperPreview.update).not.toHaveBeenCalled()
576584

577585
// unmount so that polling is cleared after every test
578586
renderInstance.unmount()

0 commit comments

Comments
 (0)