-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add assertion utils separately for main and renderer
- Loading branch information
Showing
12 changed files
with
57 additions
and
25 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,2 +1 @@ | ||
export * from './assert'; | ||
export * from './function'; |
3 changes: 3 additions & 0 deletions
3
packages/app-main/src/main/utils/assert/__tests__/__snapshots__/index.test.ts.snap
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,3 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Test fn \`assertIsNever\` should throw an error 1`] = `""hello world" should be "never" type."`; |
10 changes: 10 additions & 0 deletions
10
packages/app-main/src/main/utils/assert/__tests__/index.test.ts
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 @@ | ||
import { AssertionError } from 'node:assert'; | ||
|
||
import { assertIsNever } from '../index'; | ||
|
||
describe(`Test fn \`${assertIsNever.name}\``, () => { | ||
it('should throw an error', () => { | ||
expect(() => assertIsNever('hello world' as never)).toThrow(AssertionError); | ||
expect(() => assertIsNever('hello world' as never)).toThrowErrorMatchingSnapshot(); | ||
}); | ||
}); |
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,8 @@ | ||
import { AssertionError } from 'node:assert'; | ||
|
||
/** | ||
* The function for asserting whether a value's type is `never`. | ||
*/ | ||
export function assertIsNever(value: never): never { | ||
throw new AssertionError({ message: `"${String(value)}" should be "never" type.` }); | ||
} |
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
3 changes: 2 additions & 1 deletion
3
packages/app-renderer/src/hooks/__tests__/usePersistFn.test.tsx
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
5 changes: 5 additions & 0 deletions
5
packages/app-renderer/src/utils/assert/__tests__/__snapshots__/index.test.ts.snap
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,5 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Test fn \`assert\` should assert an expression 1`] = `"Assertion Error."`; | ||
|
||
exports[`Test fn \`assertIsNever\` should throw an error 1`] = `"hello world should be "never" type."`; |
8 changes: 5 additions & 3 deletions
8
...common/src/utils/__tests__/assert.test.ts → .../src/utils/assert/__tests__/index.test.ts
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 |
---|---|---|
@@ -1,16 +1,18 @@ | ||
import { assert, assertIsNever } from '../assert'; | ||
import { AssertionError, assert, assertIsNever } from '../index'; | ||
|
||
describe(`Test fn \`${assert.name}\``, () => { | ||
it('should assert an expression', () => { | ||
const str = 'hello world'; | ||
|
||
expect(() => assert(typeof str === 'string')).not.toThrow(); | ||
expect(() => assert(typeof str === 'number')).toThrow(); | ||
expect(() => assert(typeof str === 'number')).toThrow(AssertionError); | ||
expect(() => assert(typeof str === 'number')).toThrowErrorMatchingSnapshot(); | ||
}); | ||
}); | ||
|
||
describe(`Test fn \`${assertIsNever.name}\``, () => { | ||
it('should throw an error', () => { | ||
expect(() => assertIsNever('hello world' as never)).toThrow(); | ||
expect(() => assertIsNever('hello world' as never)).toThrow(AssertionError); | ||
expect(() => assertIsNever('hello world' as never)).toThrowErrorMatchingSnapshot(); | ||
}); | ||
}); |
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,17 @@ | ||
export class AssertionError extends Error {} | ||
|
||
/** | ||
* @param value The input that is checked for being truthy. | ||
*/ | ||
export function assert(value: unknown, message?: string): asserts value { | ||
if (!value) { | ||
throw new AssertionError(message ?? 'Assertion Error.'); | ||
} | ||
} | ||
|
||
/** | ||
* The function for asserting whether a value's type is `never`. | ||
*/ | ||
export function assertIsNever(value: never): never { | ||
throw new AssertionError(`${String(value)} should be "never" type.`); | ||
} |