-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.test.ts
71 lines (61 loc) · 2.02 KB
/
errors.test.ts
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { expect, describe, test, assert } from 'vitest'
import { isWasmPanic } from '../src/utils/isWasmPanic'
import * as wasm from '../src/wasm/demo_errors'
// Note: "year" is missing!
const eventAsStr = '{ "name": "EuroRust" }'
describe('demo-errors', () => {
describe('parseWithPanic (no panic hook)', () => {
test('throws a `RuntimeError` saying `unreachable`', () => {
try {
wasm.parseWithPanic(eventAsStr)
assert(false, 'this should fail')
} catch (error) {
const e = error as Error
assert(e instanceof Error)
expect(e.name).toEqual('RuntimeError')
expect(e.message).toMatchInlineSnapshot('"unreachable"')
assert(isWasmPanic(e))
}
})
})
describe('parseWithError', () => {
test('throws an `Error`', () => {
try {
wasm.parseWithError(eventAsStr)
assert(false, 'this should fail')
} catch (error) {
const e = error as Error
assert(e instanceof Error)
expect(e.name).toEqual('Error')
expect(e.message).toMatchInlineSnapshot('"missing field `year` at line 1 column 22"')
assert(!isWasmPanic(e))
}
})
})
describe('parseWithCustomError', () => {
test('throws an `Error` with a custom message', () => {
try {
wasm.parseWithCustomError(eventAsStr)
assert(false, 'this should fail')
} catch (error) {
const e = error as Error
assert(e instanceof Error)
expect(e.name).toEqual('Error')
expect(e.message).toMatchInlineSnapshot('"[CustomError] missing field `year` at line 1 column 22"')
assert(!isWasmPanic(e))
}
})
})
describe('parseWithStringError', () => {
test('throws a `String`', () => {
try {
wasm.parseWithStringError(eventAsStr)
assert(false, 'this should fail')
} catch (error) {
const e = error as string
assert(typeof e === 'string')
expect(e).toMatchInlineSnapshot('"missing field `year` at line 1 column 22"')
}
})
})
})