Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 933
mattwallace Avatar asked Aug 15 '17 17:08

mattwallace


People also ask

How do you test a NG build?

To test your Angular build locally: Build your app: ng build --prod. Install http-server for serving the app: npm i -g http-server. cd (change directory) into the the build location and run the app with: http-server.

What happens after ng build?

ng build command compiles the Angular app into an output directory named dist/ at the given output path. This command must be executed from within the working directory. The application builder in Angular uses the webpack build tool, with configuration options specified in the workspace configuration file (angular.

What happens when we run NG test?

You can execute the unit tests for your app via the CLI by running ng test from within the root, project directory. Upon running ng test , two things happen. First, Angular uses the Karma test runner to open up a new browser window that contains the reporting mechanism for your unit tests.


2 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
like image 138
Vojtech Ruzicka Avatar answered Oct 08 '22 04:10

Vojtech Ruzicka


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
like image 28
Mauricio De La Quintana Avatar answered Oct 08 '22 04:10

Mauricio De La Quintana