diff --git a/docs/api/index.md b/docs/api/index.md
index 1e63d406b..ee6d2e1f2 100644
--- a/docs/api/index.md
+++ b/docs/api/index.md
@@ -505,6 +505,15 @@ export default {
}
}
+
+// Or if you are using script setup
+
+
+
```
`Component.spec.js`:
diff --git a/src/createInstance.ts b/src/createInstance.ts
index bb79fe94c..6a35309a7 100644
--- a/src/createInstance.ts
+++ b/src/createInstance.ts
@@ -228,6 +228,8 @@ export function createInstance(
this.$.setupState[k] = v
// eslint-disable-next-line no-empty
} catch (e) {}
+ //@ts-ignore Setting an unknown global variable for the component instance
+ globalThis[k] = v
}
// also intercept the proxy calls to make the mocks available on the instance
// (useful when a template access a global function like $t and the developer wants to mock it)
diff --git a/tests/components/ScriptSetupWithGlobalFunction.vue b/tests/components/ScriptSetupWithGlobalFunction.vue
new file mode 100644
index 000000000..30fd37f7d
--- /dev/null
+++ b/tests/components/ScriptSetupWithGlobalFunction.vue
@@ -0,0 +1,16 @@
+
+
+
+
+
diff --git a/tests/mountingOptions/mocks.spec.ts b/tests/mountingOptions/mocks.spec.ts
index c07bb5937..5b2b99e4c 100644
--- a/tests/mountingOptions/mocks.spec.ts
+++ b/tests/mountingOptions/mocks.spec.ts
@@ -2,6 +2,7 @@ import { describe, expect, it, vi } from 'vitest'
import { mount, RouterLinkStub } from '../../src'
import { defineComponent } from 'vue'
import ScriptSetupWithI18n from '../components/ScriptSetupWithI18n.vue'
+import ScriptSetupWithGlobalFunction from '../components/ScriptSetupWithGlobalFunction.vue'
import ComponentWithI18n from '../components/ComponentWithI18n.vue'
describe('mocks', () => {
@@ -90,6 +91,19 @@ describe('mocks', () => {
expect(wrapper.text()).toContain('mocked')
})
+ it('mocks a global function used in a script setup', async () => {
+ const wrapper = mount(ScriptSetupWithGlobalFunction, {
+ global: {
+ mocks: {
+ $fetch: async (url: string) => ({ data: 'mocked' })
+ }
+ }
+ })
+ expect(wrapper.text()).toContain('hello')
+ await wrapper.find('button').trigger('click')
+ expect(wrapper.text()).toContain('mocked')
+ })
+
it('mocks a global function in an option component', () => {
const wrapper = mount(ComponentWithI18n, {
global: {