From b420f2b72394bae5bd6f9900f175ad8cdc5feec4 Mon Sep 17 00:00:00 2001 From: sh7dm Date: Fri, 1 Mar 2019 14:59:36 +0300 Subject: [PATCH 1/4] feat(global-api): warn when plugin is invalid --- src/core/global-api/use.js | 8 ++++++-- test/unit/features/global-api/use.spec.js | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/core/global-api/use.js b/src/core/global-api/use.js index a5b69988ddc..8a44c23b7dd 100644 --- a/src/core/global-api/use.js +++ b/src/core/global-api/use.js @@ -1,6 +1,6 @@ /* @flow */ -import { toArray } from '../util/index' +import { toArray, warn } from '../util/index' export function initUse (Vue: GlobalAPI) { Vue.use = function (plugin: Function | Object) { @@ -12,10 +12,14 @@ export function initUse (Vue: GlobalAPI) { // additional parameters const args = toArray(arguments, 1) args.unshift(this) - if (typeof plugin.install === 'function') { + if (plugin && typeof plugin.install === 'function') { plugin.install.apply(plugin, args) } else if (typeof plugin === 'function') { plugin.apply(null, args) + } else if (process.env.NODE_ENV !== 'production') { + warn( + 'Plugin should be either function or object with install method.' + ) } installedPlugins.push(plugin) return this diff --git a/test/unit/features/global-api/use.spec.js b/test/unit/features/global-api/use.spec.js index d54ff0750f3..c9d699abe5a 100644 --- a/test/unit/features/global-api/use.spec.js +++ b/test/unit/features/global-api/use.spec.js @@ -54,4 +54,18 @@ describe('Global API: use', () => { it('chain call', () => { expect(Vue.use(() => {})).toBe(Vue) }) + + it('should warn if using invalid plugin', () => { + const Ctor1 = Vue.extend({}) + Ctor1.use('hello') + expect('Plugin should be either function object with install method.').toHaveBeenWarned() + + const Ctor2 = Vue.extend({}) + Ctor2.use(undefined) + expect('Plugin should be either function object with install method.').toHaveBeenWarned() + + const Ctor3 = Vue.extend({}) + Ctor3.use(null) + expect('Plugin should be either function object with install method.').toHaveBeenWarned() + }) }) From aa1d9ac8e353e3962c44ae129e678b5b265368f9 Mon Sep 17 00:00:00 2001 From: sh7dm Date: Fri, 1 Mar 2019 17:05:03 +0300 Subject: [PATCH 2/4] test(global-api): remove failing test --- test/unit/features/global-api/use.spec.js | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/test/unit/features/global-api/use.spec.js b/test/unit/features/global-api/use.spec.js index c9d699abe5a..d54ff0750f3 100644 --- a/test/unit/features/global-api/use.spec.js +++ b/test/unit/features/global-api/use.spec.js @@ -54,18 +54,4 @@ describe('Global API: use', () => { it('chain call', () => { expect(Vue.use(() => {})).toBe(Vue) }) - - it('should warn if using invalid plugin', () => { - const Ctor1 = Vue.extend({}) - Ctor1.use('hello') - expect('Plugin should be either function object with install method.').toHaveBeenWarned() - - const Ctor2 = Vue.extend({}) - Ctor2.use(undefined) - expect('Plugin should be either function object with install method.').toHaveBeenWarned() - - const Ctor3 = Vue.extend({}) - Ctor3.use(null) - expect('Plugin should be either function object with install method.').toHaveBeenWarned() - }) }) From 900a7bb442d31b68f3725dc75d42cc42fbb6941c Mon Sep 17 00:00:00 2001 From: Dmitry Sharshakov Date: Tue, 5 Mar 2019 19:43:00 +0300 Subject: [PATCH 3/4] Update use.spec.js --- test/unit/features/global-api/use.spec.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/unit/features/global-api/use.spec.js b/test/unit/features/global-api/use.spec.js index d54ff0750f3..3d66c14ebc8 100644 --- a/test/unit/features/global-api/use.spec.js +++ b/test/unit/features/global-api/use.spec.js @@ -54,4 +54,18 @@ describe('Global API: use', () => { it('chain call', () => { expect(Vue.use(() => {})).toBe(Vue) }) + + it('should warn if using invalid plugin', () => { + const Ctor1 = Vue.extend({}) + Ctor1.use('hello') + expect('Plugin should be either function object with install method.').toHaveBeenWarned() + + const Ctor2 = Vue.extend({}) + Ctor2.use(undefined) + expect('Plugin should be either function object with install method.').toHaveBeenWarned() + + const Ctor3 = Vue.extend({}) + Ctor3.use(null) + expect('Plugin should be either function object with install method.').toHaveBeenWarned() + }) }) From c826d08f2f303c1f1edf1a05b557823971a9a7c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorsten=20L=C3=BCnborg?= Date: Tue, 5 Mar 2019 18:10:58 +0100 Subject: [PATCH 4/4] test: fix string, adding missing 'or ' --- test/unit/features/global-api/use.spec.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/unit/features/global-api/use.spec.js b/test/unit/features/global-api/use.spec.js index 3d66c14ebc8..b0c888a7b11 100644 --- a/test/unit/features/global-api/use.spec.js +++ b/test/unit/features/global-api/use.spec.js @@ -58,14 +58,14 @@ describe('Global API: use', () => { it('should warn if using invalid plugin', () => { const Ctor1 = Vue.extend({}) Ctor1.use('hello') - expect('Plugin should be either function object with install method.').toHaveBeenWarned() + expect('Plugin should be either function or object with install method.').toHaveBeenWarned() const Ctor2 = Vue.extend({}) Ctor2.use(undefined) - expect('Plugin should be either function object with install method.').toHaveBeenWarned() + expect('Plugin should be either function or object with install method.').toHaveBeenWarned() const Ctor3 = Vue.extend({}) Ctor3.use(null) - expect('Plugin should be either function object with install method.').toHaveBeenWarned() + expect('Plugin should be either function or object with install method.').toHaveBeenWarned() }) })