-
Notifications
You must be signed in to change notification settings - Fork 248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Computed ref unwrapping is different than in vue #2196
Computed ref unwrapping is different than in vue #2196
Comments
I think this is the wrong repo, moving to @vue/test-utils |
This one slipt my radar as it was transferred from test-utils v1, but this indeed looks like an issue. I made a simpler repro, to ensure this was not something related to the Options API or the class usage. <template>
<div id="app">
{{ storeValue }}
</div>
</template>
<script setup lang="ts">
import { computed, Ref } from 'vue';
const props = defineProps<{ store: { count: Ref<number> } }>();
const storeValue = computed(() => props.store.count.value + 1);
</script> import { mount } from '@vue/test-utils';
import { ref } from 'vue';
import { describe, it, expect } from 'vitest';
import HelloWorld from '../HelloWorld.vue';
describe('HelloWorld', () => {
it('can access the nested computed value', async () => {
const wrapper = mount(HelloWorld, {
props: {
store: {
count: ref(2),
},
},
});
expect(wrapper.get('#app').text()).toBe('3'); // fails
expect(wrapper.vm.storeValue).toBe(3); // fails
});
}); The issue goes away if we drop the store level in the props: <template>
<div id="app">
{{ storeValue }}
</div>
</template>
<script setup lang="ts">
import { computed, Ref } from 'vue';
const props = defineProps<{ count: Ref<number> }>();
const storeValue = computed(() => props.count.value + 1);
</script> describe('HelloWorld', () => {
it('can access the nested computed value', async () => {
const wrapper = mount(HelloWorld, {
props: {
count: ref(2),
},
});
expect(wrapper.get('#app').text()).toBe('3'); // succeeds
expect(wrapper.vm.storeValue).toBe(3); // succeeds
});
}); So there is an issue with nested refs inside objects that we need to take a look at. |
I am still having this issue as of today, are there any updates? Passing an object that contains a nested ref or computed ref as a prop directly unwraps the reactive nodes in the component's mounted instance. |
I am not sure why this happens, but it looks like we grab all of the |
I am experiencing the same problem and unfortunately this is blocking for most of my tests: any update about it? |
Not right now, would you like to take a look and try to fix it? I am not able to do it right now. |
@lmiller1990 hi there! I gave it a shot in this PR. It seems that the issue was due to the |
The issue is highlighted here: vuejs/test-utils#2196 Solution was made but only works for vue 3: vuejs/test-utils#2196 Skipping it for now, tested in manually
Subject of the issue
Hey, I'm working on a codebase where we use computed refs outside of vue components.
A store class has a computed ref (
store.count
in the stackblitz environment), an instance of this class is passed as prop to a vue component.In Vue itself,
store.count
is a ComputedRefImpl, where you still need .value to access the value.In Vue-test-utils
store.count
already is the value itself, andstore.count.value
will be undinefedSteps to reproduce
I have created a stackblitz environment
When viewing the ui, App.storeValue is
3
, and store.count is a ComputedRefImplIf you kill the devserver and
npm test
App.storeValue isNaN
, because store.count is the value itself, so store.count.value is undefinedExpected behaviour
store.count should be a computedRefImpl which still needs
.value
to access the valueActual behaviour
store.count immediately contains the value, unlike in vue itself
Possible Solution
?
The text was updated successfully, but these errors were encountered: