|
| 1 | +import { logger } from '@sentry/utils'; |
| 2 | +import { createApp } from 'vue'; |
| 3 | + |
| 4 | +import * as Sentry from '../../src'; |
| 5 | +import { createTransport, Hub, makeMain } from '../../src'; |
| 6 | + |
| 7 | +const PUBLIC_DSN = 'https://username@domain/123'; |
| 8 | + |
| 9 | +describe('Sentry.initVueApp', () => { |
| 10 | + let loggerWarnings: unknown[] = []; |
| 11 | + let warnings: unknown[] = []; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + warnings = []; |
| 15 | + loggerWarnings = []; |
| 16 | + |
| 17 | + jest.spyOn(logger, 'warn').mockImplementation((message: unknown) => { |
| 18 | + loggerWarnings.push(message); |
| 19 | + }); |
| 20 | + |
| 21 | + jest.spyOn(console, 'warn').mockImplementation((message: unknown) => { |
| 22 | + warnings.push(message); |
| 23 | + }); |
| 24 | + }); |
| 25 | + |
| 26 | + afterEach(() => { |
| 27 | + jest.resetAllMocks(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('warns when called before SDK.init()', () => { |
| 31 | + const hub = new Hub(); |
| 32 | + makeMain(hub); |
| 33 | + |
| 34 | + const app = createApp({ |
| 35 | + template: '<div>hello</div>', |
| 36 | + }); |
| 37 | + |
| 38 | + Sentry.initVueApp(app); |
| 39 | + |
| 40 | + expect(loggerWarnings).toEqual([ |
| 41 | + '[@sentry/vue]: Cannot initialize as no Client available. Make sure to call `Sentry.init` before calling `initVueApp()`.', |
| 42 | + ]); |
| 43 | + expect(warnings).toEqual([]); |
| 44 | + }); |
| 45 | + |
| 46 | + it('warns when mounting before SDK.initVueApp()', () => { |
| 47 | + Sentry.init({ dsn: PUBLIC_DSN, app: false, autoSessionTracking: false }); |
| 48 | + |
| 49 | + const el = document.createElement('div'); |
| 50 | + const app = createApp({ |
| 51 | + template: '<div>hello</div>', |
| 52 | + }); |
| 53 | + |
| 54 | + app.mount(el); |
| 55 | + |
| 56 | + Sentry.initVueApp(app); |
| 57 | + |
| 58 | + expect(warnings).toEqual([ |
| 59 | + '[@sentry/vue]: Misconfigured SDK. Vue app is already mounted. Make sure to call `app.mount()` after `Sentry.init()`.', |
| 60 | + ]); |
| 61 | + expect(loggerWarnings).toEqual([]); |
| 62 | + }); |
| 63 | + |
| 64 | + it('works when calling SDK.initVueApp()', () => { |
| 65 | + Sentry.init({ dsn: PUBLIC_DSN, app: false, autoSessionTracking: false }); |
| 66 | + |
| 67 | + const el = document.createElement('div'); |
| 68 | + const app = createApp({ |
| 69 | + template: '<div>hello</div>', |
| 70 | + }); |
| 71 | + |
| 72 | + Sentry.initVueApp(app); |
| 73 | + |
| 74 | + app.mount(el); |
| 75 | + |
| 76 | + expect(warnings).toEqual([]); |
| 77 | + expect(loggerWarnings).toEqual([]); |
| 78 | + |
| 79 | + expect(app.config.errorHandler).toBeDefined(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('allows to pass a client to SDK.initVueApp()', () => { |
| 83 | + const el = document.createElement('div'); |
| 84 | + const app = createApp({ |
| 85 | + template: '<div>hello</div>', |
| 86 | + }); |
| 87 | + |
| 88 | + const client = new Sentry.BrowserClient({ |
| 89 | + dsn: PUBLIC_DSN, |
| 90 | + integrations: [], |
| 91 | + stackParser: () => [], |
| 92 | + transport: () => createTransport({ recordDroppedEvent: () => undefined }, _ => Promise.resolve({})), |
| 93 | + }); |
| 94 | + |
| 95 | + Sentry.initVueApp(app, client); |
| 96 | + |
| 97 | + app.mount(el); |
| 98 | + |
| 99 | + expect(warnings).toEqual([]); |
| 100 | + expect(loggerWarnings).toEqual([]); |
| 101 | + |
| 102 | + expect(app.config.errorHandler).toBeDefined(); |
| 103 | + }); |
| 104 | +}); |
0 commit comments