13

如何让 Angular CLI 在 Angular 7 中提供正确的环境/环境*.ts 文件?

我尝试在三个不同的地方配置替换environment.tswith ,但没有成功。environment.test.ts

我试过用ng test和运行它ng test --configuration test

每次,如果我调试测试,我发现它使用的是environment.prod.ts(?!) 而不是environment.test.ts.

这是一个屏幕截图,显示了我尝试做的三个地方fileReplacements在此处输入图像描述 这是整个配置供参考:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "mms": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "mms",
      "schematics": {},
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/mms",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets",
              "src/web.config"
            ],
            "styles": [
              "src/styles.scss",
              "src/assets/libs/pikaday/pikaday.scss",
              "src/assets/scss/dragula/dragula.css",
              "src/assets/libs/hopscotch/css/hopscotch.css",
              "src/assets/scss/site.scss"
            ],
            "scripts": [
              "src/assets/libs/hopscotch/js/hopscotch.js",
              "src/assets/libs/pikaday/pikaday.js",
              "src/assets/libs/bitmovin-loader.js"
            ]
          },
          "configurations": {
            "test": {
              "fileReplacements": [{
                "replace": "src/environments/environment.ts",
                "with": "src/environments/environment.test.ts"
              }]
            },
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                },
                {
                  "replace": "src/index.html",
                  "with": "src/index.prod.html"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true
            },
            "mastermock": {
              "main": "src/main-mock.ts",
              "fileReplacements": [{
                "replace": "src/environments/environment.ts",
                "with": "src/environments/environment.mock.ts"
              }]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "mms:build",
            "proxyConfig": "proxy.conf.json"
          },
          "configurations": {
            "production": {
              "browserTarget": "mms:build:production"
            },
            "mastermock": {
              "browserTarget": "mms:build:mastermock",
              "proxyConfig": "proxy.conf.json"
            }
          }
        },
        "serve-local": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "mms:build",
            "proxyConfig": "proxy.local.conf.json"
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "mms:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "src/karma.conf.js",
            "styles": [
              "src/styles.scss"
            ],
            "scripts": [],
            "assets": [
              "src/favicon.ico",
              "src/assets",
              "src/web.config"
            ],
            "codeCoverageExclude": [
              "**/*.mock.ts",
              "**/*.module.ts"
            ],
            "fileReplacements": [{
              "replace": "src/environments/environment.ts",
              "with": "src/environments/environment.test.ts"
            }]
          },
          "configurations": {
            "test": {
              "fileReplacements": [{
                "replace": "src/environments/environment.ts",
                "with": "src/environments/environment.test.ts"
              }]
            },
            "production": {
              "karmaConfig": "src/karma.conf.prod.js"
            }
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "src/tsconfig.app.json",
              "src/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**",
              "**/*.dev.ts",
              "**/e2e/**"
            ]
          }
        }
      }
    },
    "mms-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.dev.js",
            "devServerTarget": "mms:serve"
          },
          "configurations": {
            "local": {
              "protractorConfig": "e2e/protractor.conf.dev.js",
              "devServerTarget": "mms:serve-local"
            }
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "mms",
  "schematics": {
    "@ngrx/schematics:component": {
      "styleext": "scss"
    }
  },
  "cli": {
    "defaultCollection": "@ngrx/schematics"
  }
}
4

3 回答 3

9
  1. 这是由 Visual Studio Code 通过导入“帮助”我引起的: import { environment } from '@environments/environment.prod';
  2. 正确的地方fileReplacements是在architect.test.options.fileReplacements
  3. 为了防止 VSCode 再次像这样“帮助”我,我将把所有文件environment.*.ts重命名为. 至少,如果我输入错误,那会冲我大喊大叫。environment.tsDO-NOT-IMPORT.ENVIRONMENT.*.ts
于 2019-01-16T21:31:06.100 回答
1

您可以将 fileReplacements 添加到 angular.json 的测试部分

"test": {
      "builder": "@angular-devkit/build-angular:karma",
      "options": {
        "main": "src/test.ts",
        "polyfills": "src/polyfills.ts",
        "tsConfig": "tsconfig.spec.json",
        "karmaConfig": "karma.conf.js",
        "fileReplacements": [{
          "replace": "src/environments/environment.ts",
          "with": "src/environments/environment.test.ts"
        }],

测试 angular-12。

于 2021-09-05T08:51:26.743 回答
0

添加自定义环境配置时,您还需要将其添加到文件中的test元素中angular.json,否则测试只会使用默认环境配置运行。

例如,假设您向现有build.configurations元素添加了一个名为“customenv”的额外环境配置:

"build": {
    ...

    "configurations": {
        "production": {
            "budgets": [
                {
                    "type": "initial",
                    "maximumWarning": "500kb",
                    "maximumError": "1mb"
                },
                {
                    "type": "anyComponentStyle",
                    "maximumWarning": "2kb",
                    "maximumError": "4kb"
                }
            ],
            "fileReplacements": [
                {
                    "replace": "src/environments/environment.ts",
                    "with": "src/environments/environment.prod.ts"
                }
            ],
            "outputHashing": "all"
        },
        "development": {
            "buildOptimizer": false,
            "optimization": false,
            "vendorChunk": true,
            "extractLicenses": false,
            "sourceMap": true,
            "namedChunks": true
        },
        "customenv": {
            "fileReplacements": [
                {
                    "replace": "src/environments/environment.ts",
                    "with": "src/environments/environment.customenv.ts"
                }
            ]
        }
    },
    "defaultConfiguration": "production"
},

此时可以运行

ng build --configuration=customenv

然后您可能还修改了已经存在的serve.configurations元素,如下所示:

"serve": {
    ...
    "configurations": {
        "production": {
            "browserTarget": "myproject:build:production"
        },
        "development": {
            "browserTarget": "myproject:build:development"
        },
        "testws": {
            "browserTarget": "myproject:build:customenv"
        }
    },
    "defaultConfiguration": "development"
},

所以现在你可以运行:

ng serve --configuration=customenv

但是如果你尝试运行

ng test --configuration=customenv

你得到错误:

发生未处理的异常:工作区中未设置配置“customenv”。

为此,您需要创建一个test.configurations元素并仅添加新的自定义环境:

"test": {
    ...                  
    "configurations": {                        
        "customenv": {
            "fileReplacements": [
                {
                    "replace": "src/environments/environment.ts",
                    "with": "src/environments/environment.customenv.ts"
                }
            ]
        }
    },
于 2021-06-03T09:48:33.570 回答