fix(runtime-core): allow spying on proxy methods (#4216)
Since Jest v26.6.1, the mock method changed (see this commit 30e8020362) to rely on `Object.defineProperty` in some cases.
This breaks spying on proxy's methods, because even if Jest is properly calling `Object.defineProperty`, the cached value in the `get` section of the proxy is never updated, and the spy is in fact never used.
This is easily reproducible as vue-next already uses a version of jest with these changes.
This is blocking projects (like vue-test-utils-next and vue-cli) to update to recent Jest versions.
This commit adds a `defineProperty` method to the proxy handler, that properly updates the defined value in the cache.
This commit is contained in:
@@ -397,8 +397,10 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
|
||||
const { data, setupState, ctx } = instance
|
||||
if (setupState !== EMPTY_OBJ && hasOwn(setupState, key)) {
|
||||
setupState[key] = value
|
||||
return true
|
||||
} else if (data !== EMPTY_OBJ && hasOwn(data, key)) {
|
||||
data[key] = value
|
||||
return true
|
||||
} else if (hasOwn(instance.props, key)) {
|
||||
__DEV__ &&
|
||||
warn(
|
||||
@@ -445,6 +447,19 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
|
||||
hasOwn(publicPropertiesMap, key) ||
|
||||
hasOwn(appContext.config.globalProperties, key)
|
||||
)
|
||||
},
|
||||
|
||||
defineProperty(
|
||||
target: ComponentRenderContext,
|
||||
key: string,
|
||||
descriptor: PropertyDescriptor
|
||||
) {
|
||||
if (descriptor.get != null) {
|
||||
this.set!(target, key, descriptor.get(), null)
|
||||
} else if (descriptor.value != null) {
|
||||
this.set!(target, key, descriptor.value, null)
|
||||
}
|
||||
return Reflect.defineProperty(target, key, descriptor)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user