This repository has been archived by the owner on Aug 16, 2022. It is now read-only.
-
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.
- Loading branch information
1 parent
7dc928f
commit acee839
Showing
7 changed files
with
98 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
Empty file.
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,10 @@ | ||
/** | ||
* @file Test Fixture - ErrnoException | ||
* @module tests/fixtures/ErrnoException | ||
*/ | ||
|
||
const EXCEPTION: NodeJS.ErrnoException = new Error('Test errno message') | ||
|
||
EXCEPTION.code = 'ERRNO' | ||
|
||
export default EXCEPTION |
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,6 @@ | ||
/** | ||
* @file Test Fixture - Error | ||
* @module tests/fixtures/Error | ||
*/ | ||
|
||
export default new Error('Test error message') |
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,48 @@ | ||
import ERRNO_EXCEPTION from '@tests/fixtures/errno-exception.fixture' | ||
import ERROR from '@tests/fixtures/error.fixture' | ||
import type { Testcase } from '@tests/utils/types' | ||
import testSubject from '../ignore-404.util' | ||
|
||
/** | ||
* @file Functional Tests - ignore404 | ||
* @module trext/utils/tests/functional/ignore404 | ||
*/ | ||
|
||
describe('functional:utils/ignore404', () => { | ||
type Case = Testcase<string | undefined> & { | ||
error: Error | NodeJS.ErrnoException | ||
throw: 'throw' | 'not throw' | ||
state: string | ||
} | ||
|
||
const cases: Case[] = [ | ||
{ | ||
error: ERROR, | ||
expected: undefined, | ||
state: 'promise contains Error', | ||
throw: 'not throw' | ||
}, | ||
{ | ||
error: ERRNO_EXCEPTION, | ||
expected: ERRNO_EXCEPTION.code, | ||
state: 'promise contains NodeJS.ErrnoException', | ||
throw: 'throw' | ||
} | ||
] | ||
|
||
it.each<Case>(cases)('should $throw if $state', async testcase => { | ||
// Arrange | ||
const { error, expected } = testcase | ||
let exception: NodeJS.ErrnoException | null = null | ||
|
||
// Act | ||
try { | ||
await testSubject(new Promise((resolve, reject) => reject(error))) | ||
} catch (error) { | ||
exception = error as NodeJS.ErrnoException | ||
} | ||
|
||
// Expect | ||
expect(exception?.code).toBe(expected) | ||
}) | ||
}) |
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,27 @@ | ||
/** | ||
* @file Utilities - ignore404 | ||
* @module trext/utils/ignore404 | ||
*/ | ||
|
||
/** | ||
* Waits for a promise to complete and rejects **only if** an error is thrown | ||
* and the error is **not** a missing file or directory error. | ||
* | ||
* @template T - Data in promise | ||
* | ||
* @async | ||
* @param {Promise<T>} p - Promise to evaluate | ||
* @return {Promise<T | null>} Data in promise or null | ||
* @throws {NodeJS.ErrnoException} | ||
*/ | ||
async function ignore404<T extends any>(p: Promise<T>): Promise<T | null> { | ||
try { | ||
return await p | ||
} catch (error) { | ||
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error | ||
} | ||
|
||
return null | ||
} | ||
|
||
export default ignore404 |
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,6 @@ | ||
/** | ||
* @file Entry Point - Utilities | ||
* @module trext/utils | ||
*/ | ||
|
||
export { default as ignore404 } from './ignore-404.util' |