Unit testing using Jest

Jest is Facebook launched a unit testing tool.

installation

--save-altitude install the dev @ ts-types is / is

Adding script package.json in:

“test”: "jest"

Jest generate configuration files (generated jest.config.js)

NPX ts-config is: init

use

Ts briefly to write a file

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

Writing test cases:

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);
});

Execute test scripts:

altitude test run

The benefits of using ts-jest to be able to type checking in the test case.

 

Guess you like

Origin www.cnblogs.com/V587Chinese/p/11520371.html