[英]Vue composition API computed property test
I starting to use the composition API in a new Vue project and start developing with TDD.我开始在一个新的 Vue 项目中使用组合 API 并开始使用 TDD 进行开发。 Also, I start to use TypeScript.
另外,我开始使用 TypeScript。
I create a very simple Component Logo.vue
我创建了一个非常简单的组件
Logo.vue
<template>
<div>
<div class="c-logo__fullname">{{ fullName }}</div>
</div>
</template>
<script lang="ts">
import { defineComponent, computed } from '@vue/composition-api'
interface User {
firstName: string
lastName: number
}
export default defineComponent({
props: {
user: {
type: Object as () => User,
required: true,
},
},
setup({ user }) {
const fullName = computed(() => `${user.firstName} ${user.lastName}`)
return {
fullName,
}
},
})
</script>
And now I starting to test very basic stuff in Logo.spec.js
file现在我开始在
Logo.spec.js
文件中测试非常基本的东西
import { mount } from '@vue/test-utils'
import Logo from '@/components/Logo.vue'
let wrapper = null
const user = {
firstName: 'Test',
lastName: 'User',
}
beforeEach(() => {
wrapper = mount(Logo, {
propsData: {
user,
},
})
})
afterEach(() => {
wrapper.destroy()
})
describe('Logo', () => {
test('is a Vue instance', () => {
expect(wrapper.vm).toBeTruthy()
})
test('User has firstname "Test"', () => {
expect(wrapper.props().user.firstName).toBe('Test')
})
test('User has lastname "User"', () => {
expect(wrapper.props().user.lastName).toBe('User')
})
test('User has fullname "Test User"', () => {
expect(wrapper.find('.c-logo__fullname').text()).toBe('Test User')
})
})
My problem now is that the test for the output of my computed property fullName
fails every time.我现在的问题是我的计算属性
fullName
的 output 的测试每次都失败。
FAIL test/Logo.spec.js
Logo
✓ is a Vue instance (11 ms)
✓ User has firstname "Test" (2 ms)
✓ User has lastname "User" (2 ms)
✕ User has fullname "Test User" (7 ms)
● Logo › User has fullname "Test User"
expect(received).toBe(expected) // Object.is equality
Expected: "Test User"
Received: ""
35 |
36 | test('User has fullname "Test User"', () => {
> 37 | expect(wrapper.find('.c-logo__fullname').text()).toBe('Test User')
| ^
38 | })
39 | })
40 |
at Object.<anonymous> (test/Logo.spec.js:37:54)
Also when I make a console.log(wrapper.html())
I get only an empty div-container.此外,当我制作
console.log(wrapper.html())
时,我只得到一个空的 div 容器。 Why?为什么?
I followed some articles to start with TDD and Vue and also composition API, but I tried now a lot of scenarios without passing the test for the computed property.我关注了一些文章,从 TDD 和 Vue 开始,还编写了 API,但我现在尝试了很多场景,但没有通过计算属性的测试。
I'm happy about every little help you can give me and save my day.我很高兴你能给我的每一个小帮助,拯救我的一天。
I had the same problem.我有同样的问题。 It looks like you need to include the composition API in the test, and then attach it to the vue instance:
看来您需要在测试中包含组合 API ,然后将其附加到 vue 实例:
import CompositionApi from '@vue/composition-api'
const localVue = createLocalVue()
beforeEach(() => {
localVue.use(CompositionApi)
})
As described here Vue composition API computed property test如此处所述Vue 组成 API 计算属性测试
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.