-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* extract retry utils * util for getting spdx data * use util for spdx data * address feedback + fix issue with fetching spdx data * fix build error
- Loading branch information
1 parent
d5673ea
commit 031531c
Showing
8 changed files
with
279 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import axios from 'axios'; | ||
|
||
import { retryAsync, retryAxios } from './async'; | ||
|
||
describe('retryAsync', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should retry promise fails', async () => { | ||
const expectedRetryCount = 2; | ||
let actualRetryCount = 0; | ||
|
||
async function execute() { | ||
if (actualRetryCount === expectedRetryCount) { | ||
return Promise.resolve(); | ||
} | ||
|
||
actualRetryCount += 1; | ||
throw new Error('failure'); | ||
} | ||
|
||
await retryAsync({ execute }); | ||
expect(actualRetryCount).toBe(expectedRetryCount); | ||
}); | ||
|
||
it('should fail when promise fails too many times', async () => { | ||
const expectedRetryCount = 3; | ||
let actualRetryCount = 0; | ||
|
||
// eslint-disable-next-line @typescript-eslint/require-await | ||
async function execute() { | ||
actualRetryCount += 1; | ||
throw new Error('failure'); | ||
} | ||
|
||
await expect( | ||
retryAsync({ execute, retries: expectedRetryCount }), | ||
).rejects.toThrow('failure'); | ||
expect(actualRetryCount).toBe(expectedRetryCount); | ||
}); | ||
}); | ||
|
||
describe('retryAxios', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should retry fetching a failed request', async () => { | ||
const expectedRetryCount = 2; | ||
let actualRetryCount = 0; | ||
jest.spyOn(axios, 'request').mockImplementation(() => { | ||
if (actualRetryCount === expectedRetryCount) { | ||
return Promise.resolve({ | ||
data: 'data', | ||
status: 200, | ||
}); | ||
} | ||
|
||
actualRetryCount += 1; | ||
throw new Error('failure'); | ||
}); | ||
|
||
const res = await retryAxios({ url: '/test' }); | ||
expect(res.data).toBe('data'); | ||
expect(actualRetryCount).toBe(expectedRetryCount); | ||
}); | ||
|
||
it('should fail when the request fails too many times', async () => { | ||
const expectedRetryCount = 3; | ||
let actualRetryCount = 0; | ||
jest.spyOn(axios, 'request').mockImplementation(() => { | ||
actualRetryCount += 1; | ||
throw new Error('failure'); | ||
}); | ||
|
||
await expect( | ||
retryAxios({ url: '/test', retries: expectedRetryCount }), | ||
).rejects.toThrow('failure'); | ||
expect(actualRetryCount).toBe(expectedRetryCount); | ||
}); | ||
}); |
Oops, something went wrong.