单元测试初体验
相关文档
JS的:jestjs
Vue的:testing-library
安装 Jest
npm install -g jest
或局部:
npm install -D jest
在 package.json 中指定 test 脚本:
{
"scripts": {
"test": "jest"
}
}
Jest 的最基础,最常用的三个 API 是:describe、test 和 expect。
- describe 是 test suite(测试套件)
- test (也可以写成 it) 是 test case(测试用例)
- expect 是断言
完整的匹配器
运行单元测试
//运行该命令将执行该文件夹下所有'.test.js'文件
npm test
//运行某个文件
npm run jest -t XXX.test.js
当然,如果想要看到覆盖率的报告,可以使用 jest --coverage,或者 jest-report。
在 VS Code 中,我们也可以安装插件:Jest Runner。