|
1 | 1 | import { getCurrentHub } from '@sentry/core';
|
| 2 | +import type { BrowserClient } from '@sentry/svelte'; |
2 | 3 | import * as SentrySvelte from '@sentry/svelte';
|
3 | 4 | import { SDK_VERSION, WINDOW } from '@sentry/svelte';
|
4 | 5 | import { vi } from 'vitest';
|
5 | 6 |
|
6 |
| -import { init } from '../../src/client/sdk'; |
| 7 | +import { BrowserTracing, init } from '../../src/client'; |
7 | 8 |
|
8 | 9 | const svelteInit = vi.spyOn(SentrySvelte, 'init');
|
9 | 10 |
|
@@ -47,5 +48,79 @@ describe('Sentry client SDK', () => {
|
47 | 48 | // @ts-ignore need access to protected _tags attribute
|
48 | 49 | expect(currentScope._tags).toEqual({ runtime: 'browser' });
|
49 | 50 | });
|
| 51 | + |
| 52 | + describe('automatically added integrations', () => { |
| 53 | + it.each([ |
| 54 | + ['tracesSampleRate', { tracesSampleRate: 0 }], |
| 55 | + ['tracesSampler', { tracesSampler: () => 1.0 }], |
| 56 | + ['enableTracing', { enableTracing: true }], |
| 57 | + ])('adds the BrowserTracing integration if tracing is enabled via %s', (_, tracingOptions) => { |
| 58 | + init({ |
| 59 | + dsn: 'https://[email protected]/1337', |
| 60 | + ...tracingOptions, |
| 61 | + }); |
| 62 | + |
| 63 | + const integrationsToInit = svelteInit.mock.calls[0][0].integrations; |
| 64 | + const browserTracing = (getCurrentHub().getClient() as BrowserClient)?.getIntegrationById('BrowserTracing'); |
| 65 | + |
| 66 | + expect(integrationsToInit).toContainEqual(expect.objectContaining({ name: 'BrowserTracing' })); |
| 67 | + expect(browserTracing).toBeDefined(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('merges the BrowserTracing integration with the user-provided one', () => {}); |
| 71 | + }); |
| 72 | + |
| 73 | + it.each([ |
| 74 | + ['enableTracing', { enableTracing: false }], |
| 75 | + ['no tracing option set', {}], |
| 76 | + ])("doesn't add the BrowserTracing integration if tracing is disabled via %s", (_, tracingOptions) => { |
| 77 | + init({ |
| 78 | + dsn: 'https://[email protected]/1337', |
| 79 | + ...tracingOptions, |
| 80 | + }); |
| 81 | + |
| 82 | + const integrationsToInit = svelteInit.mock.calls[0][0].integrations; |
| 83 | + const browserTracing = (getCurrentHub().getClient() as BrowserClient)?.getIntegrationById('BrowserTracing'); |
| 84 | + |
| 85 | + expect(integrationsToInit).not.toContainEqual(expect.objectContaining({ name: 'BrowserTracing' })); |
| 86 | + expect(browserTracing).toBeUndefined(); |
| 87 | + }); |
| 88 | + |
| 89 | + it("doesn't add the BrowserTracing integration if `__SENTRY_TRACING__` is set to false", () => { |
| 90 | + // This is the closest we can get to unit-testing the `__SENTRY_TRACING__` tree-shaking guard |
| 91 | + // IRL, the code to add the integration would most likely be removed by the bundler. |
| 92 | + |
| 93 | + globalThis.__SENTRY_TRACING__ = false; |
| 94 | + |
| 95 | + init({ |
| 96 | + dsn: 'https://[email protected]/1337', |
| 97 | + enableTracing: true, |
| 98 | + }); |
| 99 | + |
| 100 | + const integrationsToInit = svelteInit.mock.calls[0][0].integrations; |
| 101 | + const browserTracing = (getCurrentHub().getClient() as BrowserClient)?.getIntegrationById('BrowserTracing'); |
| 102 | + |
| 103 | + expect(integrationsToInit).not.toContainEqual(expect.objectContaining({ name: 'BrowserTracing' })); |
| 104 | + expect(browserTracing).toBeUndefined(); |
| 105 | + |
| 106 | + delete globalThis.__SENTRY_TRACING__; |
| 107 | + }); |
| 108 | + |
| 109 | + // TODO: this test is only meaningful once we have a routing instrumentation which we always want to add |
| 110 | + // to a user-provided BrowserTracing integration (see NextJS SDK) |
| 111 | + it.skip('Merges the user-provided BrowserTracing integration with the automatically added one', () => { |
| 112 | + init({ |
| 113 | + dsn: 'https://[email protected]/1337', |
| 114 | + integrations: [new BrowserTracing({ tracePropagationTargets: ['myDomain.com'] })], |
| 115 | + enableTracing: true, |
| 116 | + }); |
| 117 | + |
| 118 | + const integrationsToInit = svelteInit.mock.calls[0][0].integrations; |
| 119 | + const browserTracing = (getCurrentHub().getClient() as BrowserClient)?.getIntegrationById('BrowserTracing'); |
| 120 | + |
| 121 | + expect(integrationsToInit).toContainEqual(expect.objectContaining({ name: 'BrowserTracing' })); |
| 122 | + expect(browserTracing).toBeDefined(); |
| 123 | + expect((browserTracing as BrowserTracing).options.tracePropagationTargets).toEqual(['myDomain.com']); |
| 124 | + }); |
50 | 125 | });
|
51 | 126 | });
|
0 commit comments