Testing
Last updated
Last updated
The Angular CLI downloads and install everything you need to test Angular with Jasmine
testing framework.
When you run ng test
, the application is built in watch mode, and lauches the Karma
test runner.
You may generate a karma.conf.js
running ng generate config karma
, to customize karma.
You may run in CI (Continuous Integration)
with:
To generate a coverage report:
Suppose you want the code base to have a minimum of 80% code coverage.
To do so, open karma.conf.js
, and add the check
property in the coverageReport:
key
To execetute only specified choosen tests you may append a f
in front of describe
or it
.
The same way you can specify choosen tests to skip with a x
in front of describe
or it
.
DOM
queryingWhen writing tests for your Components, you will likely have to query for specific DOM
elements.
It is good practice to add to these specific tested elements a HTML property, working like ids
, so they can easily be reached inside the tests.
TestBed
It creates a dynamically constructed Angular test module that emulates an Angular @NgModule
.
Check the most important static methods here.
TestBed.configureTestingModule()
The TestBed.configureTestingModule()
method takes a metada object that can have most of the properties of an @NgModule
.
If you are running tests inside WSL2, it will have problems since it requires Chrome to execute and show the results.
Running npm run test
with --browsers=ChromeHeadless
will help, by avoiding opening a Chrome browser. But it will still require you to install Chrome inside the WSL Ubuntu.
I tried mapping the CHROME_BIN
to the external Windows Chrome executable.
But it did not work.
Check to see if google-chrome
autocompletes on the terminal.
If you just want to run the tests on the terminal just run with:
Follow here or here to install VcXsrv
on Windows, so that opening Chrome from ubuntu will show graphically on Windows.