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?
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.
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With