Skip to content

Commit e4a9dd6

Browse files
committed
add and fix tests
1 parent 43d06c9 commit e4a9dd6

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
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 and puts request in metadata 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 requestDataIntegration = findIntegrationByName(nodeInitOptions.integrations, 'RequestData');
153154

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

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

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ describe('withSentry', () => {
104104
});
105105

106106
describe('tracing', () => {
107-
it('starts a transaction when tracing is enabled', async () => {
107+
it('starts a transaction and sets metadata when tracing is enabled', async () => {
108108
jest
109109
.spyOn(hub.Hub.prototype, 'getClient')
110110
.mockReturnValueOnce({ getOptions: () => ({ tracesSampleRate: 1 } as ClientOptions) } as Client);
@@ -118,6 +118,7 @@ describe('withSentry', () => {
118118

119119
metadata: {
120120
source: 'route',
121+
request: expect.objectContaining({ url: 'http://dogs.are.great' }),
121122
},
122123
},
123124
{ request: expect.objectContaining({ url: 'http://dogs.are.great' }) },

0 commit comments

Comments
 (0)