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

Types for wrapper.vm when using child component #2058

Closed
Swoox opened this issue May 17, 2023 · 5 comments
Closed

Types for wrapper.vm when using child component #2058

Swoox opened this issue May 17, 2023 · 5 comments
Labels
bug Something isn't working

Comments

@Swoox
Copy link

Swoox commented May 17, 2023

Describe the bug
If try to access a variable of an hook with a child component, the vue-tsc give an error. wrapper.vm does not have a stateForm property, because wrapper.vm is typed as ComponentPublicInstance.

To Reproduce
https://stackblitz.com/edit/vitest-dev-vitest-ztd6bn

Expected behavior
No error.

Related information:
See #972
Tested with #2026

Workaround

const childComp = wrapper.findComponent(ChildComp);
const childWrapper = mount(childComp);
expect(childWrapper.vm.stateForm.length).toBe(undefined);
@Swoox Swoox added the bug Something isn't working label May 17, 2023
@cexbrayat
Copy link
Member

Yeah, as you mentioned this is related to #972 , and we can't fix it in VTU, it has to be fixed in either Vue itself or Volar.

As a workaround, you can expose stateForm or explicitly cast wrapper.vm in your test.

Closing as duplicate of #972

@cexbrayat cexbrayat closed this as not planned Won't fix, can't repro, duplicate, stale May 17, 2023
@Swoox
Copy link
Author

Swoox commented May 17, 2023

I actually tested #972 with #2026 and seems that 2026 fixes 972.
Check https://stackblitz.com/edit/vitest-dev-vitest-1rkt44
So I see this as a different issue/bug.

<script setup lang="ts">
import { computed, ref } from 'vue';
...
const times = ref(2);
...
</script>

And test with
expect(wrapper.vm.times).toBe(2);

@cexbrayat
Copy link
Member

time being undefined sounds correct no? But times should be 2

But if that compiles correctly then this is a regression in the types of wrapper.vm in the alpha release: wrapper.vm.type should not compile

@Swoox
Copy link
Author

Swoox commented May 17, 2023

Ofc time is undefined..
expect(wrapper.vm.times).toBe(2);
My bad.

@Swoox Swoox changed the title Bug: Types for wrapper.vm when using child component May 17, 2023
@cexbrayat
Copy link
Member

No worries. Your issue made me realize that we may have a regression in our typings as that should not compile.
See #2060 if you're interested

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants