|
| 1 | +import { Integrations as TracingIntegrations } from '@sentry/tracing'; |
| 2 | +import { Integration } from '@sentry/types'; |
| 3 | + |
| 4 | +import { init, Integrations, nextRouterInstrumentation, Scope } from '../src/index.client'; |
| 5 | +import { NextjsOptions } from '../src/utils/nextjsOptions'; |
| 6 | + |
| 7 | +const { BrowserTracing } = TracingIntegrations; |
| 8 | + |
| 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 | +}); |
| 24 | + |
| 25 | +describe('Client init()', () => { |
| 26 | + afterEach(() => { |
| 27 | + mockInit.mockClear(); |
| 28 | + configureScopeCallback = () => undefined; |
| 29 | + }); |
| 30 | + |
| 31 | + it('inits the React SDK', () => { |
| 32 | + expect(mockInit).toHaveBeenCalledTimes(0); |
| 33 | + 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), |
| 41 | + }, |
| 42 | + }, |
| 43 | + environment: 'test', |
| 44 | + integrations: expect.any(Array), |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + it('sets runtime on scope', () => { |
| 49 | + const mockScope = new Scope(); |
| 50 | + init({}); |
| 51 | + configureScopeCallback(mockScope); |
| 52 | + // @ts-ignore need access to protected _tags attribute |
| 53 | + expect(mockScope._tags).toEqual({ runtime: 'browser' }); |
| 54 | + }); |
| 55 | + |
| 56 | + describe('integrations', () => { |
| 57 | + it('adds BrowserTracing integration by default', () => { |
| 58 | + init({}); |
| 59 | + |
| 60 | + const reactInitOptions: NextjsOptions = mockInit.mock.calls[0][0]; |
| 61 | + expect(reactInitOptions.integrations).toHaveLength(1); |
| 62 | + |
| 63 | + const integrations = reactInitOptions.integrations as Integration[]; |
| 64 | + expect(integrations[0]).toEqual(expect.any(BrowserTracing)); |
| 65 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 66 | + expect((integrations[0] as InstanceType<typeof BrowserTracing>).options.routingInstrumentation).toEqual( |
| 67 | + nextRouterInstrumentation, |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + it('supports passing integration through options', () => { |
| 72 | + init({ integrations: [new Integrations.Breadcrumbs({ console: false })] }); |
| 73 | + const reactInitOptions: NextjsOptions = mockInit.mock.calls[0][0]; |
| 74 | + expect(reactInitOptions.integrations).toHaveLength(2); |
| 75 | + |
| 76 | + const integrations = reactInitOptions.integrations as Integration[]; |
| 77 | + expect(integrations).toEqual([expect.any(Integrations.Breadcrumbs), expect.any(BrowserTracing)]); |
| 78 | + }); |
| 79 | + |
| 80 | + it('uses custom BrowserTracing but uses nextRouterInstrumentation', () => { |
| 81 | + init({ |
| 82 | + integrations: [new BrowserTracing({ idleTimeout: 5000, startTransactionOnLocationChange: false })], |
| 83 | + }); |
| 84 | + |
| 85 | + const reactInitOptions: NextjsOptions = mockInit.mock.calls[0][0]; |
| 86 | + expect(reactInitOptions.integrations).toHaveLength(1); |
| 87 | + const integrations = reactInitOptions.integrations as Integration[]; |
| 88 | + expect((integrations[0] as InstanceType<typeof BrowserTracing>).options).toEqual( |
| 89 | + expect.objectContaining({ |
| 90 | + idleTimeout: 5000, |
| 91 | + startTransactionOnLocationChange: false, |
| 92 | + routingInstrumentation: nextRouterInstrumentation, |
| 93 | + }), |
| 94 | + ); |
| 95 | + }); |
| 96 | + }); |
| 97 | +}); |
0 commit comments