This repository has been archived by the owner on Oct 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
195 lines (168 loc) · 5.14 KB
/
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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { spy } from 'simple-spy'
import { createGuard } from './guard'
jest.useFakeTimers()
const context = {
functionName: 'test',
getRemainingTimeInMillis: spy(() => 10000)
}
const callback = (error: any, result: any) => {
if (error) {
throw error
}
}
beforeEach(() => {
context.getRemainingTimeInMillis.reset()
})
test('Set up logger', async () => {
// Arrange
const setupLogger = spy(() => () => {})
const guard = createGuard('logdnaKey', 'dns', setupLogger as any)
const fun = (event: any, logger: any, context: any) => {}
const awsFun = guard(fun)
// Act
await awsFun(null, context as any, callback)
// Assert
expect(setupLogger.callCount).toBe(1)
expect(setupLogger.args[0][0]).toBe('logdnaKey')
expect(setupLogger.args[0][1]).toBe('dns')
})
test('Pass args to lambda', async () => {
// Arrange
expect.assertions(3)
const setupLogger = spy(() => () => ({
log: () => {},
captureException: () => {}
}))
const guard = createGuard('logdnaKey', 'dns', setupLogger as any)
const mockEvenet = { id: 1 }
const fun = (event: any, logger: any, cnx: any) => {
expect(event).toBe(mockEvenet)
expect(typeof logger.log).toBe('function')
expect(cnx).toBe(context)
}
const awsFun = guard(fun)
// Act
await awsFun(mockEvenet, context as any, callback)
})
test('Pass the result to the callback', async () => {
// Arrange
expect.assertions(1)
const setupLogger = spy(() => () => {
log: () => {}
})
const guard = createGuard('logdnaKey', 'dns', setupLogger as any)
const mockEvenet = { id: 1 }
const myResult = { id: 1 }
const myCallback = (error: any, result: any) => {
expect(result).toBe(myResult)
}
const fun = (event: any, logger: any, cnx: any) => {
return myResult
}
const awsFun = guard(fun)
// Act
await awsFun(mockEvenet, context as any, myCallback)
})
test('Capture exception on failure', async () => {
// Arrange
expect.assertions(4)
const captureException = spy(() => null)
const setupLogger = () => () => ({
captureException
})
const guard = createGuard('logdnaKey', 'dns', setupLogger as any)
const mockEvenet = { id: 1 }
const myError = new Error('Random error')
const myCallback = (error: any, result: any) => {
expect(error).toBe(myError)
expect(result).toBe(undefined)
}
const fun = (event: any, logger: any, cnx: any) => {
throw myError
}
const awsFun = guard(fun)
// Act
await awsFun(mockEvenet, context as any, myCallback)
expect(captureException.callCount).toBe(1)
expect(captureException.args[0][0]).toBe(myError)
})
test('Capture a timeout error before timeout', async () => {
// Arrange
;(setTimeout as any).mockClear()
const timeoutTime = 10000
const myContext = {
functionName: 'test',
getRemainingTimeInMillis: spy(() => timeoutTime)
}
const captureException = spy(() => null)
const setupLogger = () => () => ({
captureException
})
const guard = createGuard('logdnaKey', 'dns', setupLogger as any)
const fun = (event: any, logger: any, cnx: any) => {
return new Promise(resolve => {
setTimeout(resolve, timeoutTime + 1)
})
}
const awsFun = guard(fun)
// Act
awsFun(null, myContext as any, callback)
// Assert
expect((setTimeout as any).mock.calls.length).toBe(2)
expect((setTimeout as any).mock.calls[0][1]).toBe(timeoutTime - 500)
expect(captureException.callCount).toBe(0)
jest.runTimersToTime(9500)
expect(captureException.callCount).toBe(1)
expect(captureException.args[0][0].message.includes('Timeout')).toBe(true)
})
test('Extract request stack from header', async () => {
// Arrange
const createLogger = spy(() => {})
const setupLogger = spy(() => createLogger)
const guard = createGuard('logdnaKey', 'dns', setupLogger as any)
const fun = (event: any, logger: any, context: any) => {}
const awsFun = guard(fun)
const event = {
headers: {
'x-request-stack': 'id1,id2,id3'
}
}
// Act
await awsFun(event, context as any, callback)
// Assert
expect(createLogger.callCount).toBe(1)
expect(createLogger.args[0][0]).toBe(context)
expect(createLogger.args[0][1]).toEqual(['id1', 'id2', 'id3'])
})
test('Extract request stack from event', async () => {
// Arrange
const createLogger = spy(() => {})
const setupLogger = spy(() => createLogger)
const guard = createGuard('logdnaKey', 'dns', setupLogger as any)
const fun = (event: any, logger: any, context: any) => {}
const awsFun = guard(fun)
const event = {
requestStack: ['id1', 'id2']
}
// Act
await awsFun(event, context as any, callback)
// Assert
expect(createLogger.callCount).toBe(1)
expect(createLogger.args[0][0]).toBe(context)
expect(createLogger.args[0][1]).toEqual(event.requestStack)
})
test('Parse JSON event', async () => {
// Arrange
const setupLogger = () => () => {}
const guard = createGuard('logdnaKey', 'dns', setupLogger as any)
const fun = spy((event: any, logger: any, context: any) => {})
const awsFun = guard(fun as any)
const event = JSON.stringify({
data: 5
})
// Act
await awsFun(event, context as any, callback)
// Assert
expect(fun.callCount).toBe(1)
expect(fun.args[0][0]).toEqual({ data: 5 })
})