【发布时间】:2021-02-02 17:48:15
【问题描述】:
我正在尝试使用 Typescript 专注于 TDD,因此我使用 vue-cli 构建了一个新的 Vue 项目(我选择使用 Vue3、Typescript 和 Jest)。但是,当我开箱即用地运行单元测试时,它失败了。经过一番调查,我发现来自@vue/test-utils 的mount 命令不会呈现任何道具值:
HelloWorld.vue
<template>
<div class="hello">
<h1>{{ msg }}</h1>
{{ test }}
</div>
</template>
<script lang="ts">
import { Options, Vue } from "vue-class-component";
@Options({
props: {
msg: String,
}
})
export default class HelloWorld extends Vue {
msg!: string;
test: string = "It's a test";
}
</script>
example.specs.ts
import { mount } from "@vue/test-utils";
import HelloWorld from "@/components/HelloWorld.vue";
describe("HelloWorld.vue", () => {
it("renders props.msg when passed", async () => {
const msg = "new message";
const wrapper = mount(HelloWorld, {
props: { msg }
});
expect(wrapper.text()).toContain(msg);
});
});
当我打印 wrapper.html() 时,我得到以下信息:
<div class="hello" msg="new message"><h1></h1></div>
它似乎根本没有将msg 或test 渲染到html 中。它已将其列为道具,仅此而已。
我认为其中一个原因可能是我使用的是 Typescript 组件而不是传统的组件,这可能会导致它失败,但我不确定。
有什么建议/提示吗?
【问题讨论】:
标签: typescript unit-testing vue.js jestjs vue-cli