angular - Jest 单元测试无法使用 WebStorm Jest Testrunner 启动

标签 angular jestjs webstorm nrwl-nx

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

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 应用程序效果很好。

最佳答案

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

  1. 将以下 Jest 配置添加到 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"
        ]
      }
    }
  }

  • 更改编辑配置中的配置以使用package.json而不是jest.config
  • 关于angular - Jest 单元测试无法使用 WebStorm Jest Testrunner 启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57536031/

    相关文章:

    javascript - 如何让 PhpStorm 支持这种语法

    angular - 如何使用/导入http模块?

    html - 无法绑定(bind)到 Angular 4 中的 style.clip-path

    javascript - Jest 测试无法解析 React 组件中的纯 HTML 标签

    javascript - Jest - 简单的测试很慢

    javascript - 使用 Jest 测试 NUXT.js 和 Vue.js 应用程序。获取 '[vuex] module namespace not found in mapState()' 和 '[vuex] unknown action type'

    html - WebStorm 特殊字符编码

    javascript - WebStorm实时模板: how to create an 'export default from' live template/How to get current folder name?

    angular - focusout 和模糊在 Angular 中不起作用

    angular - Electron + Angular 应用程序 + 错误 : Uncaught (in promise): Error: Cannot match any routes. URL 段: