-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathMetricScope.test.ts
188 lines (149 loc) · 4.2 KB
/
MetricScope.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
import sleep from '../../../test/utils/Sleep';
import { metricScope } from '../MetricScope';
import { MetricsLogger } from '../MetricsLogger';
const mockEnvironment = jest.fn();
jest.mock('../../logger/MetricsLoggerFactory', () => {
return {
createMetricsLogger: () => new MetricsLogger(mockEnvironment),
};
});
test('async scope executes handler function', async () => {
// arrange
let wasInvoked = false;
const handler = metricScope(() => async () => {
await sleep(1);
wasInvoked = true;
});
// act
await handler();
// assert
expect(wasInvoked).toBe(true);
});
test('sync scope executes handler function', async () => {
// arrange
let a = false;
const handler = metricScope(() => () => {
a = true;
});
// act
// the customer can pass in a synchronous function, but we will still return
// an async function back to the Lambda caller
await handler();
// assert
expect(a).toBe(true);
});
test('async scope passes arguments', async () => {
// arrange
let arg1 = false;
let arg2 = '';
const handler = metricScope(() => async (input1: boolean, input2: string) => {
await sleep(1);
arg1 = input1;
arg2 = input2;
});
// act
// the customer can pass in a synchronous function, but we will still return
// an async function back to the Lambda caller
await handler(true, 'success');
// assert
expect(arg1).toBe(true);
expect(arg2).toBe('success');
});
test('async scope returns child function return value', async () => {
// arrange
const expected = true;
const handler = metricScope(() => async () => {
return await Promise.resolve(expected);
});
// act
// the customer can pass in a synchronous function, but we will still return
// an async function back to the Lambda caller
const result = await handler();
// assert
expect(result).toBe(expected);
});
test('sync scope passes arguments', async () => {
// arrange
let arg1 = false;
let arg2 = '';
const handler = metricScope(() => (input1: boolean, input2: string) => {
arg1 = input1;
arg2 = input2;
});
// act
// the customer can pass in a synchronous function, but we will still return
// an async function back to the Lambda caller
await handler(true, 'success');
// assert
expect(arg1).toBe(true);
expect(arg2).toBe('success');
});
test('sync scope returns child function return value', async () => {
// arrange
const expected = true;
const handler = metricScope(() => () => {
return expected;
});
// act
// the customer can pass in a synchronous function, but we will still return
// an async function back to the Lambda caller
const result = await handler();
// assert
expect(result).toBe(expected);
});
test('async scope rejects with child function reject value', async () => {
// arrange
const expected = true;
const handler = metricScope(() => async () => {
return await Promise.reject(expected);
});
// act
const result = handler();
// assert
await expect(result).rejects.toBe(expected);
});
test('sync scope rejects with child function error', async () => {
// arrange
const expected = true;
const handler = metricScope(() => () => {
throw expected;
});
// act
// the customer can pass in a synchronous function, but we will still return
// an async function back to the Lambda caller
const result = handler();
// assert
await expect(result).rejects.toBe(expected);
});
test('async scope flush is still called when child function rejects', async () => {
// arrange
mockEnvironment.mockReset();
const handler = metricScope(() => async () => {
return await Promise.reject('error');
});
// act
try {
await handler();
} catch (e) {
// ignored
}
// assert
expect(mockEnvironment).toHaveBeenCalled();
});
test('sync scope flush is still called when child function throws', async () => {
// arrange
mockEnvironment.mockReset();
const handler = metricScope(() => () => {
throw 'error';
});
// act
// the customer can pass in a synchronous function, but we will still return
// an async function back to the Lambda caller
try {
await handler();
} catch (e) {
// ignored
}
// assert
expect(mockEnvironment).toHaveBeenCalled();
});