diff --git a/src/mount.ts b/src/mount.ts
index 8a607aa47..27735767c 100644
--- a/src/mount.ts
+++ b/src/mount.ts
@@ -478,16 +478,35 @@ export function mount(
if (global?.mocks) {
const mixin = defineComponent({
beforeCreate() {
- for (const [k, v] of Object.entries(
- global.mocks as { [key: string]: any }
- )) {
- // we need to differentiate components that are or not not `script setup`
- // otherwise we run into a proxy set error
- // due to https://github.com/vuejs/core/commit/f73925d76a76ee259749b8b48cb68895f539a00f#diff-ea4d1ddabb7e22e17e80ada458eef70679af4005df2a1a6b73418fec897603ceR404
- // introduced in Vue v3.2.45
- if (hasSetupState(this)) {
- this.$.setupState[k] = v
- } else {
+ // we need to differentiate components that are or not not `script setup`
+ // otherwise we run into a proxy set error
+ // due to https://github.com/vuejs/core/commit/f73925d76a76ee259749b8b48cb68895f539a00f#diff-ea4d1ddabb7e22e17e80ada458eef70679af4005df2a1a6b73418fec897603ceR404
+ // introduced in Vue v3.2.45
+ if (hasSetupState(this)) {
+ // add the mocks to setupState
+ for (const [k, v] of Object.entries(
+ global.mocks as { [key: string]: any }
+ )) {
+ // we do this in a try/catch, as some properties might be read-only
+ try {
+ this.$.setupState[k] = v
+ // eslint-disable-next-line no-empty
+ } catch (e) {}
+ }
+ // 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)
+ ;(this.$ as any).proxy = new Proxy((this.$ as any).proxy, {
+ get(target, key) {
+ if (key in global.mocks) {
+ return global.mocks[key as string]
+ }
+ return target[key]
+ }
+ })
+ } else {
+ for (const [k, v] of Object.entries(
+ global.mocks as { [key: string]: any }
+ )) {
;(this as any)[k] = v
}
}
@@ -604,8 +623,10 @@ export function mount(
const appRef = componentRef.value! as ComponentPublicInstance
// we add `hasOwnProperty` so Jest can spy on the proxied vm without throwing
// note that this is not necessary with Jest v27+ or Vitest, but is kept for compatibility with older Jest versions
- appRef.hasOwnProperty = (property) => {
- return Reflect.has(appRef, property)
+ if (!app.hasOwnProperty) {
+ appRef.hasOwnProperty = (property) => {
+ return Reflect.has(appRef, property)
+ }
}
const wrapper = createVueWrapper(app, appRef, setProps)
trackInstance(wrapper)
diff --git a/src/utils.ts b/src/utils.ts
index a6a99185b..be0cad37b 100644
--- a/src/utils.ts
+++ b/src/utils.ts
@@ -80,7 +80,6 @@ export const mergeDeep = (
if (!isObject(target) || !isObject(source)) {
return source
}
-
Object.keys(source).forEach((key) => {
const targetValue = target[key]
const sourceValue = source[key]
diff --git a/tests/components/ComponentWithI18n.vue b/tests/components/ComponentWithI18n.vue
new file mode 100644
index 000000000..d955296a3
--- /dev/null
+++ b/tests/components/ComponentWithI18n.vue
@@ -0,0 +1,16 @@
+
+
+
+