Skip to content

Commit

Permalink
fix axios-timing related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
janpaepke authored and edorivai committed Feb 20, 2024
1 parent 41ed291 commit 342cedd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
5 changes: 3 additions & 2 deletions tests/communication/custom-idempotency-key.test.ts
Expand Up @@ -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' }));
Expand Down
26 changes: 16 additions & 10 deletions tests/communication/request-retrying.test.ts
Expand Up @@ -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' }));
Expand Down Expand Up @@ -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' }));
Expand All @@ -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' }));
Expand Down Expand Up @@ -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();

Expand All @@ -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' }));
Expand Down
12 changes: 8 additions & 4 deletions tests/iteration/multipage-iteration.test.ts
Expand Up @@ -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();

Expand All @@ -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());
Expand Down

0 comments on commit 342cedd

Please sign in to comment.