开源代码,自动化测试试标配。主要用于回归测试。
比如你提交了一行代码,代码管理者需要在最短时间分析出他对以前功能的影响。
最有效的方法就是跑一遍原来已经编写好的测试用例。
反过来你的代码也一定要由自动化测试case才可以
1、运行和测试代码
yarn
npm run test
2、编写Helloworld代码
module.exports = function (callback) {
callback && callback('hi');
console.log('helloworld and wudexiong');
return 'helloworld';
}
3、编写测试用例
describe('test helloworld index' , () => {
//测试用例 test return
test("should return helloworld string" , () => {
const hello = require("../index");
const result = hello();
expect(result).toBe('helloworld');
})
//测试用例 test callback
test('should call fn and receive a hi' , () => {
const hello = require("../index");
// jest下创建的函数,内部会记录调用历史
const fn = jest.fn();
hello(fn);
const calls = fn.mock.calls;
// 数组第一维表示第几次调用, 第二维表示当次调用的参数
expect(calls.length).toBe(1);
expect(calls[0][0]).toBe('hi')
})
})
4、跑测试
jest helloworld