Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lobsterkatie committed Oct 20, 2022
1 parent f6b93a7 commit def4408
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
50 changes: 49 additions & 1 deletion packages/node/test/handlers.test.ts
@@ -1,5 +1,5 @@
import * as sentryCore from '@sentry/core';
import { Hub, Scope } from '@sentry/core';
import { Hub, makeMain, Scope } from '@sentry/core';
import { Transaction } from '@sentry/tracing';
import { Event } from '@sentry/types';
import { SentryError } from '@sentry/utils';
Expand Down Expand Up @@ -136,6 +136,22 @@ describe('requestHandler', () => {
done();
});
});

it('stores request and request data options in `sdkProcessingMetadata`', () => {
const hub = new Hub(new NodeClient(getDefaultNodeClientOptions()));
jest.spyOn(sentryCore, 'getCurrentHub').mockReturnValue(hub);

const requestHandlerOptions = { include: { ip: false } };
const sentryRequestMiddleware = requestHandler(requestHandlerOptions);

sentryRequestMiddleware(req, res, next);

const scope = sentryCore.getCurrentHub().getScope();
expect((scope as any)._sdkProcessingMetadata).toEqual({
request: req,
requestDataOptionsFromExpressHandler: requestHandlerOptions,
});
});
});

describe('tracingHandler', () => {
Expand Down Expand Up @@ -392,6 +408,19 @@ describe('tracingHandler', () => {
done();
});
});

it('stores request in transaction metadata', () => {
const options = getDefaultNodeClientOptions({ tracesSampleRate: 1.0 });
const hub = new Hub(new NodeClient(options));

jest.spyOn(sentryCore, 'getCurrentHub').mockReturnValue(hub);

sentryTracingMiddleware(req, res, next);

const transaction = sentryCore.getCurrentHub().getScope()?.getTransaction();

expect(transaction?.metadata.request).toEqual(req);
});
});

describe('errorHandler()', () => {
Expand Down Expand Up @@ -498,4 +527,23 @@ describe('errorHandler()', () => {
const requestSession = scope?.getRequestSession();
expect(requestSession).toEqual(undefined);
});

it('stores request in `sdkProcessingMetadata`', () => {
const options = getDefaultNodeClientOptions({});
client = new NodeClient(options);

const hub = new Hub(client);
makeMain(hub);

// `sentryErrorMiddleware` uses `withScope`, and we need access to the temporary scope it creates, so monkeypatch
// `captureException` in order to examine the scope as it exists inside the `withScope` callback
hub.captureException = function (this: Hub, _exception: any) {
const scope = this.getScope();
expect((scope as any)._sdkProcessingMetadata.request).toEqual(req);
} as any;

sentryErrorMiddleware({ name: 'error', message: 'this is an error' }, req, res, next);

expect.assertions(1);
});
});
21 changes: 21 additions & 0 deletions packages/node/test/integrations/requestdata.test.ts
Expand Up @@ -3,6 +3,7 @@ import { Event, EventProcessor } from '@sentry/types';
import * as http from 'http';

import { NodeClient } from '../../src/client';
import { requestHandler } from '../../src/handlers';
import { RequestData, RequestDataIntegrationOptions } from '../../src/integrations/requestdata';
import * as requestDataModule from '../../src/requestdata';
import { getDefaultNodeClientOptions } from '../helper/node-client-options';
Expand Down Expand Up @@ -100,4 +101,24 @@ describe('`RequestData` integration', () => {
expect(passedOptions?.include?.user).not.toEqual(expect.arrayContaining(['email']));
});
});

describe('usage with express request handler', () => {
it('uses options from request handler', async () => {
const sentryRequestMiddleware = requestHandler({ include: { transaction: 'methodPath' } });
const res = new http.ServerResponse(req);
const next = jest.fn();

initWithRequestDataIntegrationOptions({ transactionNamingScheme: 'path' });

sentryRequestMiddleware(req, res, next);

await getCurrentHub().getScope()!.applyToEvent(event, {});
requestDataEventProcessor(event);

const passedOptions = addRequestDataToEventSpy.mock.calls[0][2];

// `transaction` matches the request middleware's option, not the integration's option
expect(passedOptions?.include).toEqual(expect.objectContaining({ transaction: 'methodPath' }));
});
});
});

0 comments on commit def4408

Please sign in to comment.