How do I run ng test and then ng build after tests

2019-05-25 22:00发布

问题:

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?

回答1:

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


回答2:

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


回答3:

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...