Skip to content

Commit f5a8337

Browse files
committed
fix AWS tests
1 parent 8e071b6 commit f5a8337

File tree

1 file changed

+54
-27
lines changed

1 file changed

+54
-27
lines changed

packages/serverless/test/awslambda.test.ts

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ const fakeCallback: Callback = (err, result) => {
3838
return err;
3939
};
4040

41-
function expectScopeSettings() {
41+
function expectScopeSettings(fakeTransactionContext: any) {
4242
// @ts-ignore see "Why @ts-ignore" note
43-
expect(Sentry.fakeScope.setSpan).toBeCalledWith(Sentry.fakeTransaction);
43+
const fakeTransaction = { ...Sentry.fakeTransaction, ...fakeTransactionContext };
44+
// @ts-ignore see "Why @ts-ignore" note
45+
expect(Sentry.fakeScope.setSpan).toBeCalledWith(fakeTransaction);
4446
// @ts-ignore see "Why @ts-ignore" note
4547
expect(Sentry.fakeScope.setTag).toBeCalledWith('server_name', expect.anything());
4648
// @ts-ignore see "Why @ts-ignore" note
@@ -186,13 +188,17 @@ describe('AWSLambda', () => {
186188
};
187189
const wrappedHandler = wrapHandler(handler);
188190
const rv = await wrappedHandler(fakeEvent, fakeContext, fakeCallback);
189-
expect(rv).toStrictEqual(42);
190-
expect(Sentry.startTransaction).toBeCalledWith({
191+
192+
const fakeTransactionContext = {
191193
name: 'functionName',
192194
op: 'awslambda.handler',
193195
metadata: { baggage: [{}, '', true], source: 'component' },
194-
});
195-
expectScopeSettings();
196+
};
197+
198+
expect(rv).toStrictEqual(42);
199+
// @ts-ignore see "Why @ts-ignore" note
200+
expect(Sentry.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
201+
expectScopeSettings(fakeTransactionContext);
196202
// @ts-ignore see "Why @ts-ignore" note
197203
expect(Sentry.fakeTransaction.finish).toBeCalled();
198204
expect(Sentry.flush).toBeCalledWith(2000);
@@ -210,12 +216,15 @@ describe('AWSLambda', () => {
210216
try {
211217
await wrappedHandler(fakeEvent, fakeContext, fakeCallback);
212218
} catch (e) {
213-
expect(Sentry.startTransaction).toBeCalledWith({
219+
const fakeTransactionContext = {
214220
name: 'functionName',
215221
op: 'awslambda.handler',
216222
metadata: { baggage: [{}, '', true], source: 'component' },
217-
});
218-
expectScopeSettings();
223+
};
224+
225+
// @ts-ignore see "Why @ts-ignore" note
226+
expect(Sentry.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
227+
expectScopeSettings(fakeTransactionContext);
219228
expect(Sentry.captureException).toBeCalledWith(error);
220229
// @ts-ignore see "Why @ts-ignore" note
221230
expect(Sentry.fakeTransaction.finish).toBeCalled();
@@ -244,7 +253,8 @@ describe('AWSLambda', () => {
244253
};
245254

246255
const handler: Handler = (_event, _context, callback) => {
247-
expect(Sentry.startTransaction).toBeCalledWith(
256+
// @ts-ignore see "Why @ts-ignore" note
257+
expect(Sentry.fakeHub.startTransaction).toBeCalledWith(
248258
expect.objectContaining({
249259
parentSpanId: '1121201211212012',
250260
parentSampled: false,
@@ -284,15 +294,18 @@ describe('AWSLambda', () => {
284294
fakeEvent.headers = { 'sentry-trace': '12312012123120121231201212312012-1121201211212012-0' };
285295
await wrappedHandler(fakeEvent, fakeContext, fakeCallback);
286296
} catch (e) {
287-
expect(Sentry.startTransaction).toBeCalledWith({
297+
const fakeTransactionContext = {
288298
name: 'functionName',
289299
op: 'awslambda.handler',
290300
traceId: '12312012123120121231201212312012',
291301
parentSpanId: '1121201211212012',
292302
parentSampled: false,
293303
metadata: { baggage: [{}, '', false], source: 'component' },
294-
});
295-
expectScopeSettings();
304+
};
305+
306+
// @ts-ignore see "Why @ts-ignore" note
307+
expect(Sentry.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
308+
expectScopeSettings(fakeTransactionContext);
296309
expect(Sentry.captureException).toBeCalledWith(e);
297310
// @ts-ignore see "Why @ts-ignore" note
298311
expect(Sentry.fakeTransaction.finish).toBeCalled();
@@ -310,13 +323,17 @@ describe('AWSLambda', () => {
310323
};
311324
const wrappedHandler = wrapHandler(handler);
312325
const rv = await wrappedHandler(fakeEvent, fakeContext, fakeCallback);
313-
expect(rv).toStrictEqual(42);
314-
expect(Sentry.startTransaction).toBeCalledWith({
326+
327+
const fakeTransactionContext = {
315328
name: 'functionName',
316329
op: 'awslambda.handler',
317330
metadata: { baggage: [{}, '', true], source: 'component' },
318-
});
319-
expectScopeSettings();
331+
};
332+
333+
expect(rv).toStrictEqual(42);
334+
// @ts-ignore see "Why @ts-ignore" note
335+
expect(Sentry.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
336+
expectScopeSettings(fakeTransactionContext);
320337
// @ts-ignore see "Why @ts-ignore" note
321338
expect(Sentry.fakeTransaction.finish).toBeCalled();
322339
expect(Sentry.flush).toBeCalled();
@@ -345,12 +362,15 @@ describe('AWSLambda', () => {
345362
try {
346363
await wrappedHandler(fakeEvent, fakeContext, fakeCallback);
347364
} catch (e) {
348-
expect(Sentry.startTransaction).toBeCalledWith({
365+
const fakeTransactionContext = {
349366
name: 'functionName',
350367
op: 'awslambda.handler',
351368
metadata: { baggage: [{}, '', true], source: 'component' },
352-
});
353-
expectScopeSettings();
369+
};
370+
371+
// @ts-ignore see "Why @ts-ignore" note
372+
expect(Sentry.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
373+
expectScopeSettings(fakeTransactionContext);
354374
expect(Sentry.captureException).toBeCalledWith(error);
355375
// @ts-ignore see "Why @ts-ignore" note
356376
expect(Sentry.fakeTransaction.finish).toBeCalled();
@@ -383,13 +403,17 @@ describe('AWSLambda', () => {
383403
};
384404
const wrappedHandler = wrapHandler(handler);
385405
const rv = await wrappedHandler(fakeEvent, fakeContext, fakeCallback);
386-
expect(rv).toStrictEqual(42);
387-
expect(Sentry.startTransaction).toBeCalledWith({
406+
407+
const fakeTransactionContext = {
388408
name: 'functionName',
389409
op: 'awslambda.handler',
390410
metadata: { baggage: [{}, '', true], source: 'component' },
391-
});
392-
expectScopeSettings();
411+
};
412+
413+
expect(rv).toStrictEqual(42);
414+
// @ts-ignore see "Why @ts-ignore" note
415+
expect(Sentry.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
416+
expectScopeSettings(fakeTransactionContext);
393417
// @ts-ignore see "Why @ts-ignore" note
394418
expect(Sentry.fakeTransaction.finish).toBeCalled();
395419
expect(Sentry.flush).toBeCalled();
@@ -418,12 +442,15 @@ describe('AWSLambda', () => {
418442
try {
419443
await wrappedHandler(fakeEvent, fakeContext, fakeCallback);
420444
} catch (e) {
421-
expect(Sentry.startTransaction).toBeCalledWith({
445+
const fakeTransactionContext = {
422446
name: 'functionName',
423447
op: 'awslambda.handler',
424448
metadata: { baggage: [{}, '', true], source: 'component' },
425-
});
426-
expectScopeSettings();
449+
};
450+
451+
// @ts-ignore see "Why @ts-ignore" note
452+
expect(Sentry.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
453+
expectScopeSettings(fakeTransactionContext);
427454
expect(Sentry.captureException).toBeCalledWith(error);
428455
// @ts-ignore see "Why @ts-ignore" note
429456
expect(Sentry.fakeTransaction.finish).toBeCalled();

0 commit comments

Comments
 (0)