如何对typescript进行单元测试

  |  

安装

yarn add -D ts-jest jest @types/jest

全局安装

npm i jest -g

初始化 jest

jest --init

移除生成的 jest.config.js 文件

配置

在 package.json 中配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": ".",
"testRegex": ".spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"coverageDirectory": "./coverage",
"testEnvironment": "node",
"roots": [
"<rootDir>/test/"
]
}

使用

1
2
3
4
5
describe("test", () => {
beforeAll(async () => {});

it("test", () => {});
});
文章目录
  1. 1. 安装
    1. 1.1. 全局安装
  2. 2. 初始化 jest
  3. 3. 移除生成的 jest.config.js 文件
  4. 4. 配置
  5. 5. 使用