-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
89 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
// We cannot make any assumptions about what users define as their handler except maybe that it is a function | ||
export interface EdgeRouteHandler { | ||
(req: unknown): unknown | Promise<unknown>; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
(req: any): any | Promise<any>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import * as coreSdk from '@sentry/core'; | ||
import * as sentryTracing from '@sentry/tracing'; | ||
|
||
import { withEdgeWrapping } from '../../src/edge/utils/edgeWrapperUtils'; | ||
|
||
jest.spyOn(sentryTracing, 'hasTracingEnabled').mockImplementation(() => true); | ||
|
||
describe('withEdgeWrapping', () => { | ||
it('should return a function that calls the passed function', async () => { | ||
const origFunctionReturnValue = new Response(); | ||
const origFunction = jest.fn(_req => origFunctionReturnValue); | ||
|
||
const wrappedFunction = withEdgeWrapping(origFunction, { | ||
spanLabel: 'some label', | ||
mechanismFunctionName: 'some name', | ||
spanOp: 'some op', | ||
}); | ||
|
||
const returnValue = await wrappedFunction(new Request('https://sentry.io/')); | ||
|
||
expect(returnValue).toBe(origFunctionReturnValue); | ||
expect(origFunction).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should return a function that calls captureException on error', async () => { | ||
const captureExceptionSpy = jest.spyOn(coreSdk, 'captureException'); | ||
const error = new Error(); | ||
const origFunction = jest.fn(_req => { | ||
throw error; | ||
}); | ||
|
||
const wrappedFunction = withEdgeWrapping(origFunction, { | ||
spanLabel: 'some label', | ||
mechanismFunctionName: 'some name', | ||
spanOp: 'some op', | ||
}); | ||
|
||
await expect(wrappedFunction(new Request('https://sentry.io/'))).rejects.toBe(error); | ||
expect(captureExceptionSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should return a function that starts a transaction when a request object is passed', async () => { | ||
const startTransactionSpy = jest.spyOn(coreSdk, 'startTransaction'); | ||
|
||
const origFunctionReturnValue = new Response(); | ||
const origFunction = jest.fn(_req => origFunctionReturnValue); | ||
|
||
const wrappedFunction = withEdgeWrapping(origFunction, { | ||
spanLabel: 'some label', | ||
mechanismFunctionName: 'some name', | ||
spanOp: 'some op', | ||
}); | ||
|
||
const request = new Request('https://sentry.io/'); | ||
await wrappedFunction(request); | ||
expect(startTransactionSpy).toHaveBeenCalledTimes(1); | ||
expect(startTransactionSpy).toHaveBeenCalledWith( | ||
expect.objectContaining({ metadata: { source: 'route' }, name: 'some label', op: 'some op' }), | ||
{ request }, | ||
); | ||
}); | ||
|
||
it("should return a function that doesn't crash when req isn't passed", async () => { | ||
const origFunctionReturnValue = new Response(); | ||
const origFunction = jest.fn(() => origFunctionReturnValue); | ||
|
||
const wrappedFunction = withEdgeWrapping(origFunction, { | ||
spanLabel: 'some label', | ||
mechanismFunctionName: 'some name', | ||
spanOp: 'some op', | ||
}); | ||
|
||
await expect(wrappedFunction()).resolves.toBe(origFunctionReturnValue); | ||
expect(origFunction).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import 'whatwg-fetch'; // polyfill fetch/Request/Response globals which edge routes need |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters