Skip to content

Commit 586e078

Browse files
test(billing): use fake timers in checkout webhook fallback test
The non-fatal-fallback test mocked paymentIntents.update to reject, so it incurred ~600ms of real retryWithBackoff delay per run. Drive the backoff with jest.useFakeTimers() + runAllTimersAsync() and restore real timers in afterEach so sibling tests are unaffected. Closes #3689
1 parent 3df0034 commit 586e078

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

modules/billing/tests/billing.webhook.checkout.unit.tests.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ describe('Billing webhook checkout unit tests:', () => {
104104
});
105105

106106
afterEach(() => {
107+
jest.useRealTimers();
107108
jest.restoreAllMocks();
108109
});
109110

@@ -284,17 +285,21 @@ describe('Billing webhook checkout unit tests:', () => {
284285
});
285286

286287
test('should not throw when paymentIntents.update fails (non-fatal fallback)', async () => {
288+
jest.useFakeTimers();
287289
const paymentIntentId = 'pi_test_failing';
288290
mockStripeInstance.paymentIntents.update.mockRejectedValue(new Error('Stripe API error'));
289291

290-
await expect(
291-
BillingWebhookService.handleCheckoutPaymentCompleted({
292-
id: stripeSessionId,
293-
payment_status: 'paid',
294-
payment_intent: paymentIntentId,
295-
metadata: { organizationId: orgId, packId: 'pack_500k', kind: 'extras' },
296-
}),
297-
).resolves.toBeUndefined();
292+
const promise = BillingWebhookService.handleCheckoutPaymentCompleted({
293+
id: stripeSessionId,
294+
payment_status: 'paid',
295+
payment_intent: paymentIntentId,
296+
metadata: { organizationId: orgId, packId: 'pack_500k', kind: 'extras' },
297+
});
298+
// handleCheckoutPaymentCompleted catches the retry exhaustion internally (non-fatal),
299+
// so the promise resolves; advance the backoff timers while it is pending.
300+
const assertion = expect(promise).resolves.toBeUndefined();
301+
await jest.runAllTimersAsync();
302+
await assertion;
298303

299304
// creditPack should still have run despite the PI update failure
300305
expect(mockExtraService.creditPack).toHaveBeenCalledWith(orgId, 'pack_500k', stripeSessionId);

0 commit comments

Comments
 (0)