-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from spaceagetv/feat/retry-until-truthy
feat: retryUntilTruthy()
- Loading branch information
Showing
4 changed files
with
290 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import chai, { expect } from 'chai' | ||
import chaiAsPromised from 'chai-as-promised' | ||
import { retryUntilTruthy } from '../src/utilities' | ||
|
||
chai.use(chaiAsPromised) | ||
|
||
describe('retryUntilTruthy', () => { | ||
it('should retry the function until it returns a truthy value', async () => { | ||
let attempts = 0 | ||
const result = await retryUntilTruthy(async () => { | ||
attempts++ | ||
if (attempts < 3) { | ||
return false | ||
} | ||
return 'success' | ||
}) | ||
expect(result).to.equal('success') | ||
expect(attempts).to.equal(3) | ||
}) | ||
|
||
it('should throw an error if timeout is reached', async () => { | ||
await expect(retryUntilTruthy(async () => false, 100)).to.be.rejectedWith( | ||
'Timeout after 100ms' | ||
) | ||
}) | ||
|
||
it('should throw an error if the error does not match defaults', async () => { | ||
await expect( | ||
retryUntilTruthy(async () => { | ||
throw new Error('test error') | ||
}) | ||
).to.be.rejectedWith('test error') | ||
}) | ||
|
||
it('should throw an error if the error does not match custom errorMatch', async () => { | ||
await expect( | ||
retryUntilTruthy( | ||
async () => { | ||
throw new Error('test error') | ||
}, | ||
undefined, | ||
undefined, | ||
{ errorMatch: 'custom error' } | ||
) | ||
).to.be.rejectedWith('test error') | ||
}) | ||
|
||
it('should throw an error if the error does not match regex errorMatch', async () => { | ||
await expect( | ||
retryUntilTruthy( | ||
async () => { | ||
throw new Error('test error') | ||
}, | ||
undefined, | ||
undefined, | ||
{ errorMatch: /custom error/ } | ||
) | ||
).to.be.rejectedWith('test error') | ||
}) | ||
}) |
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,105 @@ | ||
import { expect } from 'chai' | ||
import { | ||
addTimeoutToPromise, | ||
addTimeout, | ||
retry, | ||
setRetryOptions, | ||
getRetryOptions, | ||
resetRetryOptions, | ||
retryUntilTruthy, | ||
errToString, | ||
HelperFunctionName, | ||
} from '../src/utilities' | ||
|
||
describe('Utilities', () => { | ||
describe('addTimeoutToPromise', () => { | ||
it('should resolve the promise before timeout', async () => { | ||
const result = await addTimeoutToPromise(Promise.resolve('success'), 1000) | ||
expect(result).to.equal('success') | ||
}) | ||
|
||
it('should reject the promise after timeout', async () => { | ||
try { | ||
await addTimeoutToPromise(new Promise(() => null), 100) | ||
} catch (error) { | ||
expect(error.message).to.equal('timeout after 100ms') | ||
} | ||
}) | ||
}) | ||
|
||
// describe('addTimeout', () => { | ||
// it('should call the helper function and resolve before timeout', async () => { | ||
// const helpers = { | ||
// async testHelper() { | ||
// return 'success' | ||
// }, | ||
// } | ||
// const name = 'testHelper' as HelperFunctionName | ||
// const result = await addTimeout(name, 1000, undefined, helpers) | ||
// expect(result).to.equal('success') | ||
// }) | ||
|
||
// it('should call the helper function and reject after timeout', async () => { | ||
// const helpers = { | ||
// async testHelper() { | ||
// return new Promise(() => {}) | ||
// }, | ||
// } | ||
// try { | ||
// await addTimeout('testHelper', 100, undefined, helpers) | ||
// } catch (error) { | ||
// expect(error.message).to.equal('timeout after 100ms') | ||
// } | ||
// }) | ||
// }) | ||
|
||
describe('setRetryOptions', () => { | ||
it('should set the retry options', () => { | ||
const options = setRetryOptions({ retries: 10 }) | ||
expect(options.retries).to.equal(10) | ||
}) | ||
}) | ||
|
||
describe('getRetryOptions', () => { | ||
it('should get the current retry options', () => { | ||
resetRetryOptions() | ||
const options = getRetryOptions() | ||
expect(options.retries).to.equal(20) | ||
}) | ||
}) | ||
|
||
describe('resetRetryOptions', () => { | ||
it('should reset the retry options to default values', () => { | ||
setRetryOptions({ retries: 10 }) | ||
resetRetryOptions() | ||
const options = getRetryOptions() | ||
expect(options.retries).to.equal(20) | ||
}) | ||
}) | ||
|
||
describe('errToString', () => { | ||
it('should convert an Error object to a string', () => { | ||
const error = new Error('test error') | ||
const result = errToString(error) | ||
expect(result).to.equal('Error: test error') | ||
}) | ||
|
||
it('should convert a TypeError to a string', () => { | ||
const error = new TypeError('test error') | ||
const result = errToString(error) | ||
expect(result).to.equal('TypeError: test error') | ||
}) | ||
|
||
it('should return the string directly if the error is a string', () => { | ||
const error = 'test error' | ||
const result = errToString(error) | ||
expect(result).to.equal('test error') | ||
}) | ||
|
||
it('should convert other types to a JSON string', () => { | ||
const error = { message: 'test error' } | ||
const result = errToString(error) | ||
expect(result).to.equal('{"message":"test error"}') | ||
}) | ||
}) | ||
}) |