在不提供应用的情况下为angular 7中的ng e2e测试设置特定的环境

时间:2019-01-15 16:55:04

标签: angular angular7 e2e-testing angular-cli-v7

我最近将应用程序升级到了angular7。我正在尝试使用特定环境运行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标志将不被接受。

有什么方法可以同时实现这两种结果(特定环境+无服务)?感谢帮助。

1 个答案:

答案 0 :(得分:0)

简单地说,您必须完全忽略角度cli 。我疯了,什么也没做,就我而言,我有一个非常大的项目。我每次必须等待几分钟。

使用 ng serve 运行一个Shell,并使用 protractor e2e / your-conf.js

运行另一个Shell