【问题标题】:Jest Unit Tests do not work started with WebStorm Jest Testrunner从 WebStorm Jest Testrunner 开始,Jest 单元测试不起作用
【发布时间】:2019-12-23 10:49:26
【问题描述】:

我正在使用 WebStorm 2019.2、Angular 8 和最新的 nrwl monorepo。当使用 ng test <app-name> 开始 Angular 测试时,一切正常。如果我尝试从 WebStorm 中运行测试(通过单击编辑器中的绿色三角形),我会收到以下错误:

Error: Failed: "Zone is needed for the async() test helper but could not be found.
Please make sure that your environment includes zone.js/dist/zone.js"

我尝试了数小时的谷歌搜索以找到解决该问题的方法,但没有成功。知道如何解决这个问题吗?

PS:monorepo 还托管了一个 NestJS 服务器。在 WebStorm 中运行单个测试适用于 NestJS 应用程序。

【问题讨论】:

标签: angular jestjs webstorm nrwl-nx


【解决方案1】:

请尝试https://github.com/nrwl/nx/pull/1467#issuecomment-506973401 的解决方法:

  1. 将以下笑话配置添加到 package.json 并在必要时进行调整:
 "jest": {
    "preset": "jest-preset-angular",
    "setupFilesAfterEnv": [
      "<rootDir>/apps/nx-jest-example/src/test-setup.ts"
    ],
    "testPathIgnorePatterns": [
      "<rootDir>/node_modules/",
      "<rootDir>/dist/",
      "<rootDir>/src/test.ts",
      "<rootDir>/apps/nx-jest-example-e2e/"
    ],
    "globals": {
      "ts-jest": {
        "diagnostics": false,
        "tsConfig": "<rootDir>/apps/nx-jest-example/tsconfig.spec.json",
        "stringifyContentPathRegex": "\\.html$",
        "astTransformers": [
          "<rootDir>/node_modules/jest-preset-angular/InlineHtmlStripStylesTransformer"
        ]
      }
    }
  }

  1. 在 Edit Configurations 中更改配置以使用 package.json 而不是 jest.config

【讨论】:

    猜你喜欢
    • 2018-12-24
    • 2018-12-16
    • 2020-08-04
    • 2020-10-30
    • 1970-01-01
    • 2022-01-06
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多