From 69e59dbbe97c03b4c2602f0f7cb5382908020587 Mon Sep 17 00:00:00 2001 From: Yousif Ahmed Date: Fri, 12 Jul 2024 12:08:25 +0100 Subject: [PATCH] fix(plugin-vue): check global vue config exists before installing vue 2 handler --- CHANGELOG.md | 6 +++++ packages/plugin-vue/src/index.js | 2 +- packages/plugin-vue/test/index.test.ts | 33 ++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a18b71d41f..5c5673b7db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [Unreleased] + +### Fixed + +- (plugin-vue) Check global vue config exists before installing vue 2 handler [#2171](https://github.com/bugsnag/bugsnag-js/pull/2171) + ## [7.25.0] - 2024-07-03 ### Added diff --git a/packages/plugin-vue/src/index.js b/packages/plugin-vue/src/index.js index 2cbd4c844e..bb116fb1d5 100644 --- a/packages/plugin-vue/src/index.js +++ b/packages/plugin-vue/src/index.js @@ -16,7 +16,7 @@ module.exports = class BugsnagPluginVue { } load (client) { - if (this.Vue) { + if (this.Vue && this.Vue.config) { installVue2(this.Vue, client) return { installVueErrorHandler: () => client._logger.warn('installVueErrorHandler() was called unnecessarily') diff --git a/packages/plugin-vue/test/index.test.ts b/packages/plugin-vue/test/index.test.ts index 6875a4b9f6..6964142373 100644 --- a/packages/plugin-vue/test/index.test.ts +++ b/packages/plugin-vue/test/index.test.ts @@ -195,6 +195,39 @@ describe('bugsnag vue', () => { errorHandler('oops', { $options: { name: 'MyComponent' } }, 1) }) + it('works with a global Vue instance (CDN build)', done => { + // In Vue 3.+ the global Vue.config has been replaced with app.config, + // but CDN builds still expose a global Vue object + const mockVue = {} + const mockVueApp: Vue3App = { + use: (plugin) => { + plugin.install(mockVueApp) + }, + config: { errorHandler: undefined } + } + const client = new Client({ apiKey: 'API_KEYYY', plugins: [new BugsnagVuePlugin(mockVue)] }) + // eslint-disable-next-line + const plugin = client.getPlugin('vue')! + expect(typeof plugin.install).toBe('function') + + mockVueApp.use(plugin) + client._setDelivery(client => ({ + sendEvent: (payload) => { + expect(payload.events[0].errors[0].errorClass).toBe('Error') + expect(payload.events[0].errors[0].errorMessage).toBe('oops') + expect(payload.events[0].errors[0].stacktrace[0].file).toBe(__filename) + expect(payload.events[0]._metadata.vue).toBeDefined() + expect(payload.events[0]._metadata.vue.component).toBe('MyComponent') + expect(payload.events[0]._metadata.vue.errorInfo).toBe('render function') + done() + }, + sendSession: () => {} + })) + expect(typeof mockVueApp.config.errorHandler).toBe('function') + const errorHandler = mockVueApp.config.errorHandler as unknown as Vue3ErrorHandler + errorHandler(new Error('oops'), { $options: { name: 'MyComponent' } }, 1) + }) + // // VUE 2 //