Skip to content

Commit c29785b

Browse files
authored
ref(nextjs): Use real functions in client init tests (#4092)
This changes the structure of the `index.client.ts` tests to stop mocking `configureScope` and `@sentry/react`'s `init` functions and instead spy on the real versions. This makes the structure of the client tests better match the structure of the server tests, and is also necessary for an upcoming PR in which the behavior of those functions is tested directly (meaning they have to actually do the thing they normally would).
1 parent 1b8bfeb commit c29785b

File tree

1 file changed

+36
-39
lines changed

1 file changed

+36
-39
lines changed

packages/nextjs/test/index.client.test.ts

+36-39
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,67 @@
1+
import { getCurrentHub } from '@sentry/hub';
2+
import * as SentryReact from '@sentry/react';
13
import { Integrations as TracingIntegrations } from '@sentry/tracing';
24
import { Integration } from '@sentry/types';
5+
import { getGlobalObject } from '@sentry/utils';
36

4-
import { init, Integrations, nextRouterInstrumentation, Scope } from '../src/index.client';
7+
import { init, Integrations, nextRouterInstrumentation } from '../src/index.client';
58
import { NextjsOptions } from '../src/utils/nextjsOptions';
69

710
const { BrowserTracing } = TracingIntegrations;
811

9-
const mockInit = jest.fn();
10-
let configureScopeCallback: (scope: Scope) => void = () => undefined;
11-
12-
jest.mock('@sentry/react', () => {
13-
const actual = jest.requireActual('@sentry/react');
14-
return {
15-
...actual,
16-
init: (options: NextjsOptions) => {
17-
mockInit(options);
18-
},
19-
configureScope: (callback: (scope: Scope) => void) => {
20-
configureScopeCallback = callback;
21-
},
22-
};
23-
});
12+
const global = getGlobalObject();
13+
14+
const reactInit = jest.spyOn(SentryReact, 'init');
2415

2516
describe('Client init()', () => {
2617
afterEach(() => {
27-
mockInit.mockClear();
28-
configureScopeCallback = () => undefined;
18+
reactInit.mockClear();
19+
global.__SENTRY__.hub = undefined;
2920
});
3021

3122
it('inits the React SDK', () => {
32-
expect(mockInit).toHaveBeenCalledTimes(0);
23+
expect(reactInit).toHaveBeenCalledTimes(0);
3324
init({});
34-
expect(mockInit).toHaveBeenCalledTimes(1);
35-
expect(mockInit).toHaveBeenLastCalledWith({
36-
_metadata: {
37-
sdk: {
38-
name: 'sentry.javascript.nextjs',
39-
version: expect.any(String),
40-
packages: expect.any(Array),
25+
expect(reactInit).toHaveBeenCalledTimes(1);
26+
expect(reactInit).toHaveBeenCalledWith(
27+
expect.objectContaining({
28+
_metadata: {
29+
sdk: {
30+
name: 'sentry.javascript.nextjs',
31+
version: expect.any(String),
32+
packages: expect.any(Array),
33+
},
4134
},
42-
},
43-
environment: 'test',
44-
integrations: undefined,
45-
});
35+
environment: 'test',
36+
integrations: undefined,
37+
}),
38+
);
4639
});
4740

4841
it('sets runtime on scope', () => {
49-
const mockScope = new Scope();
42+
const currentScope = getCurrentHub().getScope();
43+
44+
// @ts-ignore need access to protected _tags attribute
45+
expect(currentScope._tags).toEqual({});
46+
5047
init({});
51-
configureScopeCallback(mockScope);
48+
5249
// @ts-ignore need access to protected _tags attribute
53-
expect(mockScope._tags).toEqual({ runtime: 'browser' });
50+
expect(currentScope._tags).toEqual({ runtime: 'browser' });
5451
});
5552

5653
describe('integrations', () => {
5754
it('does not add BrowserTracing integration by default if tracesSampleRate is not set', () => {
5855
init({});
5956

60-
const reactInitOptions: NextjsOptions = mockInit.mock.calls[0][0];
57+
const reactInitOptions: NextjsOptions = reactInit.mock.calls[0][0];
6158
expect(reactInitOptions.integrations).toBeUndefined();
6259
});
6360

6461
it('adds BrowserTracing integration by default if tracesSampleRate is set', () => {
6562
init({ tracesSampleRate: 1.0 });
6663

67-
const reactInitOptions: NextjsOptions = mockInit.mock.calls[0][0];
64+
const reactInitOptions: NextjsOptions = reactInit.mock.calls[0][0];
6865
expect(reactInitOptions.integrations).toHaveLength(1);
6966

7067
const integrations = reactInitOptions.integrations as Integration[];
@@ -78,7 +75,7 @@ describe('Client init()', () => {
7875
it('adds BrowserTracing integration by default if tracesSampler is set', () => {
7976
init({ tracesSampler: () => true });
8077

81-
const reactInitOptions: NextjsOptions = mockInit.mock.calls[0][0];
78+
const reactInitOptions: NextjsOptions = reactInit.mock.calls[0][0];
8279
expect(reactInitOptions.integrations).toHaveLength(1);
8380

8481
const integrations = reactInitOptions.integrations as Integration[];
@@ -91,7 +88,7 @@ describe('Client init()', () => {
9188

9289
it('supports passing integration through options', () => {
9390
init({ tracesSampleRate: 1.0, integrations: [new Integrations.Breadcrumbs({ console: false })] });
94-
const reactInitOptions: NextjsOptions = mockInit.mock.calls[0][0];
91+
const reactInitOptions: NextjsOptions = reactInit.mock.calls[0][0];
9592
expect(reactInitOptions.integrations).toHaveLength(2);
9693

9794
const integrations = reactInitOptions.integrations as Integration[];
@@ -104,7 +101,7 @@ describe('Client init()', () => {
104101
integrations: [new BrowserTracing({ idleTimeout: 5000, startTransactionOnLocationChange: false })],
105102
});
106103

107-
const reactInitOptions: NextjsOptions = mockInit.mock.calls[0][0];
104+
const reactInitOptions: NextjsOptions = reactInit.mock.calls[0][0];
108105
expect(reactInitOptions.integrations).toHaveLength(1);
109106
const integrations = reactInitOptions.integrations as Integration[];
110107
expect((integrations[0] as InstanceType<typeof BrowserTracing>).options).toEqual(
@@ -122,7 +119,7 @@ describe('Client init()', () => {
122119
integrations: () => [new BrowserTracing({ idleTimeout: 5000, startTransactionOnLocationChange: false })],
123120
});
124121

125-
const reactInitOptions: NextjsOptions = mockInit.mock.calls[0][0];
122+
const reactInitOptions: NextjsOptions = reactInit.mock.calls[0][0];
126123
const integrationFunc = reactInitOptions.integrations as () => Integration[];
127124
const integrations = integrationFunc();
128125
expect((integrations[0] as InstanceType<typeof BrowserTracing>).options).toEqual(

0 commit comments

Comments
 (0)