Skip to content

Commit 47516bb

Browse files
committed
fix gatsby
1 parent c8449f1 commit 47516bb

File tree

2 files changed

+16
-22
lines changed

2 files changed

+16
-22
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(`

0 commit comments

Comments
 (0)