Skip to content

Commit

Permalink
test: add testing for patterns of failure after retries
Browse files Browse the repository at this point in the history
  • Loading branch information
yutak23 committed Dec 18, 2023
1 parent 75a63ca commit 7e7c61e
Showing 1 changed file with 82 additions and 30 deletions.
112 changes: 82 additions & 30 deletions spec/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,44 +575,96 @@ describe('axiosRetry(axios, { disableOtherResponseInterceptors })', () => {
nock.enableNetConnect();
});

it('should not multiple response interceptor', (done) => {
const client = axios.create();
nock('http://example.com').get('/test').times(2).replyWithError(NETWORK_ERROR);
nock('http://example.com').get('/test').reply(200, 'It worked!');
describe('successful after retry', () => {
it('should not multiple response interceptor', (done) => {
const client = axios.create();
nock('http://example.com').get('/test').times(2).replyWithError(NETWORK_ERROR);
nock('http://example.com').get('/test').reply(200, 'It worked!');

axiosRetry(client, { disableOtherResponseInterceptors: true });
axiosRetry(client, { retries: 3, disableOtherResponseInterceptors: true });

let anotherInterceptorCallCount = 0;
client.interceptors.response.use((result) => {
anotherInterceptorCallCount += 1;
return result;
}, null);
let anotherInterceptorCallCount = 0;
client.interceptors.response.use((result) => {
anotherInterceptorCallCount += 1;
return result;
}, null);

client.get('http://example.com/test').then((result) => {
expect(result.status).toBe(200);
expect(anotherInterceptorCallCount).toBe(1);
done();
}, done.fail);
});

it('should multiple response interceptor', (done) => {
const client = axios.create();
nock('http://example.com').get('/test').times(2).replyWithError(NETWORK_ERROR);
nock('http://example.com').get('/test').reply(200, 'It worked!');

axiosRetry(client, { retries: 3, disableOtherResponseInterceptors: false });

client.get('http://example.com/test').then((result) => {
expect(result.status).toBe(200);
expect(anotherInterceptorCallCount).toBe(1);
done();
}, done.fail);
let anotherInterceptorCallCount = 0;
client.interceptors.response.use((result) => {
anotherInterceptorCallCount += 1;
return result;
}, null);

client.get('http://example.com/test').then((result) => {
expect(result.status).toBe(200);
expect(anotherInterceptorCallCount).toBe(3);
done();
}, done.fail);
});
});

it('should multiple response interceptor', (done) => {
const client = axios.create();
nock('http://example.com').get('/test').times(2).replyWithError(NETWORK_ERROR);
nock('http://example.com').get('/test').reply(200, 'It worked!');
describe('failure after retry', () => {
it('should not multiple response interceptor', (done) => {
const client = axios.create();
nock('http://example.com').get('/test').times(3).replyWithError(NETWORK_ERROR);

axiosRetry(client, { disableOtherResponseInterceptors: false });
axiosRetry(client, { retries: 3, disableOtherResponseInterceptors: true });

let anotherInterceptorCallCount = 0;
client.interceptors.response.use((result) => {
anotherInterceptorCallCount += 1;
return result;
}, null);
let anotherInterceptorCallCount = 0;
client.interceptors.response.use(null, (error) => {
anotherInterceptorCallCount += 1;
return Promise.reject(error);
});

client
.get('http://example.com/test')
.then(
() => done.fail(),
(error) => {
expect(anotherInterceptorCallCount).toBe(1);
done();
}
)
.catch(done.fail);
});

it('should multiple response interceptor', (done) => {
const client = axios.create();
nock('http://example.com').get('/test').times(3).replyWithError(NETWORK_ERROR);

axiosRetry(client, { retries: 3, disableOtherResponseInterceptors: false });

client.get('http://example.com/test').then((result) => {
expect(result.status).toBe(200);
expect(anotherInterceptorCallCount).toBe(3);
done();
}, done.fail);
let anotherInterceptorCallCount = 0;
client.interceptors.response.use(null, (error) => {
anotherInterceptorCallCount += 1;
return Promise.reject(error);
});

client
.get('http://example.com/test')
.then(
() => done.fail(),
(error) => {
expect(anotherInterceptorCallCount).toBe(4);
done();
}
)
.catch(done.fail);
});
});
});

Expand Down

0 comments on commit 7e7c61e

Please sign in to comment.