我最近将我的应用程序升级到 Angular 7。我正在尝试使用特定环境运行我的 ng e2e 测试。此外,我不希望每次为应用程序运行 e2e 测试时都运行新服务器。
使用 Angular 5 时,我曾经使用 CLI 以下列方式执行此操作:
ng e2e -e test --config protractor.conf.test.js --no-serve
在 environment.test.ts 文件中设置了我的测试环境,在 protractor.conf.test.js 文件中设置了量角器配置(baseURL 和无头浏览器)。每次运行 e2e 测试时,我都使用 no-serve 标志停止运行新服务器。
对于 Angular 7,我在 angular.json 文件中进行了以下更改:
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"sample-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",
"assets": ["src/assets", "src/favicon.ico"],
"styles": ["src/styles.css"],
"scripts": []
},
"configurations": {
"production": {
...
},
"test": {
"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.test.ts"
}
]
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "sample-app:build"
},
"configurations": {
"production": {
"browserTarget": "sample-app:build:production"
},
"test": {
"browserTarget": "sample-app:build:test"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "sample-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/styles.css"],
"assets": ["src/assets", "src/favicon.ico"]
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": ["src/tsconfig.app.json",
"src/tsconfig.spec.json"],
"exclude": ["**/node_modules/**"]
}
}
}
},
"sample-app-e2e": {
"root": "e2e",
"sourceRoot": "e2e",
"projectType": "application",
"architect": {
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "./protractor.conf.js",
"devServerTarget": "sample-app:serve"
},
"configurations": {
"test": {
"protractorConfig": "./protractor.conf.test.js",
"devServerTarget": ""
},
"production": {
"protractorConfig": "./protractor.conf.prod.js",
"devServerTarget": ""
}
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": ["e2e/tsconfig.e2e.json"],
"exclude": ["**/node_modules/**"]
}
}
}
}
},
"defaultProject": "sample-app",
"schematics": {
"@schematics/angular:component": {
"prefix": "app",
"styleext": "css"
},
"@schematics/angular:directive": {
"prefix": "app"
}
}
}
我在构建配置下设置了“测试”配置,在服务配置下设置了 browserTarget。
来到 sample-app-e2e JSON:
protractorConfig:指向相关的量角器配置文件(protractor.conf.test.js)。
空 devServerTarget:相当于 --no-serve 标志。
然后我使用 CLI 运行 e2e 测试:
ng e2e --configuration=test
现在,如何设置用于 e2e 测试的特定环境?我知道可以设置它的一种方法是通过 devServerTarget 属性。例如:
"devServerTarget": "sample-app:serve:test"
但这也意味着 --no-serve 标志将不被尊重。
有什么方法可以实现这两种结果(特定环境+不发球)?感谢帮助。