Skip to content

Commit 47aabd8

Browse files
authored
ref(core): Remove getGlobalObject() usage from @sentry/core (#5914)
This also includes a couple of changes to the hub tests which will eventually get moved to core
1 parent 9dbc380 commit 47aabd8

File tree

5 files changed

+25
-32
lines changed

5 files changed

+25
-32
lines changed

packages/core/src/hub.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import {
2323
import {
2424
consoleSandbox,
2525
dateTimestampInSeconds,
26-
getGlobalObject,
2726
getGlobalSingleton,
27+
GLOBAL_OBJ,
2828
isNodeEnv,
2929
logger,
3030
uuid4,
@@ -413,8 +413,7 @@ export class Hub implements HubInterface {
413413
const { release, environment } = (client && client.getOptions()) || {};
414414

415415
// Will fetch userAgent if called from browser sdk
416-
const global = getGlobalObject<{ navigator?: { userAgent?: string } }>();
417-
const { userAgent } = global.navigator || {};
416+
const { userAgent } = GLOBAL_OBJ.navigator || {};
418417

419418
const session = makeSession({
420419
release,
@@ -500,12 +499,11 @@ export class Hub implements HubInterface {
500499
* at the call-site. We always access the carrier through this function, so we can guarantee that `__SENTRY__` is there.
501500
**/
502501
export function getMainCarrier(): Carrier {
503-
const carrier = getGlobalObject();
504-
carrier.__SENTRY__ = carrier.__SENTRY__ || {
502+
GLOBAL_OBJ.__SENTRY__ = GLOBAL_OBJ.__SENTRY__ || {
505503
extensions: {},
506504
hub: undefined,
507505
};
508-
return carrier;
506+
return GLOBAL_OBJ;
509507
}
510508

511509
/**

packages/core/test/lib/base.test.ts

+11-13
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,18 @@ jest.mock('@sentry/utils', () => {
2222
uuid4(): string {
2323
return '42';
2424
},
25-
getGlobalObject(): any {
26-
return {
27-
console: {
28-
log(): void {
29-
// no-empty
30-
},
31-
warn(): void {
32-
// no-empty
33-
},
34-
error(): void {
35-
// no-empty
36-
},
25+
GLOBAL_OBJ: {
26+
console: {
27+
log(): void {
28+
// no-empty
3729
},
38-
};
30+
warn(): void {
31+
// no-empty
32+
},
33+
error(): void {
34+
// no-empty
35+
},
36+
},
3937
},
4038
consoleSandbox(cb: () => any): any {
4139
return cb();

packages/core/test/lib/hint.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { captureEvent, configureScope } from '@sentry/core';
2-
import { getGlobalObject } from '@sentry/utils';
2+
import { GLOBAL_OBJ } from '@sentry/utils';
33

44
import { initAndBind } from '../../src/sdk';
55
import { getDefaultTestClientOptions, TestClient } from '../mocks/client';
@@ -16,7 +16,7 @@ describe('Hint', () => {
1616

1717
afterEach(() => {
1818
jest.clearAllMocks();
19-
delete getGlobalObject().__SENTRY__;
19+
delete GLOBAL_OBJ.__SENTRY__;
2020
});
2121

2222
describe('attachments', () => {

packages/hub/test/global.test.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
/* eslint-disable deprecation/deprecation */
22

3-
import { getGlobalObject } from '@sentry/utils';
3+
import { GLOBAL_OBJ } from '@sentry/utils';
44

55
import { getCurrentHub, getHubFromCarrier, Hub } from '../src';
66

7-
const global = getGlobalObject();
8-
97
describe('global', () => {
108
test('getGlobalHub', () => {
119
expect(getCurrentHub()).toBeTruthy();
12-
expect((global as any).__SENTRY__.hub).toBeTruthy();
10+
expect(GLOBAL_OBJ.__SENTRY__.hub).toBeTruthy();
1311
});
1412

1513
test('getHubFromCarrier', () => {
@@ -22,22 +20,21 @@ describe('global', () => {
2220

2321
test('getGlobalHub', () => {
2422
const newestHub = new Hub(undefined, undefined, 999999);
25-
(global as any).__SENTRY__.hub = newestHub;
23+
GLOBAL_OBJ.__SENTRY__.hub = newestHub;
2624
expect(getCurrentHub()).toBe(newestHub);
2725
});
2826

2927
test('hub extension methods receive correct hub instance', () => {
3028
const newestHub = new Hub(undefined, undefined, 999999);
31-
(global as any).__SENTRY__.hub = newestHub;
29+
GLOBAL_OBJ.__SENTRY__.hub = newestHub;
3230
const fn = jest.fn().mockImplementation(function (...args: []) {
3331
// @ts-ignore typescript complains that this can be `any`
3432
expect(this).toBe(newestHub);
3533
expect(args).toEqual([1, 2, 3]);
3634
});
37-
(global as any).__SENTRY__.extensions = {};
38-
(global as any).__SENTRY__.extensions.testy = fn;
35+
GLOBAL_OBJ.__SENTRY__.extensions = {};
36+
GLOBAL_OBJ.__SENTRY__.extensions.testy = fn;
3937
(getCurrentHub() as any)._callExtensionMethod('testy', 1, 2, 3);
4038
expect(fn).toBeCalled();
4139
});
42-
// (global as any).__SENTRY__
4340
});

packages/hub/test/scope.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
/* eslint-disable deprecation/deprecation */
22

33
import { Event, EventHint, RequestSessionStatus } from '@sentry/types';
4-
import { getGlobalObject } from '@sentry/utils';
4+
import { GLOBAL_OBJ } from '@sentry/utils';
55

66
import { addGlobalEventProcessor, Scope } from '../src';
77

88
describe('Scope', () => {
99
afterEach(() => {
1010
jest.resetAllMocks();
1111
jest.useRealTimers();
12-
getGlobalObject<any>().__SENTRY__.globalEventProcessors = undefined;
12+
GLOBAL_OBJ.__SENTRY__.globalEventProcessors = undefined;
1313
});
1414

1515
describe('attributes modification', () => {

0 commit comments

Comments
 (0)