5

I recently upgraded my application to angular 7. I am trying to run my ng e2e tests using a specific environment. Also, I do not wish to run a new server every time I run e2e tests for the application.

When using angular 5, I used to do it the following way using CLI:

ng e2e -e test --config protractor.conf.test.js --no-serve

where my test environment was set in the environment.test.ts file and protractor configurations (baseURL and headless browser) were set in the protractor.conf.test.js file. I used the no-serve flag to stop running a new server everytime the e2e tests are run.

For angular 7, I made the following changes in the angular.json file:

{
  "$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"
    }
  }
}

I set the "test" configuration under build configurations and the browserTarget under serve configurations.

Coming to the sample-app-e2e JSON:

protractorConfig: points to the relevant protractor config file (protractor.conf.test.js).

empty devServerTarget: equivalent to the --no-serve flag.

Then I run the e2e test using the CLI as:

ng e2e --configuration=test

Now, how do I set a specific environment to use for e2e tests? I know one way it can be set is through the devServerTarget property. For e.g.:

"devServerTarget": "sample-app:serve:test"

but this will also mean that the --no-serve flag will not be honored.

Is there any way to achieve both outcomes (specific environment + no-serve)? Appreciate the help.

realIsComplex
  • 99
  • 1
  • 10

1 Answers1

0

Simply put, you have to ignore totally the angular cli. I went insane, nothing works and in my case I have an extremely large project. I have to wait several minutes each time.

Run a shell with ng serve and run another shell with protractor e2e/your-conf.js

Arthur Flower
  • 335
  • 3
  • 12