Skip to content
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

v3.2.45: regression in Vue Test Utils wrapper.vm for script setup variables #7103

Closed
cexbrayat opened this issue Nov 11, 2022 · 8 comments
Closed

Comments

@cexbrayat
Copy link
Member

Vue version

3.2.45

Link to minimal reproduction

https://stackblitz.com/edit/vitest-dev-vitest-1djhp4?file=components%2FHello.vue,test%2Fbasic.test.ts,package.json&initialPath=__vitest__

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

https://github.com/vuejs/test-utils/blob/ff93e5e60ac8482cf4326f9f83fffa8dac08f8b6/src/vueWrapper.ts#L49-L62

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

@cexbrayat cexbrayat changed the title v3.2.45: regression in Vue Test Utils for wrapper.vm v3.2.45: regression in Vue Test Utils for wrapper.vm for script setup variables Nov 11, 2022
@cexbrayat cexbrayat changed the title v3.2.45: regression in Vue Test Utils for wrapper.vm for script setup variables v3.2.45: regression in Vue Test Utils wrapper.vm for script setup variables Nov 11, 2022
@LinusBorg
Copy link
Member

LinusBorg commented Nov 11, 2022

This is an intended change to solve dev/prod inconsistencies (#6248), so we should try and see that we can keep it. I think test-utils could use instance.setupState instead?

@cexbrayat
Copy link
Member Author

Thanks @LinusBorg
I think setupState lacks $, $options, and others, which breaks other test cases.

@LinusBorg
Copy link
Member

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)

@LinusBorg
Copy link
Member

LinusBorg commented Nov 11, 2022

@cexbrayat
Copy link
Member Author

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 👍

vuejs/test-utils#1858

@cexbrayat

This comment was marked as off-topic.

@LinusBorg

This comment was marked as off-topic.

@yyx990803
Copy link
Member

Closing as intended - it's important to keep the prod/dev behavior consistent for normal users, and it only affects dev behavior, so this change is considered a fix.

@github-actions github-actions bot locked and limited conversation to collaborators Sep 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants