language-tools icon indicating copy to clipboard operation
language-tools copied to clipboard

VueWrapper typing problem

Open stevemandl opened this issue 2 years ago • 3 comments

Observed: While editing the following test file,

// App.spec.ts
import { mount } from "@vue/test-utils"
import { describe, it, expect } from "vitest"
import App from "../src/App.vue"

describe("App", () => {
    it("toggles", () => {
        const wrapper = mount<typeof App>(App)
        expect(wrapper.element.tagName).toBe("DIV")
        expect(wrapper.vm.editButton).toBeFalsy()
    });
});

for this .vue file:

// App.vue
<template>
    <div> <input type="button" :value="String(editButton)" @click="editButton = !editButton" /> </div>
</template>

<script lang="ts" setup>
import { ref } from "vue";
const editButton = ref<boolean>(false)
</script>

the type of wrapper.vm is ComponentPublicInstance instead of the specific type of the component. This causes the following to be displayed: image However, the test runs without a problem.

Expected behavior: Volar will reference the component type when validating code, and not report a problem if there isn't one.

Example project to reproduce problem: https://github.com/stevemandl/vitest_sample

stevemandl avatar Dec 18 '23 17:12 stevemandl

🤔 Shall we expose all variables in <script setup> block?

so1ve avatar Dec 19 '23 08:12 so1ve

@so1ve I'm not sure if that question was for me or not, but I'll lend my 2 cents: https://github.com/vuejs/test-utils/pull/931 is where VTU exposed everything on wrapper.vm to allow the sample test pass. I'm not sure if this is a problem with VTU or volar, but running vitest from the console passes. Since I am just seeing the problem message in vscode so I thought I would start by reporting it here. Sorry I can't be more help.

stevemandl avatar Dec 19 '23 18:12 stevemandl

Just for reference, there is more context in https://github.com/vuejs/test-utils/issues/972, in which @pikax mentions that he is doing some work on https://github.com/vuejs/core/pull/4465 (now https://github.com/vuejs/core/pull/9556) to improve types and may help solve this issue.

Clarkkkk avatar Feb 03 '24 16:02 Clarkkkk

Closed because the type returned by Volar is as expected, and Vue currently has no method to define properties in the component type that are only exposed to the template.

johnsoncodehk avatar Dec 02 '25 13:12 johnsoncodehk