vitest 案例

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

vitest 案例
Vitest是一个基于Vue的测试框架,可以用于测试Vue组件。

以下是一个使用Vitest进行测试的示例:
假设我们有一个简单的Vue组件,名为``,用于显示不同类型的通知:
```vue
<template>
<div :class="['notification', type, type === 'error' ? 'notification--error' : null, type === 'success' ? 'notification--success' : null, type === 'info' ? 'notification--info' : null]"></div>
</template>
<script>
export default {
props: {
type: {
type: String,
required: true,
validator: value => ['error', 'success', 'info'].includes(value)
},
message: {
type: String,
default: ''
}
}
}
</script>
```
要测试这个组件,我们需要使用Vitest来编写测试用例。

首先,我们需要安装Vitest:
```shell
npm install vitest --save-dev
```
然后,我们可以创建一个名为``的测试文件,并编写以下测试用例:
```javascript
import { mount } from 'vue/test-utils'
import Notification from '/components/'
describe('', () => {
it('renders the correct class based on type prop', () => {
const wrapper = mount(Notification, {
propsData: { type: 'error' }
})
expect(('.notification').classes()).toContain('notification--error') ()
})
it('adds the correct class when message prop is present', () => { const wrapper = mount(Notification, {
propsData: { type: 'info', message: 'Test message' }
})
expect(('.notification').classes()).toContain('notification--info') expect(('.notification').classes()).toContain('message')
()
})
})
```
在上面的测试用例中,我们使用`mount`函数来创建了一个`Notification`组件的实例,并为其提供了不同的`propsData`。

然后,我们使用`expect`函
数来检查组件的DOM节点是否具有正确的类名。

如果期望和实际结果不符,Vitest将抛出一个错误。

相关文档
最新文档