Solvedangular cli Can't run 'ng test' on all apps in a project and provide overrides



Angular CLI: 6.0.0
Node: 8.9.4
OS: darwin x64
Angular: 6.0.0
... animations, cli, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, platform-server, router

Package                            Version
@angular-devkit/architect          0.6.0
@angular-devkit/build-angular      0.6.0
@angular-devkit/build-optimizer    0.6.0
@angular-devkit/core               0.6.0
@angular-devkit/schematics         0.6.0
@angular/cdk                       6.0.1
@angular/material                  6.0.1
@angular/material-moment-adapter   6.0.1
@ngtools/webpack                   6.0.0
@schematics/angular                0.6.0
@schematics/update                 0.6.0
rxjs                               6.1.0
typescript                         2.7.2
webpack                            4.6.0

Repro steps

  • Have multiple apps/projects
  • Run ng test --watch=false or anything that would provide an override

Observed behavior

ng test works as long as I don't provide overrides.

Before @angular/cli@6.0.0, running ng test --watch=false would work. Now you get the following error:

$ ng test --browsers=ChromeHeadless --watch=false --progress=false --sourcemaps=false

Error: Architect commands with multiple targets cannot specify overrides.'test' would be run on the following projects: app1,app2,app3
    at TestCommand.validate (/Users/user/code/angular_app/node_modules/@angular/cli/models/architect-command.js:91:23)
    at /Users/user/code/angular_app/node_modules/@angular/cli/models/command-runner.js:274:39
    at (<anonymous>)
    at /Users/user/code/angular_app/node_modules/@angular/cli/models/command-runner.js:7:71
    at new Promise (<anonymous>)
    at __awaiter (/Users/user/code/angular_app/node_modules/@angular/cli/models/command-runner.js:3:12)
    at validateAndRunCommand (/Users/user/code/angular_app/node_modules/@angular/cli/models/command-runner.js:273:12)
    at Object.<anonymous> (/Users/user/code/angular_app/node_modules/@angular/cli/models/command-runner.js:100:26)
    at (<anonymous>)
    at fulfilled (/Users/user/code/angular_app/node_modules/@angular/cli/models/command-runner.js:4:58)

Desired behavior

I should be able to provide overrides on ng test regardless of the number of projects I'm running it on. This worked prior to @angular/cli@6.0.0.

23 Answers

✔️Accepted Answer


We also wanted to execute all test of all our projects at once in one browser. Starting and stopping the browser for each library was incredibly time-consuming.

Our solution was to create a dummy project inside the angular.json. This will execute ALL tests in one single browser. Before, our tests ran around 20 minutes with 30 libraries because the browser took so long to start and stop. Now, our tests run around 1,5 minutes for everything.

You can see what we did in my commit here: joostme/ng-test-all@6e4a00a

Other Answers:

Couldn't defaultProject from angular.json be used to select a single project if none is specified?

(Like WebStorm 2018.2 EAP still fails to run tests, due to the above error, where as the current 2018.1.3 throws "Error: The '@angular-devkit/build-angular/plugins/karma' karma plugin is meant to be used from within Angular CLI and will not work correctly outside of it." due to #10703.)

@Nxt3 I'm not sure if this is what you're asking, but for me, with 3 projects in angular.json, running ng test wil run the tests for all 3 of them. However, not simultaneously but rather sequentially: the next starts after I terminate one by hitting Ctrl+C, due to autoWatch: true in my karma.conf.js.

(But trying something like ng test --watch=false will throw the error you mentioned.)

Normally "me too" comments aren't real helpful, I'll try for an exception:

I'm seeing this same problem using the beta of @angular/cli 7.0.0, so it hasn't been accidentally fixed:

Angular CLI: 7.0.0-beta.2
Node: 10.8.0
OS: win32 x64
Angular: 7.0.0-beta.5
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                            Version
@angular-devkit/architect          0.9.0-beta.2
@angular-devkit/build-angular      0.9.0-beta.2
@angular-devkit/build-ng-packagr   0.9.0-beta.2
@angular-devkit/build-optimizer    0.9.0-beta.2
@angular-devkit/build-webpack      0.9.0-beta.2
@angular-devkit/core               0.9.0-beta.2
@angular-devkit/schematics         0.9.0-beta.2
@angular/cdk                       7.0.0-beta.0
@angular/cli                       7.0.0-beta.2
@angular/material                  7.0.0-beta.0
@ngtools/json-schema               1.1.0
@ngtools/webpack                   7.0.0-beta.2
@schematics/angular                0.9.0-beta.2
@schematics/update                 0.9.0-beta.2
ng-packagr                         4.1.1
rxjs                               6.3.2
typescript                         3.0.3
webpack                            4.17.2

The problem is exactly as described - I have a pretty spare multi-project workspace, most of the projects haven't been updated much after they were generated from the cli. And, ng test --watch=false --browsers=ChromeHeadless runs in single-run mode and uses ChromeHeadless for the first project, and uses watch mode and Chrome for all subsequent projects. So this is a semi-blocker for CI builds.

Workarounds appear to be:

  1. Run each project's tests separately (a pain for development and build configuration)
  2. Try something like nrwl nx for running tests in multiple projects

More Issues: