Skip to content

ref(nextjs): Use real functions in client init tests #4092

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 36 additions & 39 deletions packages/nextjs/test/index.client.test.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,67 @@
import { getCurrentHub } from '@sentry/hub';
import * as SentryReact from '@sentry/react';
import { Integrations as TracingIntegrations } from '@sentry/tracing';
import { Integration } from '@sentry/types';
import { getGlobalObject } from '@sentry/utils';

import { init, Integrations, nextRouterInstrumentation, Scope } from '../src/index.client';
import { init, Integrations, nextRouterInstrumentation } from '../src/index.client';
import { NextjsOptions } from '../src/utils/nextjsOptions';

const { BrowserTracing } = TracingIntegrations;

const mockInit = jest.fn();
let configureScopeCallback: (scope: Scope) => void = () => undefined;

jest.mock('@sentry/react', () => {
const actual = jest.requireActual('@sentry/react');
return {
...actual,
init: (options: NextjsOptions) => {
mockInit(options);
},
configureScope: (callback: (scope: Scope) => void) => {
configureScopeCallback = callback;
},
};
});
const global = getGlobalObject();

const reactInit = jest.spyOn(SentryReact, 'init');

describe('Client init()', () => {
afterEach(() => {
mockInit.mockClear();
configureScopeCallback = () => undefined;
reactInit.mockClear();
global.__SENTRY__.hub = undefined;
});

it('inits the React SDK', () => {
expect(mockInit).toHaveBeenCalledTimes(0);
expect(reactInit).toHaveBeenCalledTimes(0);
init({});
expect(mockInit).toHaveBeenCalledTimes(1);
expect(mockInit).toHaveBeenLastCalledWith({
_metadata: {
sdk: {
name: 'sentry.javascript.nextjs',
version: expect.any(String),
packages: expect.any(Array),
expect(reactInit).toHaveBeenCalledTimes(1);
expect(reactInit).toHaveBeenCalledWith(
expect.objectContaining({
_metadata: {
sdk: {
name: 'sentry.javascript.nextjs',
version: expect.any(String),
packages: expect.any(Array),
},
},
},
environment: 'test',
integrations: undefined,
});
environment: 'test',
integrations: undefined,
}),
);
});

it('sets runtime on scope', () => {
const mockScope = new Scope();
const currentScope = getCurrentHub().getScope();

// @ts-ignore need access to protected _tags attribute
expect(currentScope._tags).toEqual({});

init({});
configureScopeCallback(mockScope);

// @ts-ignore need access to protected _tags attribute
expect(mockScope._tags).toEqual({ runtime: 'browser' });
expect(currentScope._tags).toEqual({ runtime: 'browser' });
});

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

const reactInitOptions: NextjsOptions = mockInit.mock.calls[0][0];
const reactInitOptions: NextjsOptions = reactInit.mock.calls[0][0];
expect(reactInitOptions.integrations).toBeUndefined();
});

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

const reactInitOptions: NextjsOptions = mockInit.mock.calls[0][0];
const reactInitOptions: NextjsOptions = reactInit.mock.calls[0][0];
expect(reactInitOptions.integrations).toHaveLength(1);

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

const reactInitOptions: NextjsOptions = mockInit.mock.calls[0][0];
const reactInitOptions: NextjsOptions = reactInit.mock.calls[0][0];
expect(reactInitOptions.integrations).toHaveLength(1);

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

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

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

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

const reactInitOptions: NextjsOptions = mockInit.mock.calls[0][0];
const reactInitOptions: NextjsOptions = reactInit.mock.calls[0][0];
const integrationFunc = reactInitOptions.integrations as () => Integration[];
const integrations = integrationFunc();
expect((integrations[0] as InstanceType<typeof BrowserTracing>).options).toEqual(
Expand Down