1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
import { describe, expect, test } from 'vitest'
import { assert } from '@hollowdark/utils/assert/assert'
import { assertDefined } from '@hollowdark/utils/assert/assert-defined'
import { assertNever } from '@hollowdark/utils/assert/assert-never'
describe('assert', () => {
test('passes on truthy condition', () => {
expect(() => assert(true)).not.toThrow()
expect(() => assert(1)).not.toThrow()
expect(() => assert('non-empty')).not.toThrow()
})
test('throws on falsy condition', () => {
expect(() => assert(false)).toThrow(/Assertion failed/)
expect(() => assert(0)).toThrow()
expect(() => assert(null)).toThrow()
expect(() => assert(undefined)).toThrow()
expect(() => assert('')).toThrow()
})
test('uses supplied message', () => {
expect(() => assert(false, 'custom reason')).toThrow(/custom reason/)
})
})
describe('assertDefined', () => {
test('returns the value when defined', () => {
expect(assertDefined(5)).toBe(5)
expect(assertDefined('')).toBe('')
expect(assertDefined(false)).toBe(false)
expect(assertDefined(0)).toBe(0)
})
test('throws on null / undefined', () => {
expect(() => assertDefined(null)).toThrow()
expect(() => assertDefined(undefined)).toThrow()
})
test('uses supplied message', () => {
expect(() => assertDefined(null, 'nope')).toThrow(/nope/)
})
})
describe('assertNever', () => {
test('always throws', () => {
expect(() => assertNever('x' as never)).toThrow()
})
})
|