demo.js部分——需要测试的文件和方法
function jestdemo1fn(num){
return num>=5?'休息':'工作'
}
function jestdemo2fn(num){
return num>=1?'onetwothree':'123'
}
module.exports={
jestdemo1fn, jestdemo2fn
}
demo.test.js部分——自动化测试文件
const demojs = require('./demo');
const {jestdemo1fn,jestdemo2fn} = demojs
test('工作状态',()=>{
expect(jestdemo1fn(2)).toBe('工作')
})
test('中英文2',()=>{
expect(jestdemo2fn(2)).toBe('onetwothree')
})
test的第一个参数 是这个测试的描述 后面跟着的就是一个简单的方法名 传递的参数 toBe后是得到的结果
需要注意的点是expect 不是export 别写错了
修改你的package的部分
将scripts中 test 的值 修改成jest
"scripts": {
"test": "jest"
},
在控制台运行
npm run test
如果出错和测试结果不符合则是下面这种
const demojs = require('./demo');
const {jestdemo1fn,jestdemo2fn} = demojs
test('工作状态',()=>{
expect(jestdemo1fn(2)).toBe('工作')
})
test('中英文2',()=>{
expect(jestdemo2fn(2)).toBe('123')
})