From eaaeecd8ff98b378e902efdbbfb73bdac4be8c66 Mon Sep 17 00:00:00 2001 From: Jan Paepke Date: Wed, 13 Dec 2023 12:41:48 +0100 Subject: [PATCH] fix axios-timing related tests --- .../custom-idempotency-key.test.ts | 5 ++-- tests/communication/request-retrying.test.ts | 26 ++++++++++++------- tests/iteration/multipage-iteration.test.ts | 12 ++++++--- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/tests/communication/custom-idempotency-key.test.ts b/tests/communication/custom-idempotency-key.test.ts index c4ef385e..2ad78644 100644 --- a/tests/communication/custom-idempotency-key.test.ts +++ b/tests/communication/custom-idempotency-key.test.ts @@ -89,13 +89,14 @@ describe('custom-idempotency-key', () => { ); // Expect the first network request to have been made, and the promise to be pending. - await tick(); + await jest.advanceTimersByTimeAsync(0); // process request expect(errorInterceptor).toBeDepleted(); expect(paymentPromise).toBePending(); // Expect the second network request to have been made after two seconds, proving the Idempotency-Key header was // consistent across these two network requests, and the promise to have fulfilled. - jest.advanceTimersByTime(2e3); + await tick(); + await jest.advanceTimersByTimeAsync(3e3); await tick(); expect(successInterceptor).toBeDepleted(); expect(paymentPromise).toBeFulfilledWith(expect.objectContaining({ id: 'tr_WDqYK6vllg' })); diff --git a/tests/communication/request-retrying.test.ts b/tests/communication/request-retrying.test.ts index 6447ae2f..7870cbdc 100644 --- a/tests/communication/request-retrying.test.ts +++ b/tests/communication/request-retrying.test.ts @@ -104,13 +104,14 @@ describe('request-retrying', () => { const paymentPromise = observePromise(mollieClient.payments.get('tr_WDqYK6vllg')); // Expect the first network request to have been made, and the promise to be pending. - await tick(); + await jest.advanceTimersByTimeAsync(0); // process request expect(errorInterceptor).toBeDepleted(); expect(successInterceptor).not.toBeDepleted(); expect(paymentPromise).toBePending(); // Expect the second network request to have been made after two seconds, and the promise to have fulfilled. - jest.advanceTimersByTime(2e3); + await tick(); + await jest.advanceTimersByTimeAsync(2e3); await tick(); expect(successInterceptor).toBeDepleted(); expect(paymentPromise).toBeFulfilledWith(expect.objectContaining({ id: 'tr_WDqYK6vllg' })); @@ -143,20 +144,22 @@ describe('request-retrying', () => { const paymentPromise = observePromise(mollieClient.payments.get('tr_WDqYK6vllg')); // Expect a timeout to have been scheduled for the next attempt, and the promise to be pending. - await tick(); + await jest.advanceTimersByTimeAsync(0); // process request expect(interceptor).not.toBeDepleted(); expect(paymentPromise).toBePending(); // After two seconds: expect another timeout to have been scheduled for the next attempt, and the promise to still // be pending. - jest.advanceTimersByTime(2e3); + await tick(); + await jest.advanceTimersByTimeAsync(2e3); await tick(); expect(interceptor).not.toBeDepleted(); expect(paymentPromise).toBePending(); // After another two seconds: expect three network requests to have been made in total, and the promise to be // rejected. - jest.advanceTimersByTime(2e3); + await tick(); + await jest.advanceTimersByTimeAsync(2e3); await tick(); expect(interceptor).toBeDepleted(); expect(paymentPromise).toBeRejectedWith(expect.objectContaining({ message: 'Mock error' })); @@ -181,21 +184,23 @@ describe('request-retrying', () => { const paymentPromise = observePromise(mollieClient.payments.get('tr_WDqYK6vllg')); // Expect the first network request to have been made, and the promise to be pending. - await tick(); + await jest.advanceTimersByTimeAsync(0); // process request expect(errorInterceptor).toBeDepleted(); expect(successInterceptor).not.toBeDepleted(); expect(paymentPromise).toBePending(); // Expect the client to respect the five-second Retry-After header, and thus nothing having changed after two // seconds. - jest.advanceTimersByTime(2e3); + await tick(); + await jest.advanceTimersByTimeAsync(2e3); await tick(); expect(errorInterceptor).toBeDepleted(); expect(successInterceptor).not.toBeDepleted(); expect(paymentPromise).toBePending(); // Expect the second network request to have been made after two seconds, and the promise to have fulfilled. - jest.advanceTimersByTime(3e3); + await tick(); + await jest.advanceTimersByTimeAsync(3e3); await tick(); expect(successInterceptor).toBeDepleted(); expect(paymentPromise).toBeFulfilledWith(expect.objectContaining({ id: 'tr_WDqYK6vllg' })); @@ -245,7 +250,7 @@ describe('request-retrying', () => { ); // Expect the first network request to have been made, and the promise to be pending. - await tick(); + await jest.advanceTimersByTimeAsync(0); // process request expect(errorInterceptor).toBeDepleted(); expect(paymentPromise).toBePending(); @@ -255,7 +260,8 @@ describe('request-retrying', () => { // Expect the second network request to have been made after two seconds, proving the Idempotency-Key header was // consistent across these two network requests, and the promise to have fulfilled. - jest.advanceTimersByTime(2e3); + await tick(); + await jest.advanceTimersByTimeAsync(2e3); await tick(); expect(successInterceptor).toBeDepleted(); expect(paymentPromise).toBeFulfilledWith(expect.objectContaining({ id: 'tr_WDqYK6vllg' })); diff --git a/tests/iteration/multipage-iteration.test.ts b/tests/iteration/multipage-iteration.test.ts index a241df46..6bfd90c2 100644 --- a/tests/iteration/multipage-iteration.test.ts +++ b/tests/iteration/multipage-iteration.test.ts @@ -63,7 +63,7 @@ describe('multipage-iteration', () => { test('throttling', async () => { const { interceptor: firstInterceptor, intercept: interceptNext } = intercept(128); - const { interceptor: secondIntercetptor } = interceptNext(128); + const { interceptor: secondInterceptor } = interceptNext(128); jest.useFakeTimers(); @@ -79,17 +79,21 @@ describe('multipage-iteration', () => { ); // Expect the first network request to have been made, and the count to be 128. + await jest.advanceTimersByTimeAsync(0); // process request await tick(); expect(firstInterceptor).toBeDepleted(); - expect(secondIntercetptor).not.toBeDepleted(); + expect(secondInterceptor).not.toBeDepleted(); expect(count).toBe(128); // Expect the second network request to have been made after six seconds, and the count to be 200. - jest.advanceTimersByTime(6e3); await tick(); - expect(secondIntercetptor).toBeDepleted(); + await jest.advanceTimersByTimeAsync(6e3); + await tick(); + expect(secondInterceptor).toBeDepleted(); expect(count).toBe(200); expect(promise).toBeFulfilledWith(undefined); + + jest.useRealTimers(); }); afterAll(() => networkMocker.cleanup());