-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.test.ts
87 lines (73 loc) · 2.44 KB
/
io.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { expect, describe, test, assert, vi, afterEach } from 'vitest'
import * as wasm from '../src/wasm/demo_io'
describe('demo-io', () => {
const fn = vi.fn((n: number) => n + 1)
const asyncFn = (n: number) => {
const inner = fn(n)
return Promise.resolve(inner)
}
describe('simple', () => {
afterEach(() => {
fn.mockClear()
})
test('syncFn', () => {
const result = wasm.callSimpleSyncFn(fn, 2023)
expect(fn).toHaveBeenCalledWith(2023)
expect(fn).toHaveBeenCalledTimes(1)
expect(result).toBe(2024)
})
test('asyncFn', async () => {
const result = await wasm.callSimpleAsyncFn(asyncFn, 2023)
expect(fn).toHaveBeenCalledWith(2023)
expect(fn).toHaveBeenCalledTimes(1)
expect(result).toBe(2024)
})
})
describe('advanced', () => {
afterEach(() => {
fn.mockClear()
})
test('syncFn', () => {
wasm.callAdvancedSyncFn(fn, 2023)
expect(fn).toHaveBeenCalledTimes(1)
expect(fn).toHaveBeenCalledWith(2023)
})
test('asyncFn', async () => {
await wasm.callAdvancedAsyncFn(asyncFn, 2023)
expect(fn).toHaveBeenCalledTimes(1)
expect(fn).toHaveBeenCalledWith(2023)
})
test('syncFnTyped(u32)', () => {
const result = wasm.callAdvancedSyncFnTyped(fn, 2023)
expect(fn).toHaveBeenCalledTimes(1)
expect(fn).toHaveBeenCalledWith(2023)
expect(result).toBe(2024)
})
test('asyncFnTyped(u32)', async () => {
const result = await wasm.callAdvancedAsyncFnTyped(asyncFn, 2023)
expect(fn).toHaveBeenCalledTimes(1)
expect(fn).toHaveBeenCalledWith(2023)
expect(result).toBe(2024)
})
test('syncFnTyped throws when wrong type is provided', () => {
try {
wasm.callAdvancedSyncFnTyped(fn, '2023' as unknown as number)
} catch (error) {
const e = error as Error
expect(fn).toHaveBeenCalledTimes(1)
assert(e instanceof Error)
expect(e.message).toMatchInlineSnapshot('"invalid type: unit value, expected u32"')
}
})
test('asyncFnTyped throws when wrong type is provided', async () => {
try {
await wasm.callAdvancedAsyncFnTyped(asyncFn, '2023' as unknown as number)
} catch (error) {
const e = error as Error
expect(fn).toHaveBeenCalledTimes(1)
assert(e instanceof Error)
expect(e.message).toMatchInlineSnapshot('"invalid type: unit value, expected u32"')
}
})
})
})