-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartupHealthcheckPlugin.spec.ts
More file actions
123 lines (113 loc) · 3.2 KB
/
startupHealthcheckPlugin.spec.ts
File metadata and controls
123 lines (113 loc) · 3.2 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import type { FastifyInstance } from 'fastify'
import fastify from 'fastify'
import { describe } from 'vitest'
import type { HealthChecker } from './healthcheckCommons.js'
import {
type StartupHealthcheckPluginOptions,
startupHealthcheckPlugin,
} from './startupHealthcheckPlugin.js'
const positiveHealthcheckChecker: HealthChecker = () => {
return Promise.resolve({ result: true })
}
const negativeHealthcheckChecker: HealthChecker = () => {
return Promise.resolve({ error: new Error('Something exploded') })
}
const throwingHealthcheckChecker: HealthChecker = () => {
throw new Error('Connection refused')
}
async function initApp(opts: StartupHealthcheckPluginOptions) {
const app = fastify()
await app.register(startupHealthcheckPlugin, opts)
await app.ready()
return app
}
describe('startupHealthcheckPlugin', () => {
let app: FastifyInstance
afterAll(async () => {
await app?.close()
})
describe('public endpoint', () => {
it('app starts when no healthchecks', async () => {
app = await initApp({ healthChecks: [] })
})
it('throws an error if one mandatory healthcheck fails', async () => {
await expect(
initApp({
healthChecks: [
{
name: 'check1',
isMandatory: true,
checker: negativeHealthcheckChecker,
},
{
name: 'check2',
isMandatory: true,
checker: positiveHealthcheckChecker,
},
],
}),
).rejects.toThrowErrorMatchingInlineSnapshot(`[Error: Healthchecks failed: ["check1"]]`)
})
it('app starts if optional healthcheck fails', async () => {
app = await initApp({
healthChecks: [
{
name: 'check1',
isMandatory: false,
checker: negativeHealthcheckChecker,
},
{
name: 'check2',
isMandatory: true,
checker: positiveHealthcheckChecker,
},
],
})
})
it('app starts if all healthchecks pass', async () => {
app = await initApp({
healthChecks: [
{
name: 'check1',
isMandatory: true,
checker: positiveHealthcheckChecker,
},
{
name: 'check2',
isMandatory: true,
checker: positiveHealthcheckChecker,
},
],
})
})
it('handles checker that throws as a failed healthcheck', async () => {
await expect(
initApp({
healthChecks: [
{
name: 'check1',
isMandatory: true,
checker: throwingHealthcheckChecker,
},
],
}),
).rejects.toThrowErrorMatchingInlineSnapshot(`[Error: Healthchecks failed: ["check1"]]`)
})
it('app starts if throwing checker is optional', async () => {
app = await initApp({
healthChecks: [
{
name: 'check1',
isMandatory: false,
checker: throwingHealthcheckChecker,
},
{
name: 'check2',
isMandatory: true,
checker: positiveHealthcheckChecker,
},
],
})
})
})
})