diff --git a/docs/API.md b/docs/API.md index 7368312cc..38fcf18a9 100644 --- a/docs/API.md +++ b/docs/API.md @@ -187,23 +187,37 @@ export default {} ``` ```js -test('installs a plugin via `plugins`', () => { +test('installs plugins via `plugins`', () => { const installed = jest.fn() class Plugin { static install() { installed() } } + + const installedWithOptions = jest.fn() + class PluginWithOptions { + static install(_app: App, ...args) { + installedWithOptions(...args) + } + } + const Component = { - render() { return h('div') } + render() { + return h('div') + } } mount(Component, { global: { - plugins: [Plugin] + plugins: [Plugin, [PluginWithOptions, 'argument 1', 'another argument']] } }) expect(installed).toHaveBeenCalled() + expect(installedWithOptions).toHaveBeenCalledWith( + 'argument 1', + 'another argument' + ) }) ``` diff --git a/src/mount.ts b/src/mount.ts index 8a46f4343..c2cf253a7 100644 --- a/src/mount.ts +++ b/src/mount.ts @@ -312,7 +312,13 @@ export function mount( // use and plugins from mounting options if (global.plugins) { - for (const use of global.plugins) app.use(use) + for (const plugin of global.plugins) { + if (Array.isArray(plugin)) { + app.use(plugin[0], ...plugin.slice(1)) + continue + } + app.use(plugin) + } } // use any mixins from mounting options diff --git a/src/types.ts b/src/types.ts index 7d9d3fafc..bdca514ee 100644 --- a/src/types.ts +++ b/src/types.ts @@ -20,7 +20,7 @@ export type FindComponentSelector = RefSelector | NameSelector | string export type FindAllComponentsSelector = NameSelector | string export type GlobalMountOptions = { - plugins?: Plugin[] + plugins?: (Plugin | [Plugin, ...any[]])[] config?: Omit // isNativeTag is readonly, so we omit it mixins?: ComponentOptions[] mocks?: Record diff --git a/tests/mountingOptions/global.plugins.spec.ts b/tests/mountingOptions/global.plugins.spec.ts index 7c33c485b..ed47721c6 100644 --- a/tests/mountingOptions/global.plugins.spec.ts +++ b/tests/mountingOptions/global.plugins.spec.ts @@ -1,4 +1,4 @@ -import { h } from 'vue' +import { h, App } from 'vue' import { mount } from '../../src' @@ -25,4 +25,62 @@ describe('mounting options: plugins', () => { expect(installed).toHaveBeenCalled() }) + + it('installs a plugin with options `plugins`', () => { + const installed = jest.fn() + + class Plugin { + static install(_app: App, ...options) { + installed(...options) + } + } + + const Component = { + render() { + return h('div') + } + } + const options = { option1: true } + const testString = 'hello' + mount(Component, { + global: { + plugins: [[Plugin, options, testString]] + } + }) + + expect(installed).toHaveBeenCalledWith(options, testString) + }) +}) + +test('installs plugins with and without options', () => { + const installed = jest.fn() + class Plugin { + static install() { + installed() + } + } + + const installedWithOptions = jest.fn() + class PluginWithOptions { + static install(_app: App, ...args) { + installedWithOptions(...args) + } + } + + const Component = { + render() { + return h('div') + } + } + mount(Component, { + global: { + plugins: [Plugin, [PluginWithOptions, 'argument 1', 'another argument']] + } + }) + + expect(installed).toHaveBeenCalled() + expect(installedWithOptions).toHaveBeenCalledWith( + 'argument 1', + 'another argument' + ) })