-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.test.js
85 lines (63 loc) · 3.17 KB
/
index.test.js
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
/* eslint-env jest */
const Fastify = require('fastify')
const fastifyDatatog = require('./index')
const dogstatsdMock = {
increment: jest.fn(),
histogram: jest.fn()
}
describe('fastify-datadog', () => {
let fastify
beforeEach(() => {
fastify = Fastify()
})
afterEach(() => {
fastify.close()
jest.clearAllMocks()
})
it('should throw error if dogstatsd option is missing', async () => {
await expect(fastify.register(fastifyDatatog, { dogstatsd: undefined }))
.rejects.toEqual(Error('Missing dogstatsd option.'))
})
it('should track response time', async () => {
fastify.register(fastifyDatatog, { dogstatsd: dogstatsdMock })
fastify.get('/users/:id', async () => 200)
await fastify.inject('/users/123456')
expect(dogstatsdMock.histogram.mock.calls.length).toEqual(1)
expect(dogstatsdMock.histogram.mock.calls[0][0]).toEqual('node.fastify.router.response_time')
expect(dogstatsdMock.histogram.mock.calls[0][1]).toBeGreaterThan(0)
expect(dogstatsdMock.histogram.mock.calls[0][3][0]).toEqual('route:/users/:id')
})
it('should track method', async () => {
fastify.register(fastifyDatatog, { dogstatsd: dogstatsdMock, method: true })
fastify.get('/users/:id', async () => 200)
await fastify.inject('/users/123456')
expect(dogstatsdMock.histogram.mock.calls.length).toEqual(1)
expect(dogstatsdMock.histogram.mock.calls[0][0]).toEqual('node.fastify.router.response_time')
expect(dogstatsdMock.histogram.mock.calls[0][1]).toBeGreaterThan(0)
expect(dogstatsdMock.histogram.mock.calls[0][3][0]).toEqual('route:/users/:id')
expect(dogstatsdMock.histogram.mock.calls[0][3][1]).toEqual('method:get')
})
it('should track response code', async () => {
fastify.register(fastifyDatatog, { dogstatsd: dogstatsdMock, responseCode: true })
fastify.get('/users/:id', async () => 200)
await fastify.inject('/users/123456')
expect(dogstatsdMock.histogram.mock.calls.length).toEqual(1)
expect(dogstatsdMock.histogram.mock.calls[0][0]).toEqual('node.fastify.router.response_time')
expect(dogstatsdMock.histogram.mock.calls[0][1]).toBeGreaterThan(0)
expect(dogstatsdMock.histogram.mock.calls[0][3][0]).toEqual('route:/users/:id')
expect(dogstatsdMock.histogram.mock.calls[0][3][1]).toEqual('response_code:200')
expect(dogstatsdMock.increment.mock.calls.length).toEqual(2)
expect(dogstatsdMock.increment.mock.calls[0][0]).toEqual('node.fastify.router.response_code.200')
expect(dogstatsdMock.increment.mock.calls[1][0]).toEqual('node.fastify.router.response_code.all')
})
it('should track method', async () => {
fastify.register(fastifyDatatog, { dogstatsd: dogstatsdMock, path: true })
fastify.get('/users/:id', async () => 200)
await fastify.inject('/users/123456')
expect(dogstatsdMock.histogram.mock.calls.length).toEqual(1)
expect(dogstatsdMock.histogram.mock.calls[0][0]).toEqual('node.fastify.router.response_time')
expect(dogstatsdMock.histogram.mock.calls[0][1]).toBeGreaterThan(0)
expect(dogstatsdMock.histogram.mock.calls[0][3][0]).toEqual('route:/users/:id')
expect(dogstatsdMock.histogram.mock.calls[0][3][1]).toEqual('path:/users/123456')
})
})