diff --git a/src/vueWrapper.ts b/src/vueWrapper.ts index 0209dbb4d..b69bc85da 100644 --- a/src/vueWrapper.ts +++ b/src/vueWrapper.ts @@ -32,6 +32,13 @@ function createVMProxy( } else if (key in setupState) { // second if the key is acccessible from the setupState return Reflect.get(setupState, key, receiver) + } else if (key in vm.$.appContext.config.globalProperties) { + // third if the key is a global property + return Reflect.get( + vm.$.appContext.config.globalProperties, + key, + receiver + ) } else { // vm.$.ctx is the internal context of the vm // with all variables, methods and props diff --git a/tests/components/OptionComponent.vue b/tests/components/OptionComponent.vue new file mode 100644 index 000000000..6cfec2cae --- /dev/null +++ b/tests/components/OptionComponent.vue @@ -0,0 +1,14 @@ + + + diff --git a/tests/components/OptionSetupComponent.vue b/tests/components/OptionSetupComponent.vue new file mode 100644 index 000000000..fc4160b5b --- /dev/null +++ b/tests/components/OptionSetupComponent.vue @@ -0,0 +1,18 @@ + + + diff --git a/tests/components/OptionSetupWithoutReturnComponent.vue b/tests/components/OptionSetupWithoutReturnComponent.vue new file mode 100644 index 000000000..f9a36218c --- /dev/null +++ b/tests/components/OptionSetupWithoutReturnComponent.vue @@ -0,0 +1,18 @@ + + + diff --git a/tests/mountingOptions/global.plugins.spec.ts b/tests/mountingOptions/global.plugins.spec.ts index 974c7597e..d077ce52c 100644 --- a/tests/mountingOptions/global.plugins.spec.ts +++ b/tests/mountingOptions/global.plugins.spec.ts @@ -2,6 +2,10 @@ import { describe, expect, test, it, vi } from 'vitest' import { h, App } from 'vue' import { mount } from '../../src' +import ScriptSetup from '../components/ScriptSetup.vue' +import Option from '../components/OptionComponent.vue' +import OptionsSetup from '../components/OptionSetupComponent.vue' +import OptionsSetupWithoutReturn from '../components/OptionSetupWithoutReturnComponent.vue' describe('mounting options: plugins', () => { it('installs a plugin via `plugins`', () => { @@ -51,6 +55,41 @@ describe('mounting options: plugins', () => { expect(installed).toHaveBeenCalledWith(options, testString) }) + + describe('provides access to a global property', () => { + class Plugin { + static install(app: App) { + app.config.globalProperties.foo = 'bar' + } + } + it('provides access to a global property from a Composition API component', () => { + const wrapper = mount(ScriptSetup, { + global: { plugins: [Plugin] } + }) + expect((wrapper.vm as any).foo).toBeDefined() + }) + + it('provides access to a global property from an Options API component', () => { + const wrapper = mount(Option, { + global: { plugins: [Plugin] } + }) + expect((wrapper.vm as any).foo).toBeDefined() + }) + + it('provides access to a global property from an Options API component with a setup() function', () => { + const wrapper = mount(OptionsSetup, { + global: { plugins: [Plugin] } + }) + expect((wrapper.vm as any).foo).toBeDefined() + }) + + it('provides access to a global property from an Options API component with a setup() function that does not return', () => { + const wrapper = mount(OptionsSetupWithoutReturn, { + global: { plugins: [Plugin] } + }) + expect((wrapper.vm as any).foo).toBeDefined() + }) + }) }) test('installs plugins with and without options', () => {