generated from 47ng/typescript-library-starter
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplugins.test.ts
63 lines (59 loc) · 1.94 KB
/
plugins.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
import axios from 'axios'
import { nanoid } from 'nanoid'
import path from 'path'
import { createServer, startServer } from '../src'
import { delay } from './jigs/delay'
import './jigs/plugins/decorator' // for declaration merging
describe('Plugins', () => {
beforeEach(() => {
process.env.LOG_LEVEL = 'silent'
})
test('Loading plugins & routes from filesystem', async () => {
const server = createServer({
plugins: {
dir: path.resolve(__dirname, './jigs/plugins')
},
routes: {
dir: path.resolve(__dirname, './jigs/routes')
}
})
await server.ready()
expect(server.testPlugin).toEqual('works')
const res = await server.inject({ method: 'GET', url: '/foo' })
expect(res.statusCode).toEqual(200)
expect(res.json()).toEqual({ foo: 'bar' })
})
test('Graceful exit', async () => {
//jest.setTimeout(10000)
const key = nanoid()
const server = createServer()
server.get('/', async (_, res) => {
await delay(1000)
res.send({ key })
})
await startServer(server)
const resBeforeP = axios.get('http://localhost:3000/')
await delay(100) // Give time to the request to start before shutting down the server
await server.close()
const resBefore = await resBeforeP
expect(resBefore.data.key).toEqual(key)
})
test('Graceful exit with a slow onClose hook', async () => {
const key = nanoid()
const server = createServer()
server.get('/', async (_, res) => {
await delay(1000)
res.send({ key })
})
server.addHook('onClose', async (_, done) => {
await delay(3000) // Simulate slow shutdown of backing services
done()
})
await startServer(server)
const resBeforeP = axios.get('http://localhost:3000/')
await delay(100) // Give time to the request to start before shutting down the server
await server.close()
const resBefore = await resBeforeP
expect(resBefore.data.key).toEqual(key)
})
})