Skip to content

Commit a478689

Browse files
committed
fix GCP tests
1 parent c5c67a3 commit a478689

File tree

2 files changed

+151
-61
lines changed

2 files changed

+151
-61
lines changed

packages/serverless/test/__mocks__/@sentry/node.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const fakeHub = {
1111
pushScope: jest.fn(() => fakeScope),
1212
popScope: jest.fn(),
1313
getScope: jest.fn(() => fakeScope),
14+
startTransaction: jest.fn(context => ({ ...fakeTransaction, ...context })),
1415
};
1516
export const fakeScope = {
1617
addEventProcessor: jest.fn(),

packages/serverless/test/gcpfunction.test.ts

Lines changed: 150 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,19 @@ describe('GCPFunction', () => {
110110
};
111111
const wrappedHandler = wrapHttpFunction(handler);
112112
await handleHttp(wrappedHandler);
113-
expect(Sentry.startTransaction).toBeCalledWith({
113+
114+
const fakeTransactionContext = {
114115
name: 'POST /path',
115116
op: 'gcp.function.http',
116117
metadata: { baggage: [{}, '', true], source: 'route' },
117-
});
118+
};
118119
// @ts-ignore see "Why @ts-ignore" note
119-
expect(Sentry.fakeScope.setSpan).toBeCalledWith(Sentry.fakeTransaction);
120+
const fakeTransaction = { ...Sentry.fakeTransaction, ...fakeTransactionContext };
121+
122+
// @ts-ignore see "Why @ts-ignore" note
123+
expect(Sentry.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
124+
// @ts-ignore see "Why @ts-ignore" note
125+
expect(Sentry.fakeScope.setSpan).toBeCalledWith(fakeTransaction);
120126
// @ts-ignore see "Why @ts-ignore" note
121127
expect(Sentry.fakeTransaction.setHttpStatus).toBeCalledWith(200);
122128
// @ts-ignore see "Why @ts-ignore" note
@@ -138,25 +144,29 @@ describe('GCPFunction', () => {
138144
};
139145
await handleHttp(wrappedHandler, traceHeaders);
140146

141-
expect(Sentry.startTransaction).toBeCalledWith(
142-
expect.objectContaining({
143-
name: 'POST /path',
144-
op: 'gcp.function.http',
145-
traceId: '12312012123120121231201212312012',
146-
parentSpanId: '1121201211212012',
147-
parentSampled: false,
148-
metadata: {
149-
baggage: [
150-
{
151-
release: '2.12.1',
152-
},
153-
'',
154-
false,
155-
],
156-
source: 'route',
157-
},
158-
}),
159-
);
147+
const fakeTransactionContext = {
148+
name: 'POST /path',
149+
op: 'gcp.function.http',
150+
traceId: '12312012123120121231201212312012',
151+
parentSpanId: '1121201211212012',
152+
parentSampled: false,
153+
metadata: {
154+
baggage: [
155+
{
156+
release: '2.12.1',
157+
},
158+
'',
159+
false,
160+
],
161+
source: 'route',
162+
},
163+
};
164+
165+
// @ts-ignore see "Why @ts-ignore" note
166+
expect(Sentry.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
167+
168+
// @ts-ignore see "Why @ts-ignore" note
169+
// expect(Sentry.fakeHub.startTransaction).toBeCalledWith(expect.objectContaining(fakeTransactionContext));
160170
});
161171

162172
test('capture error', async () => {
@@ -173,16 +183,22 @@ describe('GCPFunction', () => {
173183
};
174184

175185
await handleHttp(wrappedHandler, trace_headers);
176-
expect(Sentry.startTransaction).toBeCalledWith({
186+
187+
const fakeTransactionContext = {
177188
name: 'POST /path',
178189
op: 'gcp.function.http',
179190
traceId: '12312012123120121231201212312012',
180191
parentSpanId: '1121201211212012',
181192
parentSampled: false,
182193
metadata: { baggage: [{}, '', false], source: 'route' },
183-
});
194+
};
195+
// @ts-ignore see "Why @ts-ignore" note
196+
const fakeTransaction = { ...Sentry.fakeTransaction, ...fakeTransactionContext };
197+
184198
// @ts-ignore see "Why @ts-ignore" note
185-
expect(Sentry.fakeScope.setSpan).toBeCalledWith(Sentry.fakeTransaction);
199+
expect(Sentry.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
200+
// @ts-ignore see "Why @ts-ignore" note
201+
expect(Sentry.fakeScope.setSpan).toBeCalledWith(fakeTransaction);
186202
expect(Sentry.captureException).toBeCalledWith(error);
187203
// @ts-ignore see "Why @ts-ignore" note
188204
expect(Sentry.fakeTransaction.finish).toBeCalled();
@@ -246,13 +262,19 @@ describe('GCPFunction', () => {
246262
};
247263
const wrappedHandler = wrapEventFunction(func);
248264
await expect(handleEvent(wrappedHandler)).resolves.toBe(42);
249-
expect(Sentry.startTransaction).toBeCalledWith({
265+
266+
const fakeTransactionContext = {
250267
name: 'event.type',
251268
op: 'gcp.function.event',
252269
metadata: { source: 'component' },
253-
});
270+
};
271+
// @ts-ignore see "Why @ts-ignore" note
272+
const fakeTransaction = { ...Sentry.fakeTransaction, ...fakeTransactionContext };
273+
254274
// @ts-ignore see "Why @ts-ignore" note
255-
expect(Sentry.fakeScope.setSpan).toBeCalledWith(Sentry.fakeTransaction);
275+
expect(Sentry.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
276+
// @ts-ignore see "Why @ts-ignore" note
277+
expect(Sentry.fakeScope.setSpan).toBeCalledWith(fakeTransaction);
256278
// @ts-ignore see "Why @ts-ignore" note
257279
expect(Sentry.fakeTransaction.finish).toBeCalled();
258280
expect(Sentry.flush).toBeCalledWith(2000);
@@ -267,13 +289,19 @@ describe('GCPFunction', () => {
267289
};
268290
const wrappedHandler = wrapEventFunction(handler);
269291
await expect(handleEvent(wrappedHandler)).rejects.toThrowError(error);
270-
expect(Sentry.startTransaction).toBeCalledWith({
292+
293+
const fakeTransactionContext = {
271294
name: 'event.type',
272295
op: 'gcp.function.event',
273296
metadata: { source: 'component' },
274-
});
297+
};
298+
// @ts-ignore see "Why @ts-ignore" note
299+
const fakeTransaction = { ...Sentry.fakeTransaction, ...fakeTransactionContext };
300+
301+
// @ts-ignore see "Why @ts-ignore" note
302+
expect(Sentry.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
275303
// @ts-ignore see "Why @ts-ignore" note
276-
expect(Sentry.fakeScope.setSpan).toBeCalledWith(Sentry.fakeTransaction);
304+
expect(Sentry.fakeScope.setSpan).toBeCalledWith(fakeTransaction);
277305
expect(Sentry.captureException).toBeCalledWith(error);
278306
// @ts-ignore see "Why @ts-ignore" note
279307
expect(Sentry.fakeTransaction.finish).toBeCalled();
@@ -293,13 +321,19 @@ describe('GCPFunction', () => {
293321
});
294322
const wrappedHandler = wrapEventFunction(func);
295323
await expect(handleEvent(wrappedHandler)).resolves.toBe(42);
296-
expect(Sentry.startTransaction).toBeCalledWith({
324+
325+
const fakeTransactionContext = {
297326
name: 'event.type',
298327
op: 'gcp.function.event',
299328
metadata: { source: 'component' },
300-
});
329+
};
301330
// @ts-ignore see "Why @ts-ignore" note
302-
expect(Sentry.fakeScope.setSpan).toBeCalledWith(Sentry.fakeTransaction);
331+
const fakeTransaction = { ...Sentry.fakeTransaction, ...fakeTransactionContext };
332+
333+
// @ts-ignore see "Why @ts-ignore" note
334+
expect(Sentry.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
335+
// @ts-ignore see "Why @ts-ignore" note
336+
expect(Sentry.fakeScope.setSpan).toBeCalledWith(fakeTransaction);
303337
// @ts-ignore see "Why @ts-ignore" note
304338
expect(Sentry.fakeTransaction.finish).toBeCalled();
305339
expect(Sentry.flush).toBeCalledWith(2000);
@@ -318,13 +352,19 @@ describe('GCPFunction', () => {
318352

319353
const wrappedHandler = wrapEventFunction(handler);
320354
await expect(handleEvent(wrappedHandler)).rejects.toThrowError(error);
321-
expect(Sentry.startTransaction).toBeCalledWith({
355+
356+
const fakeTransactionContext = {
322357
name: 'event.type',
323358
op: 'gcp.function.event',
324359
metadata: { source: 'component' },
325-
});
360+
};
326361
// @ts-ignore see "Why @ts-ignore" note
327-
expect(Sentry.fakeScope.setSpan).toBeCalledWith(Sentry.fakeTransaction);
362+
const fakeTransaction = { ...Sentry.fakeTransaction, ...fakeTransactionContext };
363+
364+
// @ts-ignore see "Why @ts-ignore" note
365+
expect(Sentry.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
366+
// @ts-ignore see "Why @ts-ignore" note
367+
expect(Sentry.fakeScope.setSpan).toBeCalledWith(fakeTransaction);
328368
expect(Sentry.captureException).toBeCalledWith(error);
329369
// @ts-ignore see "Why @ts-ignore" note
330370
expect(Sentry.fakeTransaction.finish).toBeCalled();
@@ -341,13 +381,19 @@ describe('GCPFunction', () => {
341381
};
342382
const wrappedHandler = wrapEventFunction(func);
343383
await expect(handleEvent(wrappedHandler)).resolves.toBe(42);
344-
expect(Sentry.startTransaction).toBeCalledWith({
384+
385+
const fakeTransactionContext = {
345386
name: 'event.type',
346387
op: 'gcp.function.event',
347388
metadata: { source: 'component' },
348-
});
389+
};
349390
// @ts-ignore see "Why @ts-ignore" note
350-
expect(Sentry.fakeScope.setSpan).toBeCalledWith(Sentry.fakeTransaction);
391+
const fakeTransaction = { ...Sentry.fakeTransaction, ...fakeTransactionContext };
392+
393+
// @ts-ignore see "Why @ts-ignore" note
394+
expect(Sentry.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
395+
// @ts-ignore see "Why @ts-ignore" note
396+
expect(Sentry.fakeScope.setSpan).toBeCalledWith(fakeTransaction);
351397
// @ts-ignore see "Why @ts-ignore" note
352398
expect(Sentry.fakeTransaction.finish).toBeCalled();
353399
expect(Sentry.flush).toBeCalledWith(2000);
@@ -362,13 +408,19 @@ describe('GCPFunction', () => {
362408
};
363409
const wrappedHandler = wrapEventFunction(handler);
364410
await expect(handleEvent(wrappedHandler)).rejects.toThrowError(error);
365-
expect(Sentry.startTransaction).toBeCalledWith({
411+
412+
const fakeTransactionContext = {
366413
name: 'event.type',
367414
op: 'gcp.function.event',
368415
metadata: { source: 'component' },
369-
});
416+
};
417+
// @ts-ignore see "Why @ts-ignore" note
418+
const fakeTransaction = { ...Sentry.fakeTransaction, ...fakeTransactionContext };
419+
420+
// @ts-ignore see "Why @ts-ignore" note
421+
expect(Sentry.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
370422
// @ts-ignore see "Why @ts-ignore" note
371-
expect(Sentry.fakeScope.setSpan).toBeCalledWith(Sentry.fakeTransaction);
423+
expect(Sentry.fakeScope.setSpan).toBeCalledWith(fakeTransaction);
372424
expect(Sentry.captureException).toBeCalledWith(error);
373425
// @ts-ignore see "Why @ts-ignore" note
374426
expect(Sentry.fakeTransaction.finish).toBeCalled();
@@ -384,13 +436,19 @@ describe('GCPFunction', () => {
384436
};
385437
const wrappedHandler = wrapEventFunction(handler);
386438
await expect(handleEvent(wrappedHandler)).rejects.toThrowError(error);
387-
expect(Sentry.startTransaction).toBeCalledWith({
439+
440+
const fakeTransactionContext = {
388441
name: 'event.type',
389442
op: 'gcp.function.event',
390443
metadata: { source: 'component' },
391-
});
444+
};
445+
// @ts-ignore see "Why @ts-ignore" note
446+
const fakeTransaction = { ...Sentry.fakeTransaction, ...fakeTransactionContext };
447+
392448
// @ts-ignore see "Why @ts-ignore" note
393-
expect(Sentry.fakeScope.setSpan).toBeCalledWith(Sentry.fakeTransaction);
449+
expect(Sentry.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
450+
// @ts-ignore see "Why @ts-ignore" note
451+
expect(Sentry.fakeScope.setSpan).toBeCalledWith(fakeTransaction);
394452
expect(Sentry.captureException).toBeCalledWith(error);
395453
});
396454
});
@@ -417,13 +475,19 @@ describe('GCPFunction', () => {
417475
};
418476
const wrappedHandler = wrapCloudEventFunction(func);
419477
await expect(handleCloudEvent(wrappedHandler)).resolves.toBe(42);
420-
expect(Sentry.startTransaction).toBeCalledWith({
478+
479+
const fakeTransactionContext = {
421480
name: 'event.type',
422481
op: 'gcp.function.cloud_event',
423482
metadata: { source: 'component' },
424-
});
483+
};
484+
// @ts-ignore see "Why @ts-ignore" note
485+
const fakeTransaction = { ...Sentry.fakeTransaction, ...fakeTransactionContext };
486+
425487
// @ts-ignore see "Why @ts-ignore" note
426-
expect(Sentry.fakeScope.setSpan).toBeCalledWith(Sentry.fakeTransaction);
488+
expect(Sentry.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
489+
// @ts-ignore see "Why @ts-ignore" note
490+
expect(Sentry.fakeScope.setSpan).toBeCalledWith(fakeTransaction);
427491
// @ts-ignore see "Why @ts-ignore" note
428492
expect(Sentry.fakeTransaction.finish).toBeCalled();
429493
expect(Sentry.flush).toBeCalledWith(2000);
@@ -438,13 +502,19 @@ describe('GCPFunction', () => {
438502
};
439503
const wrappedHandler = wrapCloudEventFunction(handler);
440504
await expect(handleCloudEvent(wrappedHandler)).rejects.toThrowError(error);
441-
expect(Sentry.startTransaction).toBeCalledWith({
505+
506+
const fakeTransactionContext = {
442507
name: 'event.type',
443508
op: 'gcp.function.cloud_event',
444509
metadata: { source: 'component' },
445-
});
510+
};
511+
// @ts-ignore see "Why @ts-ignore" note
512+
const fakeTransaction = { ...Sentry.fakeTransaction, ...fakeTransactionContext };
513+
514+
// @ts-ignore see "Why @ts-ignore" note
515+
expect(Sentry.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
446516
// @ts-ignore see "Why @ts-ignore" note
447-
expect(Sentry.fakeScope.setSpan).toBeCalledWith(Sentry.fakeTransaction);
517+
expect(Sentry.fakeScope.setSpan).toBeCalledWith(fakeTransaction);
448518
expect(Sentry.captureException).toBeCalledWith(error);
449519
// @ts-ignore see "Why @ts-ignore" note
450520
expect(Sentry.fakeTransaction.finish).toBeCalled();
@@ -461,13 +531,19 @@ describe('GCPFunction', () => {
461531
};
462532
const wrappedHandler = wrapCloudEventFunction(func);
463533
await expect(handleCloudEvent(wrappedHandler)).resolves.toBe(42);
464-
expect(Sentry.startTransaction).toBeCalledWith({
534+
535+
const fakeTransactionContext = {
465536
name: 'event.type',
466537
op: 'gcp.function.cloud_event',
467538
metadata: { source: 'component' },
468-
});
539+
};
540+
// @ts-ignore see "Why @ts-ignore" note
541+
const fakeTransaction = { ...Sentry.fakeTransaction, ...fakeTransactionContext };
542+
543+
// @ts-ignore see "Why @ts-ignore" note
544+
expect(Sentry.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
469545
// @ts-ignore see "Why @ts-ignore" note
470-
expect(Sentry.fakeScope.setSpan).toBeCalledWith(Sentry.fakeTransaction);
546+
expect(Sentry.fakeScope.setSpan).toBeCalledWith(fakeTransaction);
471547
// @ts-ignore see "Why @ts-ignore" note
472548
expect(Sentry.fakeTransaction.finish).toBeCalled();
473549
expect(Sentry.flush).toBeCalledWith(2000);
@@ -482,13 +558,19 @@ describe('GCPFunction', () => {
482558
};
483559
const wrappedHandler = wrapCloudEventFunction(handler);
484560
await expect(handleCloudEvent(wrappedHandler)).rejects.toThrowError(error);
485-
expect(Sentry.startTransaction).toBeCalledWith({
561+
562+
const fakeTransactionContext = {
486563
name: 'event.type',
487564
op: 'gcp.function.cloud_event',
488565
metadata: { source: 'component' },
489-
});
566+
};
490567
// @ts-ignore see "Why @ts-ignore" note
491-
expect(Sentry.fakeScope.setSpan).toBeCalledWith(Sentry.fakeTransaction);
568+
const fakeTransaction = { ...Sentry.fakeTransaction, ...fakeTransactionContext };
569+
570+
// @ts-ignore see "Why @ts-ignore" note
571+
expect(Sentry.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
572+
// @ts-ignore see "Why @ts-ignore" note
573+
expect(Sentry.fakeScope.setSpan).toBeCalledWith(fakeTransaction);
492574
expect(Sentry.captureException).toBeCalledWith(error);
493575
// @ts-ignore see "Why @ts-ignore" note
494576
expect(Sentry.fakeTransaction.finish).toBeCalled();
@@ -504,13 +586,20 @@ describe('GCPFunction', () => {
504586
};
505587
const wrappedHandler = wrapCloudEventFunction(handler);
506588
await expect(handleCloudEvent(wrappedHandler)).rejects.toThrowError(error);
507-
expect(Sentry.startTransaction).toBeCalledWith({
589+
590+
const fakeTransactionContext = {
508591
name: 'event.type',
509592
op: 'gcp.function.cloud_event',
510593
metadata: { source: 'component' },
511-
});
594+
};
595+
// @ts-ignore see "Why @ts-ignore" note
596+
const fakeTransaction = { ...Sentry.fakeTransaction, ...fakeTransactionContext };
597+
598+
// @ts-ignore see "Why @ts-ignore" note
599+
expect(Sentry.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
512600
// @ts-ignore see "Why @ts-ignore" note
513-
expect(Sentry.fakeScope.setSpan).toBeCalledWith(Sentry.fakeTransaction);
601+
expect(Sentry.fakeScope.setSpan).toBeCalledWith(fakeTransaction);
602+
514603
expect(Sentry.captureException).toBeCalledWith(error);
515604
});
516605
});

0 commit comments

Comments
 (0)