@@ -22,13 +22,17 @@ import { ShapeFlags } from './utils/vueShared'
2222 */
2323function createVMProxy < T extends ComponentPublicInstance > (
2424 vm : T ,
25- setupState : Record < string , any >
25+ setupState : Record < string , any > ,
26+ exposed : Record < string , any > | null
2627) : T {
2728 return new Proxy ( vm , {
2829 get ( vm , key , receiver ) {
2930 if ( vm . $ . exposed && vm . $ . exposeProxy && key in vm . $ . exposeProxy ) {
3031 // first if the key is exposed
3132 return Reflect . get ( vm . $ . exposeProxy , key , receiver )
33+ } else if ( exposed && key in exposed ) {
34+ // first if the key is exposed
35+ return Reflect . get ( exposed , key , receiver )
3236 } else if ( key in setupState ) {
3337 // second if the key is acccessible from the setupState
3438 return Reflect . get ( setupState , key , receiver )
@@ -107,11 +111,24 @@ export class VueWrapper<
107111 // if we return it as `vm`
108112 // This does not work for functional components though (as they have no vm)
109113 // or for components with a setup that returns a render function (as they have an empty proxy)
110- // in both cases, we return `vm` directly instead
114+ // in both cases, we return `vm` directly instead.
115+ //
116+ // NOTE https://github.com/vuejs/test-utils/issues/2591
117+ // I'm sry i'm not entirely sure why, but exposed properties — via expose/defineExpose
118+ // are not assigned to the componentVM when the the `vm` argument provided
119+ // to this constructor comes from `findComponent` — as in, not the original instance
120+ // but already the proxied one. I first suspected that was by design of defineExpose
121+ // but that doesn't explain why it works when finding a .vue component or
122+ // vs it's bundled version, where the different is conversion of to a render
123+ // function. Also i've noticed that sometimes we can get some exceptions in
124+ // bundle code becuase render function is hoisted and exposed if properties
125+ // are returned to template, they also become available in th einstance.
126+ //
111127 if ( hasSetupState ( vm ) ) {
112- this . componentVM = createVMProxy < T > ( vm , vm . $ . setupState )
128+ this . componentVM = createVMProxy < T > ( vm , vm . $ . setupState , vm . $ . exposed )
113129 } else {
114130 this . componentVM = vm
131+ Object . assign ( this . componentVM , vm . $ . exposed )
115132 }
116133 this . __setProps = setProps
117134
0 commit comments