Skip to content

Commit

Permalink
add and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lobsterkatie committed Sep 12, 2022
1 parent 7480c88 commit e0c4edc
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
70 changes: 70 additions & 0 deletions packages/nextjs/test/config/wrappers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import * as SentryCore from '@sentry/core';
import * as SentryTracing from '@sentry/tracing';
import { IncomingMessage, ServerResponse } from 'http';

import {
withSentryGetServerSideProps,
withSentryServerSideGetInitialProps,
// TODO: Leaving `withSentryGetStaticProps` out for now until we figure out what to do with it
// withSentryGetStaticProps,
// TODO: Leaving these out for now until we figure out pages with no data fetchers
// withSentryServerSideAppGetInitialProps,
// withSentryServerSideDocumentGetInitialProps,
// withSentryServerSideErrorGetInitialProps,
} from '../../src/config/wrappers';

const startTransactionSpy = jest.spyOn(SentryCore, 'startTransaction');
const setMetadataSpy = jest.spyOn(SentryTracing.Transaction.prototype, 'setMetadata');

describe('data-fetching function wrappers', () => {
const route = '/tricks/[trickName]';
let req: IncomingMessage;
let res: ServerResponse;

describe('starts a transaction if tracing enabled', () => {
beforeEach(() => {
req = { headers: {}, url: 'http://dogs.are.great/tricks/kangaroo' } as IncomingMessage;
res = {} as ServerResponse;

jest.spyOn(SentryTracing, 'hasTracingEnabled').mockReturnValueOnce(true);
});

afterEach(() => {
jest.clearAllMocks();
});

test('withSentryGetServerSideProps', async () => {
const origFunction = jest.fn(async () => ({ props: {} }));

const wrappedOriginal = withSentryGetServerSideProps(origFunction, route);
await wrappedOriginal({ req, res } as any);

expect(startTransactionSpy).toHaveBeenCalledWith(
expect.objectContaining({
name: '/tricks/[trickName]',
op: 'nextjs.data.server',
metadata: expect.objectContaining({ source: 'route' }),
}),
);

expect(setMetadataSpy).toHaveBeenCalledWith({ request: req });
});

test('withSentryServerSideGetInitialProps', async () => {
const origFunction = jest.fn(async () => ({}));

const wrappedOriginal = withSentryServerSideGetInitialProps(origFunction);
await wrappedOriginal({ req, res, pathname: route } as any);

expect(startTransactionSpy).toHaveBeenCalledWith(
expect.objectContaining({
name: '/tricks/[trickName]',
op: 'nextjs.data.server',
metadata: expect.objectContaining({ source: 'route' }),
}),
);

expect(setMetadataSpy).toHaveBeenCalledWith({ request: req });
});
});
});
2 changes: 2 additions & 0 deletions packages/nextjs/test/index.server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,10 @@ describe('Server init()', () => {

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

expect(rewriteFramesIntegration).toBeDefined();
expect(requestDataFramesIntegration).toBeDefined();
});

it('supports passing unrelated integrations through options', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/nextjs/test/utils/withSentry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ describe('withSentry', () => {
metadata: {
baggage: expect.any(Array),
source: 'route',
request: expect.objectContaining({ url: 'http://dogs.are.great' }),
},
},
{ request: expect.objectContaining({ url: 'http://dogs.are.great' }) },
Expand Down

0 comments on commit e0c4edc

Please sign in to comment.