v3.2.45: regression in Vue Test Utils `wrapper.vm` for script setup variables
Vue version
3.2.45
Link to minimal reproduction
Steps to reproduce
Given a component like:
<script setup lang="ts">
import { computed, ref } from 'vue';
const count = ref(4);
const times = ref(2);
const result = computed(() => count.value * times.value);
defineExpose({
result,
});
</script>
<template>
<div>{{ count }} x {{ times }} = {{ result }}</div>
</template>
VTU allows to write a test like:
import { mount } from '@vue/test-utils';
import Hello from '../components/Hello.vue';
test('mount component', async () => {
expect(Hello).toBeTruthy();
const wrapper = mount(Hello);
expect(wrapper.text()).toContain('4 x 2 = 8');
// result is exposed
expect(wrapper.vm.result).toBe(8);
// count and times are not exposed, but VTU exposes vm.$.proxy
expect(wrapper.vm.count).toBe(4);
expect(wrapper.vm.times).toBe(2);
});
VTU can access count, times and result (even if only result is exposed) because we are internally using vm.$.proxy
for the VM in script setup components
Vue v3.2.45 introduced changes that are breaking this use case.
Is there a way that we can keep this feature in VTU? Either by making the recent changes less breaking, or using another workaround in VTU.
What is expected?
The unit test should succeed, as it does with Vue v3.2.44
What is actually happening?
wrapper.vm
no longer has access to script setup variables
System Info
No response
Any additional comments?
No response
This is an intended change to solve dev/prod inconsistencies (https://github.com/vuejs/core/issues/6248), so we should try and see that we can keep it. I think test-utils could use instance.setupState
instead?
You would have to mimick what the component proxy itself does to an extend: i.e. check the setup state for the accessed key first, and if that fails, try the proxy (for all those public APIs)
I do something similar here: https://github.com/LinusBorg/vue-mixable/blob/main/src/vmContextProxy.ts
Ha! I was coming to the same proxy idea on my side: combining with your code, I have something that works with v3.2.45 and v3.2.44 Thanks for your help @LinusBorg 👍
I do something similar here: https://github.com/LinusBorg/vue-mixable/blob/main/src/vmContextProxy.ts
Unrelated @LinusBorg: shouldn't you have vm
as a parameter here https://github.com/LinusBorg/vue-mixable/blob/main/src/vmContextProxy.ts#L15 (or m
here https://github.com/LinusBorg/vue-mixable/blob/main/src/vmContextProxy.ts#L19)?