Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/core/global-api/use.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions test/unit/features/global-api/use.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 or object with install method.').toHaveBeenWarned()

const Ctor2 = Vue.extend({})
Ctor2.use(undefined)
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 or object with install method.').toHaveBeenWarned()
})
})