chai
istanbul //统计覆盖率
mocha
superagent
supertest
// 单元测试文件 test/test.js
const chai = require('chai');
const expect = chai.expect;
// const mocha = require('mocha');
const getNum = require('../index')// 【待测的功能函数】
describe('Test', function() {
it('should return 20 when the value is 10', function() {
expect(getNum(10)).to.equal(20)
})
it('should return empty when the value is empty', function() {
expect(getNum()).to.equal('')
})
it('should return string when the value is string', function() {
expect(getNum('sdsd')).to.equal('sdsd')
})
})
// 项目业务文件(待测的函数)
const getNum = (value) => {
if(typeof value=='string') {
return value;
} else {
if(!value) {
return '';
} else {
return value * 2;
}
}
}
module.exports = getNum;
// package.json
"scripts": {
"test": "istanbul cover ./node_modules/mocha/bin/_mocha",
"test-cov": "node ./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- --timeout=500000 --recursive test/test.js --bail"
},
运行npm run test-cov
结果如下图?