Skip to content

Commit 8d66cba

Browse files
committed
clean up hub mocks
1 parent 8477f97 commit 8d66cba

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

packages/node/test/handlers.test.ts

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -374,24 +374,21 @@ describe('tracingHandler', () => {
374374
const tracesSampler = jest.fn();
375375
const options = getDefaultNodeClientOptions({ tracesSampler });
376376
const hub = new Hub(new NodeClient(options));
377-
// we need to mock both of these because the tracing handler relies on `@sentry/core` while the sampler relies on
378-
// `@sentry/hub`, and mocking breaks the link between the two
379-
jest.spyOn(sentryCore, 'getCurrentHub').mockReturnValue(hub);
380-
jest.spyOn(sentryHub, 'getCurrentHub').mockReturnValue(hub);
381-
382-
sentryTracingMiddleware(req, res, next);
383-
384-
expect(tracesSampler).toHaveBeenCalledWith(
385-
expect.objectContaining({
386-
request: {
387-
headers,
388-
method,
389-
url: `http://${hostname}${path}?${queryString}`,
390-
cookies: { favorite: 'zukes' },
391-
query_string: queryString,
392-
},
393-
}),
394-
);
377+
hub.run(() => {
378+
sentryTracingMiddleware(req, res, next);
379+
380+
expect(tracesSampler).toHaveBeenCalledWith(
381+
expect.objectContaining({
382+
request: {
383+
headers,
384+
method,
385+
url: `http://${hostname}${path}?${queryString}`,
386+
cookies: { favorite: 'zukes' },
387+
query_string: queryString,
388+
},
389+
}),
390+
);
391+
});
395392
});
396393

397394
it('puts its transaction on the scope', () => {
@@ -773,13 +770,13 @@ describe('errorHandler()', () => {
773770
const hub = new Hub(client, scope);
774771

775772
jest.spyOn<any, any>(client, '_captureRequestSession');
776-
jest.spyOn(sentryCore, 'getCurrentHub').mockReturnValue(hub);
777-
jest.spyOn(sentryHub, 'getCurrentHub').mockReturnValue(hub);
778773

779-
scope?.setRequestSession({ status: 'ok' });
780-
sentryErrorMiddleware({ name: 'error', message: 'this is an error' }, req, res, next);
781-
const requestSession = scope?.getRequestSession();
782-
expect(requestSession).toEqual({ status: 'crashed' });
774+
hub.run(() => {
775+
scope?.setRequestSession({ status: 'ok' });
776+
sentryErrorMiddleware({ name: 'error', message: 'this is an error' }, req, res, next);
777+
const requestSession = scope?.getRequestSession();
778+
expect(requestSession).toEqual({ status: 'crashed' });
779+
});
783780
});
784781

785782
it('when autoSessionTracking is enabled, should not set requestSession status on Crash when it occurs outside the bounds of a request', () => {

0 commit comments

Comments
 (0)