【问题标题】:ng test command is not working with angular 8 and Jestng test 命令不适用于 angular 8 和 Jest
【发布时间】:2019-10-23 18:56:58
【问题描述】:

当我尝试使用 Angular 8 和 Jest 使用“npm run test”命令运行测试用例时遇到问题

我正在尝试从 Karma/jasmine 框架迁移到 Jest,并将 Angular 版本升级到 8。

但是npm run test 命令抛出以下错误

找不到文件:/src/tsconfig.spec.json(解析为: D:\app-jest\src\tsconfig.spec.json)

在 ConfigSet.resolvePath (node_modules/ts-jest/dist/config/config->set.js:712:19)

包.json

   {
      scripts": {
          "ng": "ng",
          "start": "ng serve",
          "test": "jest"
     }

jest.config.js

 module.exports = {
    preset: 'jest-preset-angular',
    roots: ['src'],
    setupFilesAfterEnv: ["./src/setup-jest.ts"]
 };

【问题讨论】:

    标签: angular jestjs karma-jasmine


    【解决方案1】:

    这是快速修复 将tsconfig.spec.json 移回 src。 更新jest.config.js:

    较新的版本已修复此问题。参考以下话题

    https://github.com/briebug/jest-schematic/issues/12

    【讨论】:

    • 当我尝试运行“jest:command”或“jest --watch”时,我模仿了相同的过程仍然找不到 tsconfig.spec.json。ng 测试有效,但我想使用 jest --watch。我对单元测试和玩笑很陌生。
    【解决方案2】:

    这是我的建议:

    1.在您的 package.json 文件中使用此语句。显然,您可能需要更改文件的路径。

      "jest": {
        "preset": "jest-preset-angular",
        "setupFilesAfterEnv": [
          "<rootDir>/src/setupJest.ts"
        ],
        "globals": {
          "ts-jest": {
            "tsConfig": "./tsconfig.spec.json",
            "diagnostics": false,
            "stringifyContentPathRegex": "\\.html$",
            "astTransformers": ["<rootDir>/node_modules/jest-preset-angular/InlineHtmlStripStylesTransformer"]
          }
        }
      }
    

    2。我不得不从src 文件夹中删除test.ts 文件。与此 jasmine 脚本相关的冲突。

    3.我还在tsconfig.app.json

    中添加了以下语句
      "compilerOptions": {
        //...
        "types": [
          "jest"
        ]
      }
    

    希望对您有所帮助,我建议您阅读此article

    【讨论】:

      猜你喜欢
      • 2019-03-25
      • 1970-01-01
      • 2021-11-27
      • 1970-01-01
      • 1970-01-01
      • 2016-07-14
      • 2019-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多