码迷,mamicode.com
首页 > 其他好文 > 详细

使用Jest进行单元测试

时间:2019-09-15 00:59:57      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:expec   文件   rip   microsoft   exp   类型   一个   conf   cti   

Jest是Facebook推出的一款单元测试工具。

安装

npm install --save-dev jest ts-jest @types/jest

在package.json中添加脚本:

“test”: "jest"

生成Jest配置文件(生成jest.config.js)

npx ts-jest config:init

使用

先简单编写一个ts文件

function adds(a: number, b: number) {
   return  a+b;
}
function sub(a: number, b: number) {
   return a-b;
}
export { adds, sub };

编写测试用例:

import {adds,sub} from ‘./math‘

test(‘adds: 1 + 1 = 2‘,()=>{
   expect(adds(1,1)).toBe(2);
});
test("sub: 1 - 2 = -1",()=>{
   expect(sub(1,2)).toBe(-1);
});

执行测试脚本:

npm run test

使用ts-jest的好处是,能够在测试用例中进行类型检查。

 

使用Jest进行单元测试

标签:expec   文件   rip   microsoft   exp   类型   一个   conf   cti   

原文地址:https://www.cnblogs.com/V587Chinese/p/11520371.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!