如何测试
步骤
- 选择测试工具:jest
- 设计测试用例
- 写测试、运行测试、改代码
- 单元测试、功能测试、集成测试
重点
- 单元测试不应该与外界打交道(那是集成测试要做的)
- 单元测试的对象是函数
- 功能测试的对象是模块
- 集成测试的对象是系统
安装
$ yarn add --dev jest
- 创建要测试的 sum.js
function sum(a, b) {
return a + b;
}
module.exports = sum;
- 创建测试文件 sum.test.js
const sum = require('./sum');
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
- 配置 jest 到 package.json
{
"scripts": {
"test": "jest"
}
}
- 运行测试
$ yarn test