In this article we will learn how to write unit tests in for services and component in an Angular app using the Jasmine framework and karma .
Before proceeding with Angular unit test case , we should have knowledge on angular.
You can learn angular 8 from below link
https://www.youtube.com/watch?v=n6TDWpJ-qjs&list=PL5Agzt13Z4g_AVsqkZtsykXZPLcA40jVC
https://www.youtube.com/TheDotNetOffice
What is Jasmine
- Jasmine is an open-source JavaScript testing framework.
- JavaScript testing framework provides building blocks to write JavaScript unit test cases so each line of JavaScript statement is properly unit tested.
- It is used to test any type of JavaScript application.
- Jasmine is a BDD (Behavior Driven Development)
- In BDD Test are written in Non-technical language so everyone can understand it easily.
- BDD manly focus on the testing the behavior of code rather than implementation.
- By following BDD procedure, Jasmine provides a small syntax to test the smallest unit of the entire application instead of testing it as a whole.
- It does not depend on any other JavaScript frameworks.
- It does not require a DOM, And it has a clean, obvious syntax so that you can easily write tests.
- Jasmine support asynchronous testing.
- Jasmine has the test double functions called spies , A spy can stub any function and track all calls to it and its arguments
What is Karma
- Karma is a testing automation tool created by the Angular JS team.
- Karma is Open source tool.
- Karma is a tool made on top of NodeJS to run JavaScript test cases.
- This is not a testing framework like Jasmine or Mocha or Chai etc
- It only allows us to run JavaScript test cases written using testing frameworks like Jasmine.
- Karma allow us to execute the test cases on any browsers.
- Karma runs JavaScript test cases against real browsers through Command Line Interface (CLI) rather than virtual browser and DOM. Since, DOM rendering across browsers vary, so for correctness of the test report it uses real browsers.
- It can be configured what all browsers need to be used while running Karma.
- Karma can we configured with continuous integration tools like Travis, Jenkin etc.
EmoticonEmoticon