美文网首页
001 前端项目中加入jest , 并支持ts

001 前端项目中加入jest , 并支持ts

作者: 愚蠢的二师弟 | 来源:发表于2020-03-11 19:39 被阅读0次

    1 加入 jest ts-jest @types/jest

    jest 为测试框架
    tes-jest 用于转换ts代码
    @types/jest 用于vscode中防止报错

    yarn add  jest ts-jest @types/jest
    

    2 前端目录中, 新建 test目录

    是 test 前后各有2个 底杠

    image.png

    3 package.json中加入如下配置

      "jest": {
        //  把ts 结尾的文件转换成 js  文件
        "transform": {
          ".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
        },
       // 测试文件代码目录
        "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
       //  测试文件支持的后缀
        "moduleFileExtensions": [
          "js",
          "jsx",
          "ts",
          "tsx"
        ],
        "moduleDirectories": [
          "node_modules"
        ],
        "moduleNameMapper": {
          "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__jest__/__mocks__/fileMock.js",
          "\\.(css|scss)$": "identity-obj-proxy",
          "^cpn(.*)$": "<rootDir>/src/components$1"
        }
      }
    

    相关文章

      网友评论

          本文标题:001 前端项目中加入jest , 并支持ts

          本文链接:https://www.haomeiwen.com/subject/vlbhjhtx.html