Update your package.json file for better tests
By default Angular gives you a couple of commands to run:
npm run start
npm run build
npm run watch
npm run test
These commands get you up and running, you can run the app in the browser, build the final version and run tests in run once.
I like to add a few more commands to my package.json
file, these extra ones are:
npm run test:watch
npm run test:coverage
npm run test:ci
npm run lint
These commands give me more options for running my tests, in watch mode, with code coverage and in both watch mode with code coverage. There is also a linting check, which is always helpful for code quality checks.
The full commands in package.json
look like this:
"test:watch": "ng test --watch",
"test:coverage": "ng test --code-coverage",
"lint": "ng lint",
"test:ci": "ng test --watch=true --browsers=ChromeHeadless --code-coverage"
Out of all four new commands the most essential one is the test:ci
one, which runs my tests in headless mode (which is great as I don’t want to have the Karma runner opening the browser all the time) gives me an overview of all the code covered by tests.
While something like NX will give you more options and use Jest, being able to run these commands in a Angular CLI based project does make running tests easier leaving no reason not to write tests.