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:
@@ -214,6 +214,74 @@ describe('component: proxy', () => {
|
||||
])
|
||||
})
|
||||
|
||||
test('allow updating proxy with Object.defineProperty', () => {
|
||||
let instanceProxy: any
|
||||
const Comp = {
|
||||
render() {},
|
||||
setup() {
|
||||
return {
|
||||
isDisplayed: true
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
instanceProxy = this
|
||||
}
|
||||
}
|
||||
|
||||
const app = createApp(Comp)
|
||||
|
||||
app.mount(nodeOps.createElement('div'))
|
||||
|
||||
Object.defineProperty(instanceProxy, 'isDisplayed', { value: false })
|
||||
|
||||
expect(instanceProxy.isDisplayed).toBe(false)
|
||||
|
||||
Object.defineProperty(instanceProxy, 'isDisplayed', { value: true })
|
||||
|
||||
expect(instanceProxy.isDisplayed).toBe(true)
|
||||
|
||||
Object.defineProperty(instanceProxy, 'isDisplayed', {
|
||||
get() {
|
||||
return false
|
||||
}
|
||||
})
|
||||
|
||||
expect(instanceProxy.isDisplayed).toBe(false)
|
||||
|
||||
Object.defineProperty(instanceProxy, 'isDisplayed', {
|
||||
get() {
|
||||
return true
|
||||
}
|
||||
})
|
||||
|
||||
expect(instanceProxy.isDisplayed).toBe(true)
|
||||
})
|
||||
|
||||
test('allow spying on proxy methods', () => {
|
||||
let instanceProxy: any
|
||||
const Comp = {
|
||||
render() {},
|
||||
setup() {
|
||||
return {
|
||||
toggle() {}
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
instanceProxy = this
|
||||
}
|
||||
}
|
||||
|
||||
const app = createApp(Comp)
|
||||
|
||||
app.mount(nodeOps.createElement('div'))
|
||||
|
||||
const spy = jest.spyOn(instanceProxy, 'toggle')
|
||||
|
||||
instanceProxy.toggle()
|
||||
|
||||
expect(spy).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
// #864
|
||||
test('should not warn declared but absent props', () => {
|
||||
const Comp = {
|
||||
|
||||
Reference in New Issue
Block a user