Tuesday, August 10, 2021

Angular - What are component spec files

The spec files are unit tests for your source files. The convention for Angular applications is to have a .spec.ts file for each .ts file. They are run using the Jasmine javascript test framework through the Karma test runner (https://karma-runner.github.io/) when you use the ng test command.


Set up testing


The Angular CLI downloads and installs everything you need to test an Angular application with the Jasmine test framework.


The project you create with the CLI is immediately ready to test. Just run the ng test CLI command:

ng test


The ng test command builds the application in watch mode, and launches the Karma test runner.


The console output looks a bit like this:


10% building modules 1/1 modules 0 active

...INFO [karma]: Karma v1.7.1 server started at http://0.0.0.0:9876/

...INFO [launcher]: Launching browser Chrome ...

...INFO [launcher]: Starting browser Chrome

...INFO [Chrome ...]: Connected on socket ...

Chrome ...: Executed 3 of 3 SUCCESS (0.135 secs / 0.205 secs)


The last line of the log is the most important. It shows that Karma ran three tests that all passed.


A Chrome browser also opens and displays the test output in the "Jasmine HTML Reporter" like this.


References:

https://stackoverflow.com/questions/37502809/what-are-the-spec-ts-files-generated-by-angular-cli-for


No comments:

Post a Comment