|
| 1 | +/* eslint-disable @typescript-eslint/no-var-requires */ |
| 2 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 3 | + |
| 4 | +const { onClientEntry } = require('../gatsby-browser'); |
| 5 | + |
| 6 | +(global as any).__SENTRY_RELEASE__ = '683f3a6ab819d47d23abfca9a914c81f0524d35b'; |
| 7 | +(global as any).__SENTRY_DSN__ = 'https://examplePublicKey@o0.ingest.sentry.io/0'; |
| 8 | + |
| 9 | +let sentryInit = jest.fn(); |
| 10 | +let sentryProcessEvent: <T>(event: T) => T; |
| 11 | +jest.mock('@sentry/react', () => { |
| 12 | + const original = jest.requireActual('@sentry/react'); |
| 13 | + return { |
| 14 | + ...original, |
| 15 | + init: (...args: any[]) => { |
| 16 | + sentryInit(...args); |
| 17 | + }, |
| 18 | + addGlobalEventProcessor: (callback: any) => { |
| 19 | + sentryProcessEvent = callback; |
| 20 | + }, |
| 21 | + }; |
| 22 | +}); |
| 23 | + |
| 24 | +let tracingAddExtensionMethods = jest.fn(); |
| 25 | +jest.mock('@sentry/tracing', () => { |
| 26 | + const original = jest.requireActual('@sentry/tracing'); |
| 27 | + return { |
| 28 | + ...original, |
| 29 | + addExtensionMethods: (...args: any[]) => { |
| 30 | + tracingAddExtensionMethods(...args); |
| 31 | + }, |
| 32 | + }; |
| 33 | +}); |
| 34 | + |
| 35 | +describe('onClientEntry', () => { |
| 36 | + beforeEach(() => { |
| 37 | + sentryInit = jest.fn(); |
| 38 | + tracingAddExtensionMethods = jest.fn(); |
| 39 | + |
| 40 | + // @ts-ignore need to set as undefined |
| 41 | + sentryProcessEvent = undefined; |
| 42 | + }); |
| 43 | + |
| 44 | + afterEach(() => { |
| 45 | + (window as any).Sentry = undefined; |
| 46 | + }); |
| 47 | + |
| 48 | + it('inits Sentry by default', () => { |
| 49 | + onClientEntry(undefined, {}); |
| 50 | + expect(sentryInit).toHaveBeenCalledTimes(1); |
| 51 | + expect(sentryInit).toHaveBeenLastCalledWith({ |
| 52 | + dsn: (global as any).__SENTRY_DSN__, |
| 53 | + environment: process.env.NODE_ENV, |
| 54 | + integrations: [], |
| 55 | + release: (global as any).__SENTRY_RELEASE__, |
| 56 | + tracesSampleRate: 0, |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + it('sets window.Sentry', () => { |
| 61 | + onClientEntry(undefined, {}); |
| 62 | + expect((window as any).Sentry).not.toBeUndefined(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('adds a global event processor', () => { |
| 66 | + onClientEntry(undefined, {}); |
| 67 | + if (sentryProcessEvent) { |
| 68 | + const changedEvent = sentryProcessEvent({}); |
| 69 | + |
| 70 | + expect(changedEvent).toEqual({ |
| 71 | + sdk: { |
| 72 | + name: 'sentry.javascript.gatsby', |
| 73 | + packages: [ |
| 74 | + { |
| 75 | + name: 'npm:@sentry/gatsby', |
| 76 | + version: expect.any(String), |
| 77 | + }, |
| 78 | + ], |
| 79 | + version: expect.any(String), |
| 80 | + }, |
| 81 | + }); |
| 82 | + } else { |
| 83 | + fail('process event not defined'); |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + it('adds Tracing extension methods', () => { |
| 88 | + onClientEntry(undefined, {}); |
| 89 | + |
| 90 | + expect(tracingAddExtensionMethods).toHaveBeenCalledTimes(1); |
| 91 | + expect(tracingAddExtensionMethods).toHaveBeenLastCalledWith(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('sets a tracesSampleRate if defined as option', () => { |
| 95 | + onClientEntry(undefined, { tracesSampleRate: 0.5 }); |
| 96 | + expect(sentryInit).toHaveBeenLastCalledWith( |
| 97 | + expect.objectContaining({ |
| 98 | + tracesSampleRate: 0.5, |
| 99 | + }), |
| 100 | + ); |
| 101 | + }); |
| 102 | + |
| 103 | + it('adds `BrowserTracing` integration if tracesSampleRate is defined', () => { |
| 104 | + onClientEntry(undefined, { tracesSampleRate: 0.5 }); |
| 105 | + expect(sentryInit).toHaveBeenLastCalledWith( |
| 106 | + expect.objectContaining({ |
| 107 | + integrations: [expect.objectContaining({ name: 'BrowserTracing' })], |
| 108 | + }), |
| 109 | + ); |
| 110 | + }); |
| 111 | + |
| 112 | + it('only defines a single `BrowserTracing` integration', () => { |
| 113 | + const Tracing = jest.requireActual('@sentry/tracing'); |
| 114 | + const integrations = [new Tracing.Integrations.BrowserTracing()]; |
| 115 | + onClientEntry(undefined, { tracesSampleRate: 0.5, integrations }); |
| 116 | + |
| 117 | + expect(sentryInit).toHaveBeenLastCalledWith( |
| 118 | + expect.objectContaining({ |
| 119 | + integrations: [expect.objectContaining({ name: 'BrowserTracing' })], |
| 120 | + }), |
| 121 | + ); |
| 122 | + }); |
| 123 | + |
| 124 | + // Run this last to check for any test side effects |
| 125 | + it('does not run if plugin params are undefined', () => { |
| 126 | + onClientEntry(); |
| 127 | + expect(sentryInit).toHaveBeenCalledTimes(0); |
| 128 | + expect((window as any).Sentry).toBeUndefined(); |
| 129 | + expect(sentryProcessEvent).toBeUndefined(); |
| 130 | + expect(tracingAddExtensionMethods).toHaveBeenCalledTimes(0); |
| 131 | + }); |
| 132 | +}); |
0 commit comments