接着前面的内容:https://www.cnblogs.com/yanggb/p/12682364.html

单元测试

vue cli拥有开箱即用的通过jest或mocha进行单元测试的内置选项。官方的vue test utils还提供了更多详细的指引和自定义设置。

简单的断言

你不必为了可测性在组件中做任何特殊的操作,导出原始设置就可以了:

<template>
<span>{{ message }}</span>
</template> <script>
export default {
data () {
return {
message: 'hello!'
}
},
created () {
this.message = 'bye!'
}
}
</script>

然后随着vue test utils导入组件,你就可以使用许多常见的断言(这里我们用的是jest风格的expect断言作为示例):

// 导入Vue Test Utils内的shallowMount和待测试的组件
import { shallowMount } from '@vue/test-utils'
import MyComponent from './MyComponent.vue' // 挂载这个组件
const wrapper = shallowMount(MyComponent) // 这里是一些Jest的测试,你也可以使用你喜欢的任何断言库或测试
describe('MyComponent', () => {
// 检查原始组件选项
it('has a created hook', () => {
expect(typeof MyComponent.created).toBe('function')
}) // 评估原始组件选项中的函数的结果
it('sets the correct default data', () => {
expect(typeof MyComponent.data).toBe('function')
const defaultData = MyComponent.data()
expect(defaultData.message).toBe('hello!')
}) // 检查 mount 中的组件实例
it('correctly sets the message when created', () => {
expect(wrapper.vm.$data.message).toBe('bye!')
}) // 创建一个实例并检查渲染输出
it('renders the correct message', () => {
expect(wrapper.text()).toBe('bye!')
})
})

编写可被测试的组件

很多组件的渲染输出由它的props决定。实际上,如果一个组件的渲染输出完全取决于它的props,那么它会让测试变得简单,就好像断言不同参数的纯函数的返回值。看下面的这个例子:

import { shallowMount } from '@vue/test-utils'
import MyComponent from './MyComponent.vue' // 挂载元素并返回已渲染的组件的工具函数
function getMountedComponent(Component, propsData) {
return shallowMount(Component, {
propsData
})
} describe('MyComponent', () => {
it('renders correctly with different props', () => {
expect(
getMountedComponent(MyComponent, {
msg: 'Hello'
}).text()
).toBe('Hello') expect(
getMountedComponent(MyComponent, {
msg: 'Bye'
}).text()
).toBe('Bye')
})
})

断言异步更新

出于vue是异步进行更新dom操作的情况,一些依赖dom更新结果的断言必须在【vm.$nextTick()】resolve之后进行:

// 在状态更新后检查生成的HTML
it('updates the rendered message when wrapper.message updates', async () => {
const wrapper = shallowMount(MyComponent)
wrapper.setData({ message: 'foo' }) // 在状态改变后和断言 DOM 更新前等待一刻
await wrapper.vm.$nextTick()
expect(wrapper.text()).toBe('foo')
})

关于更深入vue单元测试的内容可以到vue test utils的官方文档查看。

"我还是很喜欢你,像细雨洒落八千里,淅淅沥沥。"

最新文章

  1. 深入理解Bindler
  2. Java基础学习小记--多态
  3. 微软BI 之SSIS 系列 - Precedence Constraint 详解优先约束的使用
  4. linux_command_撷叏命令: cut, grep
  5. javaweb数据库操作
  6. loutsScript 常用代码
  7. js里面获取三位不重复值
  8. 提示框alertmsg
  9. css link和@import区别
  10. 安卓服务(Service)的两种开启方式以及服务的生命周期
  11. U3D Trigger事件触发
  12. BZOJ 3626: [LNOI2014]LCA( 树链剖分 + 离线 )
  13. canvas绘制圆形进度条(或显示当前已浏览网页百分比)
  14. TFS实现需求工作项自动级联保存
  15. 微信小程序评分功能
  16. [译]Serilog Tutorial
  17. ButterKnife注解式绑定控件
  18. “Nested exception: 前言中不允许有内容&quot;错误处理
  19. 谈谈ThreadLocal的设计及不足
  20. ES系列七、ES-倒排索引详解

热门文章

  1. iOS、Android 开发的前景真的那么差吗?
  2. DHCP完整过程详解及Wireshark抓包分析
  3. Jmeter 压力测试笔记(2)--问题定位
  4. post登录 jsessionid 以及cookie 传递
  5. GO中的channel使用小结
  6. 2019-07-25【机器学习】无监督学习之聚类 K-Means算法实例 (1999年中国居民消费城市分类)
  7. [总结]最近公共祖先(倍增求LCA)
  8. 【Java】【常用类】 Arrays工具类 源码学习
  9. Spring 下,关于动态数据源的事务问题的探讨
  10. SPFA()判环