简体   繁体   English

在 angular 15 项目的 src 文件夹外进行业力测试

[英]Having karma tests outside src folder in angular 15 project

I've had karma test spec files outside the src folder in a test folder on the root level of the project.我在项目根级别的测试文件夹中的 src 文件夹外有业力测试规范文件。 After updating angular from 14 to 15, karma needs the test spec files to be in whatever folder the sourceRoot says in the angular.json file.将 angular 从 14 更新到 15 后,karma 需要测试规范文件位于 sourceRoot 在 angular.json 文件中指定的任何文件夹中。

// angular.json
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "project-name": {
      "root": "",
      "sourceRoot": "", // <<===

Is there a way to tell karma to look at the workspace root level for test downwards without changing the sourceRoot attribute in angular.json?有没有办法告诉 karma 在不更改 angular.json 中的 sourceRoot 属性的情况下向下查看工作区根级别进行测试?

I changed the sourceRoot path to "" and then Karma finds the tests as long as they're in the workspace folder and down, since i'm looking for tests with **/*.spec.ts in the我将 sourceRoot 路径更改为“”,然后 Karma 找到测试,只要它们在工作区文件夹中并向下,因为我正在寻找带有 **/*.spec.ts 的测试

// tsconfig.spec.json
"include": [
    "**/*.spec.ts",
    "**/*.d.ts",
  ]

So i'm going to answer my own question.所以我要回答我自己的问题。 This answer also leaves the sourceRoot value to whatever you choose.该答案还将 sourceRoot 值留给您选择的任何值。 The answer is that in the angular.json file, you need to include the spec files in each project test configuration relative to the sourceRoot for that project configuration.答案是在 angular.json 文件中,您需要在每个项目测试配置中包含与该项目配置的 sourceRoot 相关的规范文件。 Like so:像这样:

// angular.json
"test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "polyfills": ["zone.js/testing", "zone.js"],
            "tsConfig": "test/tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "include": [
              "../test/**/**.spec.ts",
              "../test/**/**.d.ts"
            ]
          }
        }

Then you also need to include those same files in the tsconfig.spec.json file relevant to each test configuration.然后,您还需要在与每个测试配置相关的 tsconfig.spec.json 文件中包含这些相同的文件。 Like so:像这样:

// tsconfig.spec.json
{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/spec",
    "types": [
      "jasmine",
      "node"
    ]
  },
  "files": [
    "../demo/src/polyfills.ts"
  ],
  "include": [
    "**/*.spec.ts"
  ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM