参考官方文档使用:https://jestjs.io/docs/zh-Hans/getting-started.html
首先来一个简单的例子
使用npm和yarn工具安装都是可以的
npm install jest -g
安装jest后就可以进行测试了,测试步骤如下
1.在终端npm init一个文件夹
2.创建被测试的js文件,里面写一个函数,使用common.js或者AMD导出
function sum(a, b) {
return a + b;
}
module.exports=sum
3.创建一个测试js文件,里面导入被测试js模块
const sum = require('./sum.js')
test("add 1 +2 equal 3", () => {
expect(sum(1, 2)).toBe(3);
})
4.使用test方法进行测试
test方法两个参数,第一个期望值,第二个是测试结果布尔。
5.package.json里面引入script:jest
{
"name": "jest",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"scripts": {
"test": "jest"
}
}
6.终端使用npm run test或者npm test,测试成功如图