Skip to content

Commit 1eb3183

Browse files
committed
Add tests
1 parent dcfa0be commit 1eb3183

File tree

8 files changed

+89
-7
lines changed

8 files changed

+89
-7
lines changed

packages/nextjs/jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ module.exports = {
55
// This prevents the build tests from running when unit tests run. (If they do, they fail, because the build being
66
// tested hasn't necessarily run yet.)
77
testPathIgnorePatterns: ['<rootDir>/test/buildProcess/'],
8+
setupFiles: ['<rootDir>/test/setupUnitTests.ts'],
89
};

packages/nextjs/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"devDependencies": {
3333
"@types/webpack": "^4.41.31",
3434
"eslint-plugin-react": "^7.31.11",
35-
"next": "10.1.3"
35+
"next": "10.1.3",
36+
"whatwg-fetch": "3.6.2"
3637
},
3738
"peerDependencies": {
3839
"next": "^10.0.8 || ^11.0 || ^12.0 || ^13.0",

packages/nextjs/src/edge/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// We cannot make any assumptions about what users define as their handler except maybe that it is a function
22
export interface EdgeRouteHandler {
3-
(req: unknown): unknown | Promise<unknown>;
3+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4+
(req: any): any | Promise<any>;
45
}

packages/nextjs/src/edge/utils/edgeWrapperUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export function withEdgeWrapping<H extends EdgeRouteHandler>(
6565
try {
6666
const handlerResult: ReturnType<H> = await handler.apply(this, args);
6767

68-
if (handlerResult instanceof Response) {
68+
if ((handlerResult as unknown) instanceof Response) {
6969
span?.setHttpStatus(handlerResult.status);
7070
} else {
7171
span?.setStatus('ok');

packages/nextjs/src/index.types.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ export declare function flush(timeout?: number | undefined): PromiseLike<boolean
3030
export declare function lastEventId(): string | undefined;
3131
export declare function getSentryRelease(fallback?: string): string | undefined;
3232

33-
export declare function withSentryAPI<H extends (...args: any[]) => any>(
34-
handler: H,
33+
export declare function withSentryAPI<APIHandler extends (...args: any[]) => any>(
34+
handler: APIHandler,
3535
parameterizedRoute: string,
36-
): (...args: Parameters<H>) => ReturnType<H> extends Promise<unknown> ? ReturnType<H> : Promise<ReturnType<H>>;
36+
): (
37+
...args: Parameters<APIHandler>
38+
) => ReturnType<APIHandler> extends Promise<unknown> ? ReturnType<APIHandler> : Promise<ReturnType<APIHandler>>;
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import * as coreSdk from '@sentry/core';
2+
import * as sentryTracing from '@sentry/tracing';
3+
4+
import { withEdgeWrapping } from '../../src/edge/utils/edgeWrapperUtils';
5+
6+
jest.spyOn(sentryTracing, 'hasTracingEnabled').mockImplementation(() => true);
7+
8+
describe('withEdgeWrapping', () => {
9+
it('should return a function that calls the passed function', async () => {
10+
const origFunctionReturnValue = new Response();
11+
const origFunction = jest.fn(_req => origFunctionReturnValue);
12+
13+
const wrappedFunction = withEdgeWrapping(origFunction, {
14+
spanLabel: 'some label',
15+
mechanismFunctionName: 'some name',
16+
spanOp: 'some op',
17+
});
18+
19+
const returnValue = await wrappedFunction(new Request('https://sentry.io/'));
20+
21+
expect(returnValue).toBe(origFunctionReturnValue);
22+
expect(origFunction).toHaveBeenCalledTimes(1);
23+
});
24+
25+
it('should return a function that calls captureException on error', async () => {
26+
const captureExceptionSpy = jest.spyOn(coreSdk, 'captureException');
27+
const error = new Error();
28+
const origFunction = jest.fn(_req => {
29+
throw error;
30+
});
31+
32+
const wrappedFunction = withEdgeWrapping(origFunction, {
33+
spanLabel: 'some label',
34+
mechanismFunctionName: 'some name',
35+
spanOp: 'some op',
36+
});
37+
38+
await expect(wrappedFunction(new Request('https://sentry.io/'))).rejects.toBe(error);
39+
expect(captureExceptionSpy).toHaveBeenCalledTimes(1);
40+
});
41+
42+
it('should return a function that starts a transaction when a request object is passed', async () => {
43+
const startTransactionSpy = jest.spyOn(coreSdk, 'startTransaction');
44+
45+
const origFunctionReturnValue = new Response();
46+
const origFunction = jest.fn(_req => origFunctionReturnValue);
47+
48+
const wrappedFunction = withEdgeWrapping(origFunction, {
49+
spanLabel: 'some label',
50+
mechanismFunctionName: 'some name',
51+
spanOp: 'some op',
52+
});
53+
54+
const request = new Request('https://sentry.io/');
55+
await wrappedFunction(request);
56+
expect(startTransactionSpy).toHaveBeenCalledTimes(1);
57+
expect(startTransactionSpy).toHaveBeenCalledWith(
58+
expect.objectContaining({ metadata: { source: 'route' }, name: 'some label', op: 'some op' }),
59+
{ request },
60+
);
61+
});
62+
63+
it("should return a function that doesn't crash when req isn't passed", async () => {
64+
const origFunctionReturnValue = new Response();
65+
const origFunction = jest.fn(() => origFunctionReturnValue);
66+
67+
const wrappedFunction = withEdgeWrapping(origFunction, {
68+
spanLabel: 'some label',
69+
mechanismFunctionName: 'some name',
70+
spanOp: 'some op',
71+
});
72+
73+
await expect(wrappedFunction()).resolves.toBe(origFunctionReturnValue);
74+
expect(origFunction).toHaveBeenCalledTimes(1);
75+
});
76+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import 'whatwg-fetch'; // polyfill fetch/Request/Response globals which edge routes need

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25053,7 +25053,7 @@ whatwg-encoding@^2.0.0:
2505325053
dependencies:
2505425054
iconv-lite "0.6.3"
2505525055

25056-
whatwg-fetch@>=0.10.0:
25056+
whatwg-fetch@3.6.2, whatwg-fetch@>=0.10.0:
2505725057
version "3.6.2"
2505825058
resolved "https://registry.yarnpkg.com/whatwg-fetch/-/whatwg-fetch-3.6.2.tgz#dced24f37f2624ed0281725d51d0e2e3fe677f8c"
2505925059
integrity sha512-bJlen0FcuU/0EMLrdbJ7zOnW6ITZLrZMIarMUVmdKtsGvZna8vxKYaexICWPfZ8qwf9fzNq+UEIZrnSaApt6RA==

0 commit comments

Comments
 (0)