2

升级到 Angular 6 和 Karma 1.7.1 后,该命令ng test --environment=test不再考虑--environment标志,并且始终使用 dev 的值(来自environment.ts)运行。

有谁知道如何解决这个问题?也不再尊重singleRun: falsefrom并且浏览器在完成后立即关闭。karma.conf.js

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "meteor-app": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist",
            "index": "src/index.html",
            "main": "src/main.ts",
            "tsConfig": "src/tsconfig.app.json",
            "polyfills": "src/polyfills.ts",
            "stylePreprocessorOptions": {
              "includePaths": [
                "src/sass",
                "node_modules"
              ]
            },
            "assets": [
              "src/assets"
            ],
            "styles": [
              "src/sass/styles.scss",
              "node_modules/ng2-toasty/style-material.css"
            ],
            "scripts": []
          },
          "configurations": {
            "test": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.test.ts"
                }
              ]
            },
            "staging": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.staging.ts"
                }
              ]
            },
            "production": {
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "meteor-app:build"
          },
          "configurations": {
            "test": {
              "browserTarget": "meteor-app:build:test"
            },
            "staging": {
              "browserTarget": "meteor-app:build:staging"
            },
            "production": {
              "browserTarget": "meteor-app:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "meteor-app:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "karmaConfig": "./karma.conf.js",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "scripts": [],
            "styles": [
              "src/sass/styles.scss",
              "node_modules/ng2-toasty/style-material.css"
            ],
            "assets": [
              "src/assets"
            ],
            "fileReplacements": [
              {
                "replace": "src/environments/environment.ts",
                "with": "src/environments/environment.test.ts"
              }
            ],
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "src/tsconfig.app.json",
              "src/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    },
    "meteor-app-e2e": {
      "root": "",
      "sourceRoot": "",
      "projectType": "application",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "./protractor.conf.js",
            "devServerTarget": "meteor-app:serve"
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "e2e/tsconfig.e2e.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "meteor-app",
  "cli": {
    "warnings": {
      "typescriptMismatch": false
    }
  },
  "schematics": {
    "@schematics/angular:component": {
      "prefix": "swc",
      "styleext": "scss"
    },
    "@schematics/angular:directive": {
      "prefix": "swc"
    }
  }
}
4

1 回答 1

2

不幸的是,我认为 angular Cli 存在问题,因为创建一个新的应用程序并运行ng test --prod它是ng test --configuration=production失败的简写,我相信它必须开箱即用。

fileReplacements我已经通过在配置中使用以下内容解决了这个问题,angular.json尽管我认为这是一种解决方法而不是正确的解决方案:

 "test": {
            ...
            "fileReplacements": [
              {
                "replace": "src/environments/environment.ts",
                "with": "src/environments/environment.test.ts"
              }
            ],
          }

希望它可以帮助其他有同样问题的人。

于 2018-05-10T14:59:56.853 回答