-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Lexus Drumgold <[email protected]>
- Loading branch information
1 parent
1fb621e
commit 90cb6cf
Showing
3 changed files
with
70 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* @file Unit Tests - tryit | ||
* @module tutils/utils/tests/unit/tryit | ||
*/ | ||
|
||
import INTEGER from '#fixtures/integer' | ||
import type { Fn } from '#src/types' | ||
import type { Mock } from '#tests/interfaces' | ||
import testSubject from '../tryit' | ||
|
||
describe('unit:utils/tryit', () => { | ||
let fn: Mock<Fn<[string?], typeof INTEGER>> | ||
|
||
beforeAll(() => { | ||
fn = vi.fn() | ||
}) | ||
|
||
it('should return error-first async callback', () => { | ||
expect(testSubject(fn)).to.be.a('function') | ||
}) | ||
|
||
describe('error-first callback', () => { | ||
it('should return [error, null] if fn throws', async () => { | ||
// Arrange | ||
const error: Error = new Error('not implemented') | ||
fn.mockRejectedValueOnce(error) | ||
|
||
// Act + Expect | ||
expect(await testSubject(fn)()).to.deep.equal([error, null]) | ||
}) | ||
|
||
it('should return [null, result]', async () => { | ||
// Arrange | ||
fn.mockResolvedValueOnce(INTEGER) | ||
|
||
// Act + Expect | ||
expect(await testSubject(fn)()).to.deep.equal([null, INTEGER]) | ||
}) | ||
}) | ||
}) |
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,29 @@ | ||
/** | ||
* @file Utilities - tryit | ||
* @module tutils/utils/tryit | ||
*/ | ||
|
||
import type { Fn, Tryit } from '#src/types' | ||
|
||
/** | ||
* Converts `fn` to an error-first callback. | ||
* | ||
* @template T - Function to convert | ||
* @template E - Error type | ||
* | ||
* @param {T} fn - Function to convert | ||
* @return {Tryit<T, E>} Error first callback | ||
*/ | ||
function tryit<T extends Fn, E extends Error = Error>(fn: T): Tryit<T, E> { | ||
return async ( | ||
...args: Parameters<T> | ||
): Promise<[E, null] | [null, Awaited<ReturnType<T>>]> => { | ||
try { | ||
return [null, (await fn(...args)) as Awaited<ReturnType<T>>] | ||
} catch (e: unknown) { | ||
return [e as E, null] | ||
} | ||
} | ||
} | ||
|
||
export default tryit |