|
| 1 | +import type { BrowserClient } from '@sentry/browser'; |
| 2 | +import * as SentryBrowser from '@sentry/browser'; |
| 3 | +import { BrowserTracing, getCurrentHub, SDK_VERSION, WINDOW } from '@sentry/browser'; |
| 4 | +import { vi } from 'vitest'; |
| 5 | + |
| 6 | +import { init } from '../../../astro/src/client/sdk'; |
| 7 | + |
| 8 | +const browserInit = vi.spyOn(SentryBrowser, 'init'); |
| 9 | + |
| 10 | +describe('Sentry client SDK', () => { |
| 11 | + describe('init', () => { |
| 12 | + afterEach(() => { |
| 13 | + vi.clearAllMocks(); |
| 14 | + WINDOW.__SENTRY__.hub = undefined; |
| 15 | + }); |
| 16 | + |
| 17 | + it('adds Astro metadata to the SDK options', () => { |
| 18 | + expect(browserInit).not.toHaveBeenCalled(); |
| 19 | + |
| 20 | + init({}); |
| 21 | + |
| 22 | + expect(browserInit).toHaveBeenCalledTimes(1); |
| 23 | + expect(browserInit).toHaveBeenCalledWith( |
| 24 | + expect.objectContaining({ |
| 25 | + _metadata: { |
| 26 | + sdk: { |
| 27 | + name: 'sentry.javascript.astro', |
| 28 | + version: SDK_VERSION, |
| 29 | + packages: [ |
| 30 | + { name: 'npm:@sentry/astro', version: SDK_VERSION }, |
| 31 | + { name: 'npm:@sentry/browser', version: SDK_VERSION }, |
| 32 | + ], |
| 33 | + }, |
| 34 | + }, |
| 35 | + }), |
| 36 | + ); |
| 37 | + }); |
| 38 | + |
| 39 | + it('sets the runtime tag on the scope', () => { |
| 40 | + const currentScope = getCurrentHub().getScope(); |
| 41 | + |
| 42 | + // @ts-expect-error need access to protected _tags attribute |
| 43 | + expect(currentScope._tags).toEqual({}); |
| 44 | + |
| 45 | + init({ dsn: 'https://[email protected]/1337' }); |
| 46 | + |
| 47 | + // @ts-expect-error need access to protected _tags attribute |
| 48 | + expect(currentScope._tags).toEqual({ runtime: 'browser' }); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('automatically adds integrations', () => { |
| 52 | + it.each([ |
| 53 | + ['tracesSampleRate', { tracesSampleRate: 0 }], |
| 54 | + ['tracesSampler', { tracesSampler: () => 1.0 }], |
| 55 | + ['enableTracing', { enableTracing: true }], |
| 56 | + ])('adds the BrowserTracing integration if tracing is enabled via %s', (_, tracingOptions) => { |
| 57 | + init({ |
| 58 | + dsn: 'https://[email protected]/1337', |
| 59 | + ...tracingOptions, |
| 60 | + }); |
| 61 | + |
| 62 | + const integrationsToInit = browserInit.mock.calls[0][0]?.integrations; |
| 63 | + const browserTracing = (getCurrentHub().getClient() as BrowserClient)?.getIntegrationById('BrowserTracing'); |
| 64 | + |
| 65 | + expect(integrationsToInit).toContainEqual(expect.objectContaining({ name: 'BrowserTracing' })); |
| 66 | + expect(browserTracing).toBeDefined(); |
| 67 | + }); |
| 68 | + |
| 69 | + it.each([ |
| 70 | + ['enableTracing', { enableTracing: false }], |
| 71 | + ['no tracing option set', {}], |
| 72 | + ])("doesn't add the BrowserTracing integration if tracing is disabled via %s", (_, tracingOptions) => { |
| 73 | + init({ |
| 74 | + dsn: 'https://[email protected]/1337', |
| 75 | + ...tracingOptions, |
| 76 | + }); |
| 77 | + |
| 78 | + const integrationsToInit = browserInit.mock.calls[0][0]?.integrations; |
| 79 | + const browserTracing = (getCurrentHub().getClient() as BrowserClient)?.getIntegrationById('BrowserTracing'); |
| 80 | + |
| 81 | + expect(integrationsToInit).not.toContainEqual(expect.objectContaining({ name: 'BrowserTracing' })); |
| 82 | + expect(browserTracing).toBeUndefined(); |
| 83 | + }); |
| 84 | + |
| 85 | + it("doesn't add the BrowserTracing integration if `__SENTRY_TRACING__` is set to false", () => { |
| 86 | + globalThis.__SENTRY_TRACING__ = false; |
| 87 | + |
| 88 | + init({ |
| 89 | + dsn: 'https://[email protected]/1337', |
| 90 | + enableTracing: true, |
| 91 | + }); |
| 92 | + |
| 93 | + const integrationsToInit = browserInit.mock.calls[0][0]?.integrations; |
| 94 | + const browserTracing = (getCurrentHub().getClient() as BrowserClient)?.getIntegrationById('BrowserTracing'); |
| 95 | + |
| 96 | + expect(integrationsToInit).not.toContainEqual(expect.objectContaining({ name: 'BrowserTracing' })); |
| 97 | + expect(browserTracing).toBeUndefined(); |
| 98 | + |
| 99 | + delete globalThis.__SENTRY_TRACING__; |
| 100 | + }); |
| 101 | + |
| 102 | + it('Overrides the automatically default BrowserTracing instance with a a user-provided instance', () => { |
| 103 | + init({ |
| 104 | + dsn: 'https://[email protected]/1337', |
| 105 | + integrations: [new BrowserTracing({ finalTimeout: 10, startTransactionOnLocationChange: false })], |
| 106 | + enableTracing: true, |
| 107 | + }); |
| 108 | + |
| 109 | + const integrationsToInit = browserInit.mock.calls[0][0]?.integrations; |
| 110 | + |
| 111 | + const browserTracing = (getCurrentHub().getClient() as BrowserClient)?.getIntegrationById( |
| 112 | + 'BrowserTracing', |
| 113 | + ) as BrowserTracing; |
| 114 | + const options = browserTracing.options; |
| 115 | + |
| 116 | + expect(integrationsToInit).toContainEqual(expect.objectContaining({ name: 'BrowserTracing' })); |
| 117 | + expect(browserTracing).toBeDefined(); |
| 118 | + |
| 119 | + // This shows that the user-configured options are still here |
| 120 | + expect(options.finalTimeout).toEqual(10); |
| 121 | + }); |
| 122 | + }); |
| 123 | + }); |
| 124 | +}); |
0 commit comments