mattwallace
mattwallace

Reputation: 4222

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

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?

Upvotes: 10

Views: 17969

Answers (4)

Paul
Paul

Reputation: 478

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.

Upvotes: 1

Vojtech Ruzicka
Vojtech Ruzicka

Reputation: 17075

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

Upvotes: 31

Mauricio De La Quintana
Mauricio De La Quintana

Reputation: 2442

inside your package.json you can create a custom script.

"scripts": {
"build": "ng build",
"test": "ng test --single-run",
"ci": "npm run test && npm run build"
},

then just run

npm run ci

Upvotes: 20

salmore
salmore

Reputation: 135

ng test --single-run && ng build

Explanation:

  • Firstly make sure to run ng test as single run, otherwise it'll never terminate: ng test --single-run.
  • Then use double ampersand && to indicate that the next command should only run on successful exit status.
  • Then ng build.

This should work for both Windows and Linux systems...

Upvotes: 5

Related Questions