我使用 Angular 环境变量来 configure API endpoints :
.\src\environments:
environment.ts
environment.test.ts
environment.prod.ts
环境文件包含如下设置,这些设置对于本地开发和 CI 服务器是不同的:
export const environment = {
...
apiUrl: "https://api.sample.com"
};
当我需要构建或启动应用程序时,它工作正常。我可以简单地指定环境参数:
ng serve --environment=test
...但它似乎是 impossible to set a specific environment运行 e2e 时 Protractor测试。以下命令只是忽略环境(根据 this comment 似乎是预期的)。始终使用默认环境:
ng e2e --environment=test // same as `ng e2e`
是否有任何其他方法可以使用特定环境运行测试?
最佳答案
使用新的 Angular 6 angular.json
配置文件:
我能够在我的 e2e 测试中使用我的文件 environment.test.ts
。
我必须为我的项目创建一个新架构师,我将其命名为 serve-e2e
。
"serve-e2e": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "q-desktop-v2:build:test"
}
}
我的构建:测试配置使用“fileReplacements”配置:
"test": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.test.ts"
}
]
}
然后在我的 project-e2e 中,我为 devServerTarget 使用自定义的“serve-e2e”选项:
"options": {
"protractorConfig": "./protractor.conf.js",
"devServerTarget": "q-desktop-v2:serve-e2e"
}
如果应用程序在测试环境中运行,这使得添加或忽略特定代码变得容易:
if (!environment.test) {
// do something
}
关于angular - 如何使用不同的 Angular 环境变量运行 Protractor e2e 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45330680/