vuejs / test-utils

Vue Test Utils for Vue 3
https://test-utils.vuejs.org
MIT License
1.05k stars 245 forks source link

Bug: ref reactivity isn't maintained when testing #2502

Closed InjustFr closed 3 months ago

InjustFr commented 3 months ago

Describe the bug When updating a non template ref through code, v-if changes doesn't seem to update As you can see in the linked stackblitz, we have a conditional rendering depending on a ref opened with a default value of false. This ref is updated on mount thanks to a prop called show

As you'll find in the test, we set the show prop to true on mount but the DOM isn't updated and the content isn't rendered, failing the test. If you dump the HTML you can see it renders to <!--v-if--> instead of the desired html structure

This is a simplified case of a more complex component where I need to have a separate prop from the conditional rendering

To Reproduce https://stackblitz.com/edit/github-dgffss?file=src%2Fcomponents%2FHelloWorld.vue

Expected behavior I would expect ref reactivity to still work in test environments

cexbrayat commented 3 months ago

Hi @InjustFr

Thanks for the repro. As you update the ref in onMounted, you need to call await nextTick(); in your test after mounting your component to let Vue update the template.

  it('renders properly', async () => {
    const wrapper = mount(HelloWorld, {
      props: { msg: 'Hello Vitest', show: true },
    });
    await nextTick();

See https://test-utils.vuejs.org/guide/advanced/async-suspense.html