Skip to content

Commit e0c4edc

Browse files
committed
add and fix tests
1 parent 7480c88 commit e0c4edc

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import * as SentryCore from '@sentry/core';
2+
import * as SentryTracing from '@sentry/tracing';
3+
import { IncomingMessage, ServerResponse } from 'http';
4+
5+
import {
6+
withSentryGetServerSideProps,
7+
withSentryServerSideGetInitialProps,
8+
// TODO: Leaving `withSentryGetStaticProps` out for now until we figure out what to do with it
9+
// withSentryGetStaticProps,
10+
// TODO: Leaving these out for now until we figure out pages with no data fetchers
11+
// withSentryServerSideAppGetInitialProps,
12+
// withSentryServerSideDocumentGetInitialProps,
13+
// withSentryServerSideErrorGetInitialProps,
14+
} from '../../src/config/wrappers';
15+
16+
const startTransactionSpy = jest.spyOn(SentryCore, 'startTransaction');
17+
const setMetadataSpy = jest.spyOn(SentryTracing.Transaction.prototype, 'setMetadata');
18+
19+
describe('data-fetching function wrappers', () => {
20+
const route = '/tricks/[trickName]';
21+
let req: IncomingMessage;
22+
let res: ServerResponse;
23+
24+
describe('starts a transaction if tracing enabled', () => {
25+
beforeEach(() => {
26+
req = { headers: {}, url: 'http://dogs.are.great/tricks/kangaroo' } as IncomingMessage;
27+
res = {} as ServerResponse;
28+
29+
jest.spyOn(SentryTracing, 'hasTracingEnabled').mockReturnValueOnce(true);
30+
});
31+
32+
afterEach(() => {
33+
jest.clearAllMocks();
34+
});
35+
36+
test('withSentryGetServerSideProps', async () => {
37+
const origFunction = jest.fn(async () => ({ props: {} }));
38+
39+
const wrappedOriginal = withSentryGetServerSideProps(origFunction, route);
40+
await wrappedOriginal({ req, res } as any);
41+
42+
expect(startTransactionSpy).toHaveBeenCalledWith(
43+
expect.objectContaining({
44+
name: '/tricks/[trickName]',
45+
op: 'nextjs.data.server',
46+
metadata: expect.objectContaining({ source: 'route' }),
47+
}),
48+
);
49+
50+
expect(setMetadataSpy).toHaveBeenCalledWith({ request: req });
51+
});
52+
53+
test('withSentryServerSideGetInitialProps', async () => {
54+
const origFunction = jest.fn(async () => ({}));
55+
56+
const wrappedOriginal = withSentryServerSideGetInitialProps(origFunction);
57+
await wrappedOriginal({ req, res, pathname: route } as any);
58+
59+
expect(startTransactionSpy).toHaveBeenCalledWith(
60+
expect.objectContaining({
61+
name: '/tricks/[trickName]',
62+
op: 'nextjs.data.server',
63+
metadata: expect.objectContaining({ source: 'route' }),
64+
}),
65+
);
66+
67+
expect(setMetadataSpy).toHaveBeenCalledWith({ request: req });
68+
});
69+
});
70+
});

packages/nextjs/test/index.server.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,10 @@ describe('Server init()', () => {
150150

151151
const nodeInitOptions = nodeInit.mock.calls[0][0] as ModifiedInitOptions;
152152
const rewriteFramesIntegration = findIntegrationByName(nodeInitOptions.integrations, 'RewriteFrames');
153+
const requestDataFramesIntegration = findIntegrationByName(nodeInitOptions.integrations, 'RequestData');
153154

154155
expect(rewriteFramesIntegration).toBeDefined();
156+
expect(requestDataFramesIntegration).toBeDefined();
155157
});
156158

157159
it('supports passing unrelated integrations through options', () => {

packages/nextjs/test/utils/withSentry.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ describe('withSentry', () => {
119119
metadata: {
120120
baggage: expect.any(Array),
121121
source: 'route',
122+
request: expect.objectContaining({ url: 'http://dogs.are.great' }),
122123
},
123124
},
124125
{ request: expect.objectContaining({ url: 'http://dogs.are.great' }) },

0 commit comments

Comments
 (0)