diff --git a/src/telegram/bot.create-telegram-bot.test.ts b/src/telegram/bot.create-telegram-bot.test.ts index f1d72d4a368..1826b69889c 100644 --- a/src/telegram/bot.create-telegram-bot.test.ts +++ b/src/telegram/bot.create-telegram-bot.test.ts @@ -1760,10 +1760,19 @@ describe("createTelegramBot", () => { await Promise.all([first, second]); expect(replySpy).not.toHaveBeenCalled(); - const flushTimerCall = [...setTimeoutSpy.mock.calls] - .toReversed() - .find((call) => call[1] === TELEGRAM_TEST_TIMINGS.mediaGroupFlushMs); - const flushTimer = flushTimerCall?.[0] as (() => unknown) | undefined; + const flushTimerCallIndex = setTimeoutSpy.mock.calls.findLastIndex( + (call) => call[1] === TELEGRAM_TEST_TIMINGS.mediaGroupFlushMs, + ); + const flushTimer = + flushTimerCallIndex >= 0 + ? (setTimeoutSpy.mock.calls[flushTimerCallIndex]?.[0] as (() => unknown) | undefined) + : undefined; + // Cancel the real timer so it cannot fire a second time after we manually invoke it. + if (flushTimerCallIndex >= 0) { + clearTimeout( + setTimeoutSpy.mock.results[flushTimerCallIndex]?.value as ReturnType, + ); + } expect(flushTimer).toBeTypeOf("function"); await flushTimer?.(); @@ -1939,10 +1948,19 @@ describe("createTelegramBot", () => { await Promise.all([first, second]); expect(replySpy).not.toHaveBeenCalled(); - const flushTimerCall = [...setTimeoutSpy.mock.calls] - .toReversed() - .find((call) => call[1] === TELEGRAM_TEST_TIMINGS.mediaGroupFlushMs); - const flushTimer = flushTimerCall?.[0] as (() => unknown) | undefined; + const flushTimerCallIndex = setTimeoutSpy.mock.calls.findLastIndex( + (call) => call[1] === TELEGRAM_TEST_TIMINGS.mediaGroupFlushMs, + ); + const flushTimer = + flushTimerCallIndex >= 0 + ? (setTimeoutSpy.mock.calls[flushTimerCallIndex]?.[0] as (() => unknown) | undefined) + : undefined; + // Cancel the real timer so it cannot fire a second time after we manually invoke it. + if (flushTimerCallIndex >= 0) { + clearTimeout( + setTimeoutSpy.mock.results[flushTimerCallIndex]?.value as ReturnType, + ); + } expect(flushTimer).toBeTypeOf("function"); await flushTimer?.();