Typescript 项目使用 jest 进行单元测试
安装依赖
1 | npm install --save-dev typescript jest ts-jest @types/jest |
创建jest配置文件
1 | npx ts-jest config:init |
配置jest,往jest.config.js中添加如下配置
1 | const { default: tsjPreset } = require('ts-jest/presets'); |
在package.json中添加脚本
1 | { |
创建测试文件
1 | mkdir tests |
src/index.ts //待测试文件
1 | export function sum(a: number, b: number) { |
index.test.ts
1 | import { sum } = from '../src/index'; |
配置tsconfig.json
1 | { |
目录结构
1 | . |
运行测试
1 | npm run test |