How do I run ng test and then ng build after tests pass in Angular

Viewed 11953

I have an Angular 4 application that I'm setting up a build pipeline for. What I would like to do is have the pipeline run ng test and then once the tests pass then run ng build

Does anyone have an example of how I can do this?

4 Answers

To add to the other answers: As of Angular 6 --single-run no longer works, but you should use this instead:

--watch=false

It is described here:

Tests will execute after a build is executed via Karma, and it will automatically watch your files for changes. You can run tests a single time via --watch=false.

So now you need to do:

ng test --watch=false && ng build

We created a "test-headless" script in our package.json file:

"test-headless": "ng test --watch=false --browsers=ChromeHeadless"

Our build pipeline runs npm run test-headless to run the tests in a headless browser, then ends the test after a single run. We have a second step to run ng build --configuration production but you can also add a script in the package.json like:

"ci": npm run test-headless && ng build --configuration production then run npm run ci in your build pipeline.

Related