Skip to content

Commit b3f7e0f

Browse files
MelvinBotmountiny
andcommitted
Fix unmount test to use fake timers so it actually validates clearTimeout
The test previously used a 10s real-timer delay but only waited 150ms after unmount, so it would pass even without clearTimeout. Now uses jest.useFakeTimers() to advance past the deadline, ensuring the test catches regressions in cleanup behavior. Co-authored-by: Vit Horacek <mountiny@users.noreply.github.com>
1 parent 639f700 commit b3f7e0f

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

tests/unit/hooks/usePendingConciergeResponse.test.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,27 @@ describe('usePendingConciergeResponse', () => {
9797
});
9898

9999
it('should cancel the timer on unmount and not apply the action', async () => {
100-
// Given a pending concierge response with a large delay so the timer never races with unmount under CI load
100+
// Given a pending concierge response with a short delay
101+
const timerDelay = SHORT_DELAY;
101102
await Onyx.merge(`${ONYXKEYS.COLLECTION.PENDING_CONCIERGE_RESPONSE}${REPORT_ID}`, {
102103
reportAction: fakeConciergeAction,
103-
displayAfter: Date.now() + 10_000,
104+
displayAfter: Date.now() + timerDelay,
104105
});
105106
await waitForBatchedUpdates();
106107

108+
// Switch to fake timers so we can advance past the deadline deterministically
109+
jest.useFakeTimers();
110+
107111
const {unmount} = renderHook(() => usePendingConciergeResponse(REPORT_ID));
108-
await waitForBatchedUpdates();
109112

110113
// When the hook unmounts before the delay
111114
unmount();
112115

113-
// And we wait long enough to confirm the action was not applied
114-
await delay(150);
116+
// Advance well past the timer deadline — if clearTimeout were missing, the callback would fire here
117+
jest.advanceTimersByTime(timerDelay + 1000);
118+
119+
// Restore real timers before async assertions
120+
jest.useRealTimers();
115121
await waitForBatchedUpdates();
116122

117123
// Then the action should NOT be in REPORT_ACTIONS (timer was cleaned up)

0 commit comments

Comments
 (0)