By the command npm test. Your project will be compiled and run Karma for running all tests in your project. It will also open your browser (default address localhost:9876) and show the test results.
There are 3 main types of automated tests. There are no strict borders between them and it can be arguably hard to decide which test falls in which category.
It can be a class, or method, with mocked dependencies. We will use this type of test here.
For example, for the back-end, it can be service and repository testing together, or service and component testing for the front-end.
From front-end to back-end, that’s why it is called a (front) end to (back) end testing. In these tests usually live services using and nothing is mocked. it fully reproduces the real application work.
For automated tests on the front-end with Angular often uses built-in tools Karma and jasmine. Karma is a test runner, the tool which runs our tests in the browser. Jasmine — test tool for creating mocks and assert test results. You can use different tools for testing, such as Jest, Mocha, Chai, etc., but Jasmine already here out of the box and sufficient for most cases.
It’s important — all tests should be stored in files with name format .spec.ts. File type “spec” (stands for “specification”), tells Karma that this is the file with tests.
npm install
and then
ng test
The test is a story, it should be clear and easy to read.
Arrange — set the initial state of the system under test.
Act — call a method that you want to test.
Assert — Analyze the result and ensure that it equal to the desired result.
Jasmine has a special function, called expect.
[telegram] (https://t.me/ProDev1205)