SlideShare a Scribd company logo
6
Most read
8
Most read
12
Most read
Presented By:
Kiran Jeet Kaur & Devanshu Kumar Verma
Unit Testing in Angular
Lack of etiquette and manners is a huge turn off.
KnolX Etiquettes
Punctuality
Respect Knolx session timings, you
are requested not to join sessions
after a 5 minutes threshold post
the session start time.
Feedback
Make sure to submit a constructive
feedback for all sessions as it is
very helpful for the presenter.
Silent Mode
Keep your mobile devices in silent
mode, feel free to move out of
session in case you need to attend
an urgent call.
Avoid Disturbance
Avoid unwanted chit chat during
the session.
Our Agenda
01 Unit Testing & its importance
02 Angular CLI setup
03 Angular testing tools
04 Code Coverage
05 Demo
Test Driven Development
● Test-driven development is the act of first deciding what you
want your program to do (the specifications), formulating a
failing test, then writing the code to make that test pass. It is
most often associated with automated testing. Although you
could apply the principles to manual testing as well.
● Let’s look at a simple example: Building a wooden table.
Traditionally, we would make a table, then once the table is
made, test it to make sure it does, well, what a table should
do. TDD, on the other hand, would have us first define what
the table should do.
Test Driven Development..
I expect the table to be four feet in diameter.
The test fails because I have no table.
I cut a circular piece of wood four feet in diameter.
The test passes.
__________
I expect the table to be three feet high.
The test fails because it is sitting on the ground.
I add one leg in the middle of the table.
The test passes.
What is Unit Testing?
● Unit testing is testing a unit in an isolated environment. A unit can be a class, component,
service, directive module, etc. which can be logically separated from the software. Any unit in an
app is not isolated, it’s quite normal that it will be depending on the other units in an application
for resources like data or methods..
● A single unit is any block of code (i.e. function or class) that has one and only one responsibility.
● A function might have multiple unit tests according to the uses and output of the function.
● So if we do an integrated test of the application and it fails then it’s hard to identify where
exactly the code is breaking. So the purpose of unit testing is to test each unit individually and
see if it’s working fine.
Benefits and Objectives of
Unit Testing
Prerequisites
● Node Js 6.9 +.
● Angular cli
● A angular project - create a new project using
angular CLI which will install everything we
need to have to test the application.
ng new unit-test-demo
In the package.json you can find the
following:
Unit testing in Angular
● Jasmine-core:
Jasmine is a behavior-driven development framework for testing JavaScript code. It does not
depend on any other JavaScript frameworks. It does not require a DOM.
● Karma:
Karma is essentially a tool that spawns a web server that executes source code against test
code for each of the browsers connected. The results of each test against each browser are
examined and displayed via the command line to the developer such that they can see which
browsers and tests passed or failed.
Jasmine And Karma
Angular Testing Tools
Test Spec File
● This is the very spec file that is created
with our app i.e., app.component.spec.ts
● There are 4 majorly used keywords that
we will be using throughout our journey
of test cases in angular -
○ describe()
○ beforeEach()
○ TestBed()
○ it()
Jasmine Test Spec
Setup and teardown(Cleaning up)
●
● Testbed: TestBed.configureTestingModule() method takes a metadata object that can have most of the
properties of an @NgModule. A spec file does not know any of the custom components, classes,
imports you have mentioned in the ts file or module, everything needs to be mentioned and imported in
the spec file for creating the environment to run the tests.
● TestBed.createComponent(): Creates an instance of the AppComponent, adds a corresponding
element to the test-runner DOM, and returns a ComponentFixture.
● ComponentFixture: This is a test harness for interacting with the created component and its
corresponding element.
● detectChange() : In the actual production, data binding is done automatically but in
TestBed.createComponent() does not do data binding unless we trigger by detectChange().
● nativeElement() : nativeElement() will be HTML element, and we can access its child and dive into its
DOM tree using querySelectors().
Important Points About Jasmine
● describe() blocks define a test suite and each it block is for an individual test.
● beforeEach() runs before each test and is used for the setup part of a test.
● afterEach() runs after each test and is used for the teardown part of a test.
● You can also use beforeAll and afterAll, and these run once before or after all tests.
● You test an assertion in Jasmine with expect and using a matcher like toBeDefined,
toBeTruthy, toContain, toEqual, toThrow, toBeNull, … For example:
expect(myValue).toBeGreaterThan(3);
● You can do negative assertion with not: expect(myValue).not.toBeGreaterThan(3);
TESTING A COMPONENT
Code Coverage
● The CLI can run unit tests and create code coverage reports. Code coverage reports show you
any parts of your code base that might not be properly tested by your unit tests.
● To generate a coverage report run the following command in the root of your project.
ng test --no-watch --code-coverage
● When the tests are complete, the command creates a new /coverage folder in the project. Open
the index.html file to see a report with your source code and code coverage values.
● If you want to create code-coverage reports every time you test, set the following option in the
CLI configuration file, angular.json:
"test": {
"options": {
"codeCoverage": true
}
}
●
DEMO
References
● https://angular.io/guide/testing-code-coverage
● https://www.digitalocean.com/community/tutorials/angular-introduction-unit-te
sting
● https://medium.com/swlh/angular-unit-testing-jasmine-karma-step-by-step-e33
76d110ab4
● https://blog.logrocket.com/angular-unit-testing-tutorial-examples/
Thank You !
Get in touch with us:
Lorem Studio, Lord Building
D4456, LA, USA

More Related Content

PPTX
Angular Unit Testing
PPT
05 junit
PDF
JUnit & Mockito, first steps
PPTX
Unit Testing And Mocking
PPTX
Understanding Unit Testing
PPTX
Java Unit Testing
PPTX
JUnit- A Unit Testing Framework
PDF
Unit testing best practices
Angular Unit Testing
05 junit
JUnit & Mockito, first steps
Unit Testing And Mocking
Understanding Unit Testing
Java Unit Testing
JUnit- A Unit Testing Framework
Unit testing best practices

What's hot (20)

PPTX
Angular Unit Testing
PPTX
Automation - web testing with selenium
PDF
Testing Angular
PPTX
API Automation Testing Using RestAssured+Cucumber
PPSX
API Test Automation
PPSX
PPTX
Introduction to Selenium Web Driver
PPTX
Unit Testing Concepts and Best Practices
PPTX
Unit testing in JavaScript with Jasmine and Karma
PDF
4 Major Advantages of API Testing
PDF
TestNG Annotations in Selenium | Edureka
PPTX
Unit tests & TDD
PPT
Postman.ppt
PPTX
TypeScript Overview
PPT
Selenium
PDF
Java 17
PPS
JUnit Presentation
PDF
Automation Testing using Selenium
PPTX
Api testing
PDF
Postman: An Introduction for Testers
Angular Unit Testing
Automation - web testing with selenium
Testing Angular
API Automation Testing Using RestAssured+Cucumber
API Test Automation
Introduction to Selenium Web Driver
Unit Testing Concepts and Best Practices
Unit testing in JavaScript with Jasmine and Karma
4 Major Advantages of API Testing
TestNG Annotations in Selenium | Edureka
Unit tests & TDD
Postman.ppt
TypeScript Overview
Selenium
Java 17
JUnit Presentation
Automation Testing using Selenium
Api testing
Postman: An Introduction for Testers
Ad

Similar to Unit Testing in Angular (20)

PPTX
introduction to Unit testing in Angular.pptx
PPTX
Angular Unit testing.pptx
PDF
Angular Testing
PDF
Angular Application Testing
PDF
Testing Angular apps_ A complete guide for developers.pdf
PDF
An Introduction To Testing In AngularJS Applications
PPT
Testing in AngularJS
PDF
JavaScript Unit Testing with an Angular 5.x Use Case 101
ODP
Unit Testing and Coverage for AngularJS
PPTX
Angular Unit Test
PDF
AngularJS Unit Testing w/Karma and Jasmine
PDF
Angularjs - Unit testing introduction
PDF
Unit-testing and E2E testing in JS
PDF
Angularjs Test Driven Development (TDD)
PDF
Front-end Testing (manual, automated, you name it) - Erich Jagomägis - Develo...
PDF
Intro to Unit Testing in AngularJS
PDF
Никита Галкин "Testing in Frontend World"
PPTX
Unit testing of java script and angularjs application using Karma Jasmine Fra...
PPTX
Angular Unit Testing
PDF
Javascript tdd byandreapaciolla
introduction to Unit testing in Angular.pptx
Angular Unit testing.pptx
Angular Testing
Angular Application Testing
Testing Angular apps_ A complete guide for developers.pdf
An Introduction To Testing In AngularJS Applications
Testing in AngularJS
JavaScript Unit Testing with an Angular 5.x Use Case 101
Unit Testing and Coverage for AngularJS
Angular Unit Test
AngularJS Unit Testing w/Karma and Jasmine
Angularjs - Unit testing introduction
Unit-testing and E2E testing in JS
Angularjs Test Driven Development (TDD)
Front-end Testing (manual, automated, you name it) - Erich Jagomägis - Develo...
Intro to Unit Testing in AngularJS
Никита Галкин "Testing in Frontend World"
Unit testing of java script and angularjs application using Karma Jasmine Fra...
Angular Unit Testing
Javascript tdd byandreapaciolla
Ad

More from Knoldus Inc. (20)

PPTX
Angular Hydration Presentation (FrontEnd)
PPTX
Optimizing Test Execution: Heuristic Algorithm for Self-Healing
PPTX
Self-Healing Test Automation Framework - Healenium
PPTX
Kanban Metrics Presentation (Project Management)
PPTX
Java 17 features and implementation.pptx
PPTX
Chaos Mesh Introducing Chaos in Kubernetes
PPTX
GraalVM - A Step Ahead of JVM Presentation
PPTX
Nomad by HashiCorp Presentation (DevOps)
PPTX
Nomad by HashiCorp Presentation (DevOps)
PPTX
DAPR - Distributed Application Runtime Presentation
PPTX
Introduction to Azure Virtual WAN Presentation
PPTX
Introduction to Argo Rollouts Presentation
PPTX
Intro to Azure Container App Presentation
PPTX
Insights Unveiled Test Reporting and Observability Excellence
PPTX
Introduction to Splunk Presentation (DevOps)
PPTX
Code Camp - Data Profiling and Quality Analysis Framework
PPTX
AWS: Messaging Services in AWS Presentation
PPTX
Amazon Cognito: A Primer on Authentication and Authorization
PPTX
ZIO Http A Functional Approach to Scalable and Type-Safe Web Development
PPTX
Managing State & HTTP Requests In Ionic.
Angular Hydration Presentation (FrontEnd)
Optimizing Test Execution: Heuristic Algorithm for Self-Healing
Self-Healing Test Automation Framework - Healenium
Kanban Metrics Presentation (Project Management)
Java 17 features and implementation.pptx
Chaos Mesh Introducing Chaos in Kubernetes
GraalVM - A Step Ahead of JVM Presentation
Nomad by HashiCorp Presentation (DevOps)
Nomad by HashiCorp Presentation (DevOps)
DAPR - Distributed Application Runtime Presentation
Introduction to Azure Virtual WAN Presentation
Introduction to Argo Rollouts Presentation
Intro to Azure Container App Presentation
Insights Unveiled Test Reporting and Observability Excellence
Introduction to Splunk Presentation (DevOps)
Code Camp - Data Profiling and Quality Analysis Framework
AWS: Messaging Services in AWS Presentation
Amazon Cognito: A Primer on Authentication and Authorization
ZIO Http A Functional Approach to Scalable and Type-Safe Web Development
Managing State & HTTP Requests In Ionic.

Recently uploaded (20)

PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
cuic standard and advanced reporting.pdf
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Electronic commerce courselecture one. Pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Spectral efficient network and resource selection model in 5G networks
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
KodekX | Application Modernization Development
20250228 LYD VKU AI Blended-Learning.pptx
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Digital-Transformation-Roadmap-for-Companies.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Encapsulation_ Review paper, used for researhc scholars
cuic standard and advanced reporting.pdf
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Per capita expenditure prediction using model stacking based on satellite ima...
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Electronic commerce courselecture one. Pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
MYSQL Presentation for SQL database connectivity
Spectral efficient network and resource selection model in 5G networks
“AI and Expert System Decision Support & Business Intelligence Systems”
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
KodekX | Application Modernization Development

Unit Testing in Angular

  • 1. Presented By: Kiran Jeet Kaur & Devanshu Kumar Verma Unit Testing in Angular
  • 2. Lack of etiquette and manners is a huge turn off. KnolX Etiquettes Punctuality Respect Knolx session timings, you are requested not to join sessions after a 5 minutes threshold post the session start time. Feedback Make sure to submit a constructive feedback for all sessions as it is very helpful for the presenter. Silent Mode Keep your mobile devices in silent mode, feel free to move out of session in case you need to attend an urgent call. Avoid Disturbance Avoid unwanted chit chat during the session.
  • 3. Our Agenda 01 Unit Testing & its importance 02 Angular CLI setup 03 Angular testing tools 04 Code Coverage 05 Demo
  • 4. Test Driven Development ● Test-driven development is the act of first deciding what you want your program to do (the specifications), formulating a failing test, then writing the code to make that test pass. It is most often associated with automated testing. Although you could apply the principles to manual testing as well. ● Let’s look at a simple example: Building a wooden table. Traditionally, we would make a table, then once the table is made, test it to make sure it does, well, what a table should do. TDD, on the other hand, would have us first define what the table should do.
  • 5. Test Driven Development.. I expect the table to be four feet in diameter. The test fails because I have no table. I cut a circular piece of wood four feet in diameter. The test passes. __________ I expect the table to be three feet high. The test fails because it is sitting on the ground. I add one leg in the middle of the table. The test passes.
  • 6. What is Unit Testing? ● Unit testing is testing a unit in an isolated environment. A unit can be a class, component, service, directive module, etc. which can be logically separated from the software. Any unit in an app is not isolated, it’s quite normal that it will be depending on the other units in an application for resources like data or methods.. ● A single unit is any block of code (i.e. function or class) that has one and only one responsibility. ● A function might have multiple unit tests according to the uses and output of the function. ● So if we do an integrated test of the application and it fails then it’s hard to identify where exactly the code is breaking. So the purpose of unit testing is to test each unit individually and see if it’s working fine.
  • 7. Benefits and Objectives of Unit Testing
  • 8. Prerequisites ● Node Js 6.9 +. ● Angular cli ● A angular project - create a new project using angular CLI which will install everything we need to have to test the application. ng new unit-test-demo In the package.json you can find the following: Unit testing in Angular
  • 9. ● Jasmine-core: Jasmine is a behavior-driven development framework for testing JavaScript code. It does not depend on any other JavaScript frameworks. It does not require a DOM. ● Karma: Karma is essentially a tool that spawns a web server that executes source code against test code for each of the browsers connected. The results of each test against each browser are examined and displayed via the command line to the developer such that they can see which browsers and tests passed or failed. Jasmine And Karma
  • 11. Test Spec File ● This is the very spec file that is created with our app i.e., app.component.spec.ts ● There are 4 majorly used keywords that we will be using throughout our journey of test cases in angular - ○ describe() ○ beforeEach() ○ TestBed() ○ it()
  • 14. ● Testbed: TestBed.configureTestingModule() method takes a metadata object that can have most of the properties of an @NgModule. A spec file does not know any of the custom components, classes, imports you have mentioned in the ts file or module, everything needs to be mentioned and imported in the spec file for creating the environment to run the tests. ● TestBed.createComponent(): Creates an instance of the AppComponent, adds a corresponding element to the test-runner DOM, and returns a ComponentFixture. ● ComponentFixture: This is a test harness for interacting with the created component and its corresponding element. ● detectChange() : In the actual production, data binding is done automatically but in TestBed.createComponent() does not do data binding unless we trigger by detectChange(). ● nativeElement() : nativeElement() will be HTML element, and we can access its child and dive into its DOM tree using querySelectors().
  • 15. Important Points About Jasmine ● describe() blocks define a test suite and each it block is for an individual test. ● beforeEach() runs before each test and is used for the setup part of a test. ● afterEach() runs after each test and is used for the teardown part of a test. ● You can also use beforeAll and afterAll, and these run once before or after all tests. ● You test an assertion in Jasmine with expect and using a matcher like toBeDefined, toBeTruthy, toContain, toEqual, toThrow, toBeNull, … For example: expect(myValue).toBeGreaterThan(3); ● You can do negative assertion with not: expect(myValue).not.toBeGreaterThan(3);
  • 17. Code Coverage ● The CLI can run unit tests and create code coverage reports. Code coverage reports show you any parts of your code base that might not be properly tested by your unit tests. ● To generate a coverage report run the following command in the root of your project. ng test --no-watch --code-coverage ● When the tests are complete, the command creates a new /coverage folder in the project. Open the index.html file to see a report with your source code and code coverage values. ● If you want to create code-coverage reports every time you test, set the following option in the CLI configuration file, angular.json: "test": { "options": { "codeCoverage": true } } ●
  • 18. DEMO
  • 19. References ● https://angular.io/guide/testing-code-coverage ● https://www.digitalocean.com/community/tutorials/angular-introduction-unit-te sting ● https://medium.com/swlh/angular-unit-testing-jasmine-karma-step-by-step-e33 76d110ab4 ● https://blog.logrocket.com/angular-unit-testing-tutorial-examples/
  • 20. Thank You ! Get in touch with us: Lorem Studio, Lord Building D4456, LA, USA