-
Notifications
You must be signed in to change notification settings - Fork 0
/
panic.test.ts
45 lines (40 loc) · 1.44 KB
/
panic.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
import { expect, describe, test, assert, beforeAll, afterAll } from 'vitest'
import { isWasmPanic, type WasmPanic } from '../src/utils/isWasmPanic'
import * as wasm from '../src/wasm/demo_panic'
import { WasmPanicRegistry } from '../src/utils/WasmPanicRegistry'
import { getWasmPanicError, globalWithWasm } from '../src/utils/getWasmPanicError'
describe('demo-panic', () => {
describe('triggerPanic (with panic hook)', () => {
beforeAll(() => {
wasm.setPanicHook()
/**
* Set up a global registry for Wasm panics.
* This allows us to retrieve the panic message from the Wasm panic hook,
* which is not possible otherwise.
*/
globalWithWasm.WASM_PANIC_REGISTRY = new WasmPanicRegistry()
})
afterAll(() => {
globalWithWasm.WASM_PANIC_REGISTRY = undefined
delete globalWithWasm.WASM_PANIC_REGISTRY
})
test('throws a `RuntimeError` reporting panic info', () => {
try {
wasm.triggerPanic('panic cause')
assert(false, 'this should fail')
} catch (error) {
const e = error as WasmPanic
assert(e instanceof Error)
expect(e.name).toEqual('RuntimeError')
if (isWasmPanic(e)) {
expect(getWasmPanicError(e).message).toMatchInlineSnapshot(`
"panicked at demo-panic/src/lib.rs:13:5:
panic cause"
`)
} else {
assert(false, 'this should fail')
}
}
})
})
})