- 选用 jest 做单元测试
- 配置 eslint 的 jest 环境
- 解决 jest 不支持 es module 的问题
- 在test 目录下创建 [name].test.js(name 和 源码中的文件名保持一致)
``shell // 安装jest依赖 npm i -D jest // 使得支持
es module` npm i -D rollup-jest
在package.json中设置
```shell
"scripts":{
"test": "jest",
"test:c": "jest --coverage",
},
"jest": {
"preset": "rollup-jest"
}
在__test__
文件下创建index.test.js
/* eslint-disable no-undef */
const { sum } = require('../src/index')
test('sum success', () => {
expect(sum(1, 2)).toBe(3)
})
src/index.js
文件的内容修改为
function sum() {
let args = Array.from(arguments)
return args.reduce((start, result) => {
return start + result
}, 0)
}
export { sum }
执行 npm run test
进行测试
执行 npm run test:c
查看测试覆盖率
并且会在根目录生成一个converage
目录