This repository has been archived by the owner on Nov 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.spec.js
173 lines (146 loc) · 4.78 KB
/
middleware.spec.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const expect = require('chai').expect
const middleware = require('./middleware')
describe('express-nemo-performance', function () {
context('valid configuration', () => {
context('minimum configuration', () => {
it('returns middleware with options exposed', () => {
const mw = middleware()
expect(mw.options).to.not.be.undefined
})
})
})
context('start', () => {
context('middleware is called', () => {
let nextCalled = false
let SUT
let next
beforeEach(function () {
nextCalled = false
SUT = middleware().start
})
it('should always call next', function () {
const req = { url: '/api/path' }
const res = {}
next = () => {
nextCalled = true
}
SUT(req, res, next)
expect(nextCalled).to.be.true
})
it('should add performance tracking id to context', function () {
const req = { url: '/api/path' }
const res = {}
next = () => {
nextCalled = true
}
SUT(req, res, next)
expect(req.context.performance).to.not.be.an('undefined')
expect(req.context.performance).to.not.be.null
expect(req.context.performance.trackingId).to.not.be.an('undefined')
expect(req.context.performance.trackingId).to.not.be.null
})
})
})
context('end', function () {
context('with no error', function () {
context('middleware is called', function () {
let endNextCalled = false
let start
let SUT
let req
let res
beforeEach(function () {
req = { url: '/api/path' }
res = {}
endNextCalled = false
const mw = middleware()
start = mw.start
SUT = mw.end
})
it('should always call next', function () {
const nextEnd = () => {
endNextCalled = true
}
start(req, res)
SUT(req, res, nextEnd)
expect(endNextCalled).to.be.true
})
context('when tracking id is not set', function () {
it('should not add performance timing to context', function () {
const nextEnd = () => {
}
SUT(req, res, nextEnd)
expect(req.context).to.be.an('undefined')
})
})
context('when tracking id is set', function () {
it('should add performance timing to context', function () {
const nextEnd = () => {
}
start(req, res)
SUT(req, res, nextEnd)
expect(req.context.performance).to.not.be.an('undefined')
expect(req.context.performance).to.not.be.null
expect(req.context.performance.timing).to.not.be.an('undefined')
expect(req.context.performance.timing).to.not.be.null
expect(req.context.performance.trackingId).to.be.null
})
})
})
})
context('with error', function () {
context('middleware is called', function () {
let nextCalled = false
let start
let SUT
let req
let res
beforeEach(function () {
req = { url: '/api/path' }
res = {}
nextCalled = false
const mw = middleware()
SUT = mw.error
start = mw.start
})
it('should always call next', function () {
const nextError = () => {
nextCalled = true
}
SUT({ err: true }, req, res, nextError)
expect(nextCalled).to.be.true
})
it('should always call next with exception', function () {
let currentError = {}
const nextError = (e) => {
currentError = e
}
const expected = { err: true }
start(req, res)
SUT(expected, req, res, nextError)
expect(expected).to.be.equal(currentError)
})
context('when tracking id is not set', () => {
it('should not add performance timing to context', function () {
const nextError = () => {}
// start(req, res)
SUT({ err: true }, req, res, nextError)
expect(req.context).to.be.an('undefined')
})
})
context('when tracking id is set', () => {
it('should add performance timing to context', function () {
const nextError = () => {
}
start(req, res)
SUT({ err: true }, req, res, nextError)
expect(req.context.performance).to.not.be.an('undefined')
expect(req.context.performance).to.not.be.null
expect(req.context.performance.timing).to.not.be.an('undefined')
expect(req.context.performance.timing).to.not.be.null
})
})
})
})
})
})