Skip to content

Commit bfdaa92

Browse files
committed
ref: Remove remaining usage of makeMain
1 parent d76126b commit bfdaa92

File tree

8 files changed

+128
-783
lines changed

8 files changed

+128
-783
lines changed

packages/gatsby/gatsby-browser.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable no-console */
2-
import { init } from '@sentry/gatsby';
2+
import { getClient, init } from '@sentry/gatsby';
33

44
export function onClientEntry(_, pluginParams) {
55
const isIntialized = isSentryInitialized();
@@ -32,10 +32,7 @@ export function onClientEntry(_, pluginParams) {
3232
}
3333

3434
function isSentryInitialized() {
35-
// Although `window` should exist because we're in the browser (where this script
36-
// is run), and `__SENTRY__.hub` is created when importing the Gatsby SDK, double
37-
// check that in case something weird happens.
38-
return !!(window && window.__SENTRY__ && window.__SENTRY__.hub && window.__SENTRY__.hub.getClient());
35+
return !!getClient();
3936
}
4037

4138
function areSentryOptionsDefined(params) {

packages/gatsby/test/gatsby-browser.test.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
/* eslint-disable @typescript-eslint/no-var-requires */
2-
/* eslint-disable @typescript-eslint/no-explicit-any */
3-
1+
import type { Client } from '@sentry/types';
42
import { onClientEntry } from '../gatsby-browser';
5-
import { browserTracingIntegration } from '../src/index';
3+
import { browserTracingIntegration, getCurrentScope, getIsolationScope, setCurrentClient } from '../src/index';
64

75
(global as any).__SENTRY_RELEASE__ = '683f3a6ab819d47d23abfca9a914c81f0524d35b';
86
(global as any).__SENTRY_DSN__ = 'https://[email protected]/0';
@@ -50,24 +48,21 @@ describe('onClientEntry', () => {
5048
});
5149

5250
describe('inits Sentry once', () => {
51+
beforeEach(() => {
52+
getCurrentScope().clear();
53+
getIsolationScope().clear();
54+
getCurrentScope().setClient(undefined);
55+
});
56+
5357
afterEach(() => {
54-
delete (window as any).__SENTRY__;
5558
(global.console.warn as jest.Mock).mockClear();
5659
(global.console.error as jest.Mock).mockClear();
5760
});
5861

59-
function setMockedSentryInWindow() {
60-
(window as any).__SENTRY__ = {
61-
hub: {
62-
getClient: () => ({
63-
// Empty object mocking the client
64-
}),
65-
},
66-
};
67-
}
68-
6962
it('initialized in injected config, without pluginParams', () => {
70-
setMockedSentryInWindow();
63+
const client = {} as Client;
64+
setCurrentClient(client);
65+
7166
onClientEntry(undefined, { plugins: [] });
7267
// eslint-disable-next-line no-console
7368
expect(console.warn).not.toHaveBeenCalled();
@@ -77,7 +72,9 @@ describe('onClientEntry', () => {
7772
});
7873

7974
it('initialized in injected config, with pluginParams', () => {
80-
setMockedSentryInWindow();
75+
const client = {} as Client;
76+
setCurrentClient(client);
77+
8178
onClientEntry(undefined, { plugins: [], dsn: 'dsn', release: 'release' });
8279
// eslint-disable-next-line no-console
8380
expect((console.warn as jest.Mock).mock.calls[0]).toMatchInlineSnapshot(`

packages/node/test/async/domain.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
/* eslint-disable deprecation/deprecation */
2-
import { Hub, getCurrentHub, getCurrentScope, makeMain, setAsyncContextStrategy, withScope } from '@sentry/core';
2+
import type { Hub } from '@sentry/core';
3+
import { getCurrentHub, getCurrentScope, setAsyncContextStrategy, withScope } from '@sentry/core';
34
import { getIsolationScope, withIsolationScope } from '@sentry/core';
45
import type { Scope } from '@sentry/types';
56

67
import { setDomainAsyncContextStrategy } from '../../src/async/domain';
78

89
describe('setDomainAsyncContextStrategy()', () => {
910
beforeEach(() => {
10-
const hub = new Hub();
11-
12-
makeMain(hub);
11+
getCurrentScope().clear();
12+
getIsolationScope().clear();
1313
});
1414

1515
afterEach(() => {

packages/node/test/async/hooks.test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
/* eslint-disable deprecation/deprecation */
2+
import type { Hub } from '@sentry/core';
23
import {
3-
Hub,
44
getCurrentHub,
55
getCurrentScope,
66
getIsolationScope,
7-
makeMain,
87
setAsyncContextStrategy,
98
withIsolationScope,
109
withScope,
@@ -15,9 +14,8 @@ import { setHooksAsyncContextStrategy } from '../../src/async/hooks';
1514

1615
describe('setHooksAsyncContextStrategy()', () => {
1716
beforeEach(() => {
18-
const hub = new Hub();
19-
20-
makeMain(hub);
17+
getCurrentScope().clear();
18+
getIsolationScope().clear();
2119
});
2220

2321
afterEach(() => {

packages/node/test/handlers.test.ts

Lines changed: 55 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import * as http from 'http';
22
import * as sentryCore from '@sentry/core';
33
import {
44
Hub,
5-
Scope as ScopeClass,
65
Transaction,
76
getClient,
87
getCurrentScope,
98
getIsolationScope,
109
getMainCarrier,
11-
makeMain,
1210
mergeScopeData,
11+
setCurrentClient,
1312
spanToJSON,
13+
withScope,
1414
} from '@sentry/core';
1515
import type { Event, PropagationContext, Scope } from '@sentry/types';
1616
import { SentryError } from '@sentry/utils';
@@ -21,10 +21,14 @@ import { getDefaultNodeClientOptions } from './helper/node-client-options';
2121

2222
describe('requestHandler', () => {
2323
beforeEach(() => {
24+
getCurrentScope().clear();
25+
getIsolationScope().clear();
26+
2427
// Ensure we reset a potentially set acs to use the default
2528
const sentry = getMainCarrier().__SENTRY__;
2629
if (sentry) {
2730
sentry.acs = undefined;
31+
sentry.hub = undefined;
2832
}
2933
});
3034

@@ -38,7 +42,6 @@ describe('requestHandler', () => {
3842
const sentryRequestMiddleware = requestHandler();
3943

4044
let req: http.IncomingMessage, res: http.ServerResponse, next: () => undefined;
41-
let client: NodeClient;
4245

4346
function createNoOpSpy() {
4447
const noop = { noop: () => undefined }; // this is wrapped in an object so jest can spy on it
@@ -63,11 +66,8 @@ describe('requestHandler', () => {
6366

6467
it('autoSessionTracking is enabled, sets requestSession status to ok, when handling a request', done => {
6568
const options = getDefaultNodeClientOptions({ autoSessionTracking: true, release: '1.2' });
66-
client = new NodeClient(options);
67-
// eslint-disable-next-line deprecation/deprecation
68-
const hub = new Hub(client);
69-
// eslint-disable-next-line deprecation/deprecation
70-
makeMain(hub);
69+
const client = new NodeClient(options);
70+
setCurrentClient(client);
7171

7272
let isolationScope: Scope;
7373
sentryRequestMiddleware(req, res, () => {
@@ -83,11 +83,8 @@ describe('requestHandler', () => {
8383

8484
it('autoSessionTracking is disabled, does not set requestSession, when handling a request', done => {
8585
const options = getDefaultNodeClientOptions({ autoSessionTracking: false, release: '1.2' });
86-
client = new NodeClient(options);
87-
// eslint-disable-next-line deprecation/deprecation
88-
const hub = new Hub(client);
89-
// eslint-disable-next-line deprecation/deprecation
90-
makeMain(hub);
86+
const client = new NodeClient(options);
87+
setCurrentClient(client);
9188

9289
let isolationScope: Scope;
9390
sentryRequestMiddleware(req, res, () => {
@@ -103,11 +100,8 @@ describe('requestHandler', () => {
103100

104101
it('autoSessionTracking is enabled, calls _captureRequestSession, on response finish', done => {
105102
const options = getDefaultNodeClientOptions({ autoSessionTracking: true, release: '1.2' });
106-
client = new NodeClient(options);
107-
// eslint-disable-next-line deprecation/deprecation
108-
const hub = new Hub(client);
109-
// eslint-disable-next-line deprecation/deprecation
110-
makeMain(hub);
103+
const client = new NodeClient(options);
104+
setCurrentClient(client);
111105

112106
const captureRequestSession = jest.spyOn<any, any>(client, '_captureRequestSession');
113107

@@ -128,11 +122,8 @@ describe('requestHandler', () => {
128122

129123
it('autoSessionTracking is disabled, does not call _captureRequestSession, on response finish', done => {
130124
const options = getDefaultNodeClientOptions({ autoSessionTracking: false, release: '1.2' });
131-
client = new NodeClient(options);
132-
// eslint-disable-next-line deprecation/deprecation
133-
const hub = new Hub(client);
134-
// eslint-disable-next-line deprecation/deprecation
135-
makeMain(hub);
125+
const client = new NodeClient(options);
126+
setCurrentClient(client);
136127

137128
const captureRequestSession = jest.spyOn<any, any>(client, '_captureRequestSession');
138129

@@ -179,10 +170,8 @@ describe('requestHandler', () => {
179170
});
180171

181172
it('stores request and request data options in `sdkProcessingMetadata`', done => {
182-
// eslint-disable-next-line deprecation/deprecation
183-
const hub = new Hub(new NodeClient(getDefaultNodeClientOptions()));
184-
// eslint-disable-next-line deprecation/deprecation
185-
makeMain(hub);
173+
const client = new NodeClient(getDefaultNodeClientOptions());
174+
setCurrentClient(client);
186175

187176
const requestHandlerOptions = { include: { ip: false } };
188177
const sentryRequestMiddleware = requestHandler(requestHandlerOptions);
@@ -208,6 +197,18 @@ describe('requestHandler', () => {
208197
});
209198

210199
describe('tracingHandler', () => {
200+
beforeEach(() => {
201+
getCurrentScope().clear();
202+
getIsolationScope().clear();
203+
204+
// Ensure we reset a potentially set acs to use the default
205+
const sentry = getMainCarrier().__SENTRY__;
206+
if (sentry) {
207+
sentry.acs = undefined;
208+
sentry.hub = undefined;
209+
}
210+
});
211+
211212
const headers = { ears: 'furry', nose: 'wet', tongue: 'spotted', cookie: 'favorite=zukes' };
212213
const method = 'wagging';
213214
const protocol = 'mutualsniffing';
@@ -218,18 +219,16 @@ describe('tracingHandler', () => {
218219

219220
const sentryTracingMiddleware = tracingHandler();
220221

221-
let hub: Hub, req: http.IncomingMessage, res: http.ServerResponse, next: () => undefined;
222+
let req: http.IncomingMessage, res: http.ServerResponse, next: () => undefined;
222223

223224
function createNoOpSpy() {
224225
const noop = { noop: () => undefined }; // this is wrapped in an object so jest can spy on it
225226
return jest.spyOn(noop, 'noop') as any;
226227
}
227228

228229
beforeEach(() => {
229-
// eslint-disable-next-line deprecation/deprecation
230-
hub = new Hub(new NodeClient(getDefaultNodeClientOptions({ tracesSampleRate: 1.0 })));
231-
// eslint-disable-next-line deprecation/deprecation
232-
makeMain(hub);
230+
const client = new NodeClient(getDefaultNodeClientOptions({ tracesSampleRate: 1.0 }));
231+
setCurrentClient(client);
233232

234233
req = {
235234
headers,
@@ -349,12 +348,10 @@ describe('tracingHandler', () => {
349348
it('extracts request data for sampling context', () => {
350349
const tracesSampler = jest.fn();
351350
const options = getDefaultNodeClientOptions({ tracesSampler });
352-
// eslint-disable-next-line deprecation/deprecation
353-
const hub = new Hub(new NodeClient(options));
354-
// eslint-disable-next-line deprecation/deprecation
355-
makeMain(hub);
351+
const client = new NodeClient(options);
352+
setCurrentClient(client);
356353

357-
hub.run(() => {
354+
withScope(() => {
358355
sentryTracingMiddleware(req, res, next);
359356

360357
expect(tracesSampler).toHaveBeenCalledWith(
@@ -373,10 +370,8 @@ describe('tracingHandler', () => {
373370

374371
it('puts its transaction on the scope', () => {
375372
const options = getDefaultNodeClientOptions({ tracesSampleRate: 1.0 });
376-
// eslint-disable-next-line deprecation/deprecation
377-
const hub = new Hub(new NodeClient(options));
378-
// eslint-disable-next-line deprecation/deprecation
379-
makeMain(hub);
373+
const client = new NodeClient(options);
374+
setCurrentClient(client);
380375

381376
sentryTracingMiddleware(req, res, next);
382377

@@ -521,6 +516,18 @@ describe('tracingHandler', () => {
521516
});
522517

523518
describe('errorHandler()', () => {
519+
beforeEach(() => {
520+
getCurrentScope().clear();
521+
getIsolationScope().clear();
522+
523+
// Ensure we reset a potentially set acs to use the default
524+
const sentry = getMainCarrier().__SENTRY__;
525+
if (sentry) {
526+
sentry.acs = undefined;
527+
sentry.hub = undefined;
528+
}
529+
});
530+
524531
const headers = { ears: 'furry', nose: 'wet', tongue: 'spotted', cookie: 'favorite=zukes' };
525532
const method = 'wagging';
526533
const protocol = 'mutualsniffing';
@@ -561,10 +568,7 @@ describe('errorHandler()', () => {
561568
// by the`requestHandler`)
562569
client.initSessionFlusher();
563570

564-
// eslint-disable-next-line deprecation/deprecation
565-
const hub = new Hub(client);
566-
// eslint-disable-next-line deprecation/deprecation
567-
makeMain(hub);
571+
setCurrentClient(client);
568572

569573
jest.spyOn<any, any>(client, '_captureRequestSession');
570574

@@ -585,11 +589,9 @@ describe('errorHandler()', () => {
585589
it('autoSessionTracking is enabled + requestHandler is not used -> does not set requestSession status on Crash', done => {
586590
const options = getDefaultNodeClientOptions({ autoSessionTracking: false, release: '3.3' });
587591
client = new NodeClient(options);
588-
// eslint-disable-next-line deprecation/deprecation
589-
const hub = new Hub(client);
592+
setCurrentClient(client);
590593

591594
jest.spyOn<any, any>(client, '_captureRequestSession');
592-
jest.spyOn(sentryCore, 'getCurrentHub').mockReturnValue(hub);
593595

594596
getIsolationScope().setRequestSession({ status: 'ok' });
595597

@@ -611,15 +613,12 @@ describe('errorHandler()', () => {
611613
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
612614
// by the`requestHandler`)
613615
client.initSessionFlusher();
614-
const scope = new ScopeClass();
615-
// eslint-disable-next-line deprecation/deprecation
616-
const hub = new Hub(client, scope);
617-
// eslint-disable-next-line deprecation/deprecation
618-
makeMain(hub);
616+
617+
setCurrentClient(client);
619618

620619
jest.spyOn<any, any>(client, '_captureRequestSession');
621620

622-
hub.run(() => {
621+
withScope(() => {
623622
getIsolationScope().setRequestSession({ status: 'ok' });
624623
sentryErrorMiddleware({ name: 'error', message: 'this is an error' }, req, res, () => {
625624
expect(getIsolationScope().getRequestSession()).toEqual({ status: 'crashed' });
@@ -633,11 +632,7 @@ describe('errorHandler()', () => {
633632
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
634633
// by the`requestHandler`)
635634
client.initSessionFlusher();
636-
const scope = new ScopeClass();
637-
// eslint-disable-next-line deprecation/deprecation
638-
const hub = new Hub(client, scope);
639-
// eslint-disable-next-line deprecation/deprecation
640-
makeMain(hub);
635+
setCurrentClient(client);
641636

642637
jest.spyOn<any, any>(client, '_captureRequestSession');
643638

@@ -656,11 +651,7 @@ describe('errorHandler()', () => {
656651
it('stores request in `sdkProcessingMetadata`', done => {
657652
const options = getDefaultNodeClientOptions({});
658653
client = new NodeClient(options);
659-
660-
// eslint-disable-next-line deprecation/deprecation
661-
const hub = new Hub(client);
662-
// eslint-disable-next-line deprecation/deprecation
663-
makeMain(hub);
654+
setCurrentClient(client);
664655

665656
let isolationScope: Scope;
666657
sentryErrorMiddleware({ name: 'error', message: 'this is an error' }, req, res, () => {

0 commit comments

Comments
 (0)