Typescript 程序 极限简洁 测试上手
使用ts-jest
官网,如果没变的话
- 安装
npm install --save-dev jest typescript ts-jest @types/jest
- 配置 (jest config file)
npx ts-jest config:init
这告诉 jest如何处理 .ts后缀的文件 - 命令设置
"test":"jest --watchAll"
在package.json的scripts 里 加上这一条 - 测试
运行npm run test
即可
提普斯
如果你没有 .jest.config.js ,也可以直接在package.json的顶层加这下面的配置,其实是一样的内容
"jest": {
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
]
}
- 测试样例
describe("PQ ", () => {
test("resolves like a promise", () => {
return new PQ((resolve) => {
setTimeout(() => {
resolve(1);
}, 30);
}).then((val) => {
expect(val).toBe(1);
});
});
})