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 <constructor>", () => {
  test("resolves like a promise", () => {
    return new PQ<number>((resolve) => {
      setTimeout(() => {
        resolve(1);
      }, 30);
    }).then((val) => {
      expect(val).toBe(1);
    });
  });
})
posted @ 2022-02-22 19:10  刘老六  阅读(41)  评论(0编辑  收藏  举报