I mentioned StackBlitz in the post about online code editors. This service allows for creating i.a. running Angular applications. Once I wanted to execute unit tests written in Jasmine inside it, and I hit a lot of troubles. Finally, I managed to resolve them all.
Table of contents
The first part is to add support for Jasmine in StackBlitz. Please follow the series of edits.
Add two packages to your project: jasmine-core and @types/jasmine. Expand the DEPENDENCIES section in file explorer, type the name of the package in enter package name field and press the ENTER key.
/src/global-jasmine.ts file with the following content:
/src/styles.scss and add import:
Jasmine entry point
And finally, you’ll have to create a file that will be used instead of
main.ts to execute the tests suite.
/src/main-testing.ts file (aside
main.ts) and paste the following content:
Please note the line 24. You’ll have to manually put links to all unit tests you will want to execute.
Create a sample unit test
That file will be run because it was listed in stuff to test comment in
main-testing.ts. If you want to test other file(s), change the links accordingly.
And finally, to run the tests, edit
angular.json and change the line:
To run again the application, change it back.
I have created a sample application in StackBlitz: Jasmine in Angular.
Points to improve
There are some things which still require manual work:
- putting all unit tests to
angular.jsonto run the application or tests