Skip to content

Commit f0f08af

Browse files
authored
Merge pull request #6231 from Shopify/fix-flaky-poll-test
fix the flaky polling test in ci
2 parents 7a6e012 + 2415a3f commit f0f08af

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

packages/app/src/cli/services/app-logs/logs-command/ui/components/hooks/usePollAppLogs.test.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ describe('usePollAppLogs', () => {
218218
// needed to await the render
219219
await vi.advanceTimersByTimeAsync(0)
220220

221+
// Wait for the async polling function to execute
222+
await waitForMockCalls(mockedPollAppLogs, 1)
223+
221224
expect(mockedPollAppLogs).toHaveBeenCalledTimes(1)
222225

223226
expect(hook.lastResult?.appLogOutputs).toHaveLength(6)
@@ -340,6 +343,9 @@ describe('usePollAppLogs', () => {
340343
// needed to await the render
341344
await vi.advanceTimersByTimeAsync(0)
342345

346+
// Wait for the async polling function to execute
347+
await waitForMockCalls(mockedPollAppLogs, 1)
348+
343349
// Initial invocation, 401 returned
344350
expect(mockedPollAppLogs).toHaveBeenNthCalledWith(1, {
345351
pollOptions: {
@@ -354,6 +360,10 @@ describe('usePollAppLogs', () => {
354360

355361
// Follow up invocation, which invokes resubscribeCallback
356362
await vi.advanceTimersToNextTimerAsync()
363+
364+
// Wait for the second async polling function call to execute
365+
await waitForMockCalls(mockedPollAppLogs, 2)
366+
357367
expect(mockedPollAppLogs).toHaveBeenNthCalledWith(2, {
358368
pollOptions: {jwtToken: NEW_JWT_TOKEN, cursor: '', filters: EMPTY_FILTERS},
359369
developerPlatformClient: mockedDeveloperPlatformClient,
@@ -525,3 +535,27 @@ function renderHook<THookResult>(renderHookCallback: () => THookResult) {
525535

526536
return result
527537
}
538+
539+
async function waitForMockCalls(mockFn: any, expectedCallCount: number, timeoutMs = 2000): Promise<void> {
540+
return new Promise((resolve, reject) => {
541+
const timeout = setTimeout(
542+
() =>
543+
reject(
544+
new Error(
545+
`Mock timeout: expected ${expectedCallCount} calls, got ${mockFn.mock.calls.length} after ${timeoutMs}ms`,
546+
),
547+
),
548+
timeoutMs,
549+
)
550+
551+
const check = () => {
552+
if (mockFn.mock.calls.length >= expectedCallCount) {
553+
clearTimeout(timeout)
554+
resolve()
555+
} else {
556+
setImmediate(check)
557+
}
558+
}
559+
check()
560+
})
561+
}

0 commit comments

Comments
 (0)