diff --git a/packages/serverless/src/gcpfunction/cloud_events.ts b/packages/serverless/src/gcpfunction/cloud_events.ts index a1cb4eee56d5..429cea995f1a 100644 --- a/packages/serverless/src/gcpfunction/cloud_events.ts +++ b/packages/serverless/src/gcpfunction/cloud_events.ts @@ -72,15 +72,11 @@ function _wrapCloudEventFunction( return (fn as CloudEventFunctionWithCallback)(context, newCallback); } - void Promise.resolve() + return Promise.resolve() .then(() => (fn as CloudEventFunction)(context)) .then( - result => { - newCallback(null, result); - }, - err => { - newCallback(err, undefined); - }, + result => newCallback(null, result), + err => newCallback(err, undefined), ); }; } diff --git a/packages/serverless/src/gcpfunction/events.ts b/packages/serverless/src/gcpfunction/events.ts index 45ef6a1004a3..6b9c7b9b14a7 100644 --- a/packages/serverless/src/gcpfunction/events.ts +++ b/packages/serverless/src/gcpfunction/events.ts @@ -67,15 +67,11 @@ function _wrapEventFunction( return (fn as EventFunctionWithCallback)(data, context, newCallback); } - void Promise.resolve() + return Promise.resolve() .then(() => (fn as EventFunction)(data, context)) .then( - result => { - newCallback(null, result); - }, - err => { - newCallback(err, undefined); - }, + result => newCallback(null, result), + err => newCallback(err, undefined), ); }; } diff --git a/packages/serverless/test/gcpfunction.test.ts b/packages/serverless/test/gcpfunction.test.ts index e5f6094b8172..4b5a00199796 100644 --- a/packages/serverless/test/gcpfunction.test.ts +++ b/packages/serverless/test/gcpfunction.test.ts @@ -206,6 +206,49 @@ describe('GCPFunction', () => { }); }); + describe('wrapEventFunction() as Promise', () => { + test('successful execution', async () => { + expect.assertions(5); + + const func: EventFunction = (_data, _context) => + new Promise(resolve => { + setTimeout(() => { + resolve(42); + }, 10); + }); + const wrappedHandler = wrapEventFunction(func); + await expect(handleEvent(wrappedHandler)).resolves.toBe(42); + expect(Sentry.startTransaction).toBeCalledWith({ name: 'event.type', op: 'gcp.function.event' }); + // @ts-ignore see "Why @ts-ignore" note + expect(Sentry.fakeScope.setSpan).toBeCalledWith(Sentry.fakeTransaction); + // @ts-ignore see "Why @ts-ignore" note + expect(Sentry.fakeTransaction.finish).toBeCalled(); + expect(Sentry.flush).toBeCalledWith(2000); + }); + + test('capture error', async () => { + expect.assertions(6); + + const error = new Error('wat'); + const handler: EventFunction = (_data, _context) => + new Promise((_, reject) => { + setTimeout(() => { + reject(error); + }, 10); + }); + + const wrappedHandler = wrapEventFunction(handler); + await expect(handleEvent(wrappedHandler)).rejects.toThrowError(error); + expect(Sentry.startTransaction).toBeCalledWith({ name: 'event.type', op: 'gcp.function.event' }); + // @ts-ignore see "Why @ts-ignore" note + expect(Sentry.fakeScope.setSpan).toBeCalledWith(Sentry.fakeTransaction); + expect(Sentry.captureException).toBeCalledWith(error); + // @ts-ignore see "Why @ts-ignore" note + expect(Sentry.fakeTransaction.finish).toBeCalled(); + expect(Sentry.flush).toBeCalled(); + }); + }); + describe('wrapEventFunction() with callback', () => { test('successful execution', async () => { expect.assertions(5);