-
Notifications
You must be signed in to change notification settings - Fork 6
/
adapter.test.ts
82 lines (75 loc) · 2.07 KB
/
adapter.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
import test from 'ava'
import { expose, start } from '../src'
import { Adapter, AdapterEndpoint } from '../src/adapter'
import { NopTransport, TestAdapter } from '../src/util/testing-utils'
test('duplicate endpoint names throw error on startup', async (t) => {
const adapter = new Adapter({
name: 'TEST',
endpoints: [
new AdapterEndpoint({
name: 'test',
transport: new NopTransport(),
}),
new AdapterEndpoint({
name: 'another',
aliases: ['test'],
transport: new NopTransport(),
}),
],
})
await t.throwsAsync(async () => expose(adapter), {
message: 'Duplicate endpoint / alias: "test"',
})
})
test('lowercase adapter name throws error on startup', async (t) => {
const adapter = new Adapter({
// @ts-expect-error - tests that lowercase names throw errors in runtime
name: 'test',
endpoints: [
new AdapterEndpoint({
name: 'test',
transport: new NopTransport(),
}),
],
})
await t.throwsAsync(async () => expose(adapter), {
message: 'Adapter name must be uppercase',
})
})
test('Bootstrap function runs if provided', async (t) => {
const adapter = new Adapter({
name: 'TEST',
endpoints: [
new AdapterEndpoint({
name: 'test',
transport: new NopTransport(),
}),
],
bootstrap: async (ea) => {
ea.name = 'BOOTSTRAPPED'
},
})
await start(adapter)
t.is(adapter.name, 'BOOTSTRAPPED')
})
test.serial('Throws when transport.registerRequest errors', async (t) => {
const adapter = new Adapter({
name: 'TEST',
endpoints: [
new AdapterEndpoint({
name: 'test',
transport: new (class extends NopTransport {
async registerRequest() {
throw new Error('Error from registerRequest')
}
})(),
}),
],
})
const testAdapter = await TestAdapter.start(adapter, { testAdapter: {} as TestAdapter })
const error = await testAdapter.request({
endpoint: 'test',
})
t.is(error.statusCode, 500)
t.is(error.body, 'Error from registerRequest')
})