SlideShare a Scribd company logo
Unit Testing and Coverage for AngularJS
Sumit Khanduri
Software Consultant
Knoldus Software LLP
Introduction
● Unit testing, as the name implies, is about testing
individual units of code.
● "Did I think about the logic correctly?" or "Does the sort
function order the list in the right order?"
● The primary goal of unit testing is to take the smallest
piece of testable software in the application.
Testing AngularJS App`s
● AngularJS build around the concept of dependency injection & loose
coupling to the DOM which make it testable.
● Angular comes with dependency injection built-in, which makes testing
components much easier, because you can pass in a component's
dependencies and mock them as you wish.
● Components that have their dependencies injected allow them to be
easily mocked on a test by test basis, without having to mess with any
global variables that could accidentally affect another test.
TDD
● Karma is a JavaScript command line tool that can be
used to spawn a web server which loads your
application's source code and executes your tests.
● You can configure Karma to run against a number of
browsers.
● Karma is a NodeJS application, and should be
installed through npm.
The main goal for Karma is to bring a productive testing environment to
developers. The environment being one where they don't have to set up
loads of configurations, but rather a place where developers can just
write the code and get instant feedback from their tests. Because getting
quick feedback is what makes you productive and creative.
http://karma-runner.github.io/
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. And it has a clean, obvious
syntax so that you can easily write tests.
http://jasmine.github.io/
Unit Test Strucuture - Specs
it('should increment a variable', function () {
var foo = 0; // set up the world
foo++; // call your application code
expect(foo).toEqual(1); // passes because foo == 1
});
Unit Test Structure – Suites
Collections of specs
Your test files will contain one or more suites, each with one or
more specs.
describe("User Validation", function() {
it("requires a name", function() {
//spec content
})
it("does not validate invalid names", function() {
//spec content
})
});
Unit Test Structure – expect()
expect() is the actual 'test'
Takes a value, then 'matchers'
it('should increment a variable', function () {
var foo = 0; // set up the world
foo++; // call your application code
expect(foo).toEqual(1); // passes because foo == 1
});
Mocking in Jasmine
A test is not a unit test if:
1. It talks to the database
2. It communicates across the network
3. It touches the file system
4. It can't run correctly at the same time as any of your other unit
tests
5. You have to do special things to your environment (such as
editing config files) to run it.
~ Michael Feathers
Need to use mocks to simulate calls to external services, files, etc
Need to use mocks to reduce real dependencies in tests.
Mock HTTP ($httpBackend)
● Fake HTTP backend implementation suitable for unit testing
applications that use the $http service.
● Example:
mockHttpBackend.expect('POST',config.serverURL+
'admin/login',{'email':'','password':''}).respond({ data : {}
});
Matchers
Mocking in Jasmine – Spies
● Easily inject mock/monitoring objects
● Replaces or wraps the function it's spying on
● Gives run-time statistics on the spied function
● Know how many times a function has been called
● Inspect return values
● Inspect parameters
it('should run Successfully', function() {
spyOn(mockState, 'go');
mockScope.addEmployee();
expect(mockState.go).toHaveBeenCalledWith('home');
});
Spies
Istanbul
Yet another JS code coverage tool that computes
statement, line, function and branch coverage with
module loader hooks to transparently add coverage
when running tests. Supports all JS coverage use
cases including unit tests, server side functional tests
https://www.npmjs.com/package/istanbul
Istanbul-Setup
● Install karma-coverage
> sudo npm install -g karma-coverage
● Install istanbul
> sudo npm install istanbul
● Karma.conf.js
> Inside preprocessors json object add the path to your file for which you need coverage
report. E.g., preprocessors: 'app/component/add/add.ctrl.js':'coverage',
> Inside reporters add a string named 'coverage', E.g., reporters: ['progress','coverage',].
Istanbul Report
Setup Test Environment
● Install Karma
> npm install -g karma –save-dev
● Install Jasmine
> npm install karma-jasmine jasmine-core –save-dev
● Install ngMock
> npm install angular-mocks –save-dev
● Browser
> npm install karma-chrome-launcher –save-dev
● Create two folders in your working directory
> mkdir app
> mkdir tests
Contd..
● Karma.conf.js
> karma init
> Select jasmine as your testing framework
> Select browser, whichever you like
> Specify the paths to your spec files. E.g., 'app/*.js', 'test/*.js'.
> Add the location of angularjs in your karma.conf.js.
> Add the location of ngMock library in your karma.conf.js
●
Optional commands
> npm install karma --save-dev
> npm install karma-jasmine karma-chrome-launcher –save-dev
> npm install -g karma-cli
> karma start
Questions ?
References
● http://www.slideshare.net/timoxley/testing-with-jasmine?next_slideshow=1
● http://www.slideshare.net/nirkaufman/angularjs-unit-testing
● http://java.ociweb.com/mark/other-presentations/AngularJS-Testing.pdf
● https://www.youtube.com/watch?v=f7lIBiLmISQ
Ad

Recommended

Phân tích báo cáo tài chính tại công ty cổ phần sản xuất kính an toàn
Phân tích báo cáo tài chính tại công ty cổ phần sản xuất kính an toàn
https://www.facebook.com/garmentspace
 
Luận văn kiểm toán: VẬN DỤNG THỦ TỤC PHÂN TÍCH TRONG KIỂM TOÁN BÁO CÁO TÀI CH...
Luận văn kiểm toán: VẬN DỤNG THỦ TỤC PHÂN TÍCH TRONG KIỂM TOÁN BÁO CÁO TÀI CH...
Nguyễn Công Huy
 
Phân tích tình hình tài chính tại doanh nghiệp tư nhân gương kính cường hương
Phân tích tình hình tài chính tại doanh nghiệp tư nhân gương kính cường hương
https://www.facebook.com/garmentspace
 
Phân tích tình hình tài chính công ty cổ phần chứng khoán phượng hoàng
Phân tích tình hình tài chính công ty cổ phần chứng khoán phượng hoàng
https://www.facebook.com/garmentspace
 
Phân tích tình hình tài chính tại công ty cổ phần kết cấu thép cơ khí
Phân tích tình hình tài chính tại công ty cổ phần kết cấu thép cơ khí
https://www.facebook.com/garmentspace
 
Đề tài: Quản lý và sử dụng nhân sự tại Công ty thương mại Hợp Lực
Đề tài: Quản lý và sử dụng nhân sự tại Công ty thương mại Hợp Lực
Dịch vụ viết bài trọn gói ZALO 0917193864
 
đồ áN tốt nghiệp phân tích công tác quản trị nguồn nhân lực tại công ty xây...
đồ áN tốt nghiệp phân tích công tác quản trị nguồn nhân lực tại công ty xây...
https://www.facebook.com/garmentspace
 
Đề tài: Phát triển nguồn nhân lực tại công ty Quảng Thành, HAY
Đề tài: Phát triển nguồn nhân lực tại công ty Quảng Thành, HAY
Dịch vụ viết bài trọn gói ZALO 0917193864
 
Giải pháp nâng cao lợi nhuận tại công ty cổ phần thép và vật tư công nghiệp
Giải pháp nâng cao lợi nhuận tại công ty cổ phần thép và vật tư công nghiệp
https://www.facebook.com/garmentspace
 
Phân tích báo cáo tài chính tại công ty thương mại Hữu Nghị, 9đ
Phân tích báo cáo tài chính tại công ty thương mại Hữu Nghị, 9đ
Dịch Vụ Viết Bài Trọn Gói ZALO 0917193864
 
Đề tài: Hoàn thiện tổ chức công tác tiền lương công ty Đông Phong, 9 ĐIỂM!
Đề tài: Hoàn thiện tổ chức công tác tiền lương công ty Đông Phong, 9 ĐIỂM!
Viết thuê trọn gói ZALO 0934573149
 
Kế toán chi phí và tính giá thành tại Công ty sản xuất và thương mại
Kế toán chi phí và tính giá thành tại Công ty sản xuất và thương mại
Dịch vụ viết bài trọn gói ZALO 0917193864
 
Đề tài phát triển nguồn nhân lực công ty Việt Thắng
Đề tài phát triển nguồn nhân lực công ty Việt Thắng
Dịch Vụ Viết Bài Trọn Gói ZALO 0917193864
 
Phân tích tình hình tài chính công ty thép đan việt sdt/ ZALO 09345 497 28
Phân tích tình hình tài chính công ty thép đan việt sdt/ ZALO 09345 497 28
Thư viện Tài liệu mẫu
 
Chi phí sản xuất, giá thành sản phẩm nước mắm ông sao, HAY
Chi phí sản xuất, giá thành sản phẩm nước mắm ông sao, HAY
Dịch vụ viết bài trọn gói ZALO 0917193864
 
Đề tài: Phân tích hiệu quả sử dụng vốn của công ty xây dựng, 9đ
Đề tài: Phân tích hiệu quả sử dụng vốn của công ty xây dựng, 9đ
Dịch vụ viết bài trọn gói ZALO 0917193864
 
Đề tài hoàn thiện hoạt động tuyển dụng nhân lực, 2018, HAY
Đề tài hoàn thiện hoạt động tuyển dụng nhân lực, 2018, HAY
Dịch Vụ Viết Bài Trọn Gói ZALO 0917193864
 
Đề tài: Phân tích tình hình tài chính tại Công ty thương mại vận tải
Đề tài: Phân tích tình hình tài chính tại Công ty thương mại vận tải
Dịch vụ viết bài trọn gói ZALO: 0909232620
 
Đề tài: Quản lý, sử dụng nhân sự tại Công ty sản xuất cọc bê tông
Đề tài: Quản lý, sử dụng nhân sự tại Công ty sản xuất cọc bê tông
Dịch vụ viết bài trọn gói ZALO 0917193864
 
Phân tích hiệu quả kinh doanh của công ty trách nhiệm hữu hạn một thành viên ...
Phân tích hiệu quả kinh doanh của công ty trách nhiệm hữu hạn một thành viên ...
https://www.facebook.com/garmentspace
 
Đề Tài Khóa luận 2024 Tìm hiểu quy trình lập và phát hành Báo cáo kiểm toán v...
Đề Tài Khóa luận 2024 Tìm hiểu quy trình lập và phát hành Báo cáo kiểm toán v...
lamluanvan.net Viết thuê luận văn
 
NÂNG CAO HIỆU QUẢ SỬ DỤNG VỐN LƯU ĐỘNG TẠI CÔNG TY CỔ PHẦN MAY ĐỨC GIANG
NÂNG CAO HIỆU QUẢ SỬ DỤNG VỐN LƯU ĐỘNG TẠI CÔNG TY CỔ PHẦN MAY ĐỨC GIANG
Nguyễn Công Huy
 
Đề tài: Tư vấn tài chính doanh nghiệp tại công ty Chứng khoán, 9đ - Gửi miễn ...
Đề tài: Tư vấn tài chính doanh nghiệp tại công ty Chứng khoán, 9đ - Gửi miễn ...
Dịch vụ viết bài trọn gói ZALO: 0909232620
 
Kế toán doanh thu và xác định kết quả kinh doanh ở công ty cổ phần dệt may huế
Kế toán doanh thu và xác định kết quả kinh doanh ở công ty cổ phần dệt may huế
https://www.facebook.com/garmentspace
 
Giải pháp chủ yếu nhằm nâng cao hiệu quả hoạt động sản xuất kinh doanh tại cô...
Giải pháp chủ yếu nhằm nâng cao hiệu quả hoạt động sản xuất kinh doanh tại cô...
https://www.facebook.com/garmentspace
 
Các Nhân Tố Ảnh Hưởng Đến Hành Vi Quản Trị Lợi Nhuận Trên Báo Cáo Tài Chính
Các Nhân Tố Ảnh Hưởng Đến Hành Vi Quản Trị Lợi Nhuận Trên Báo Cáo Tài Chính
Hỗ Trợ Viết Đề Tài luanvanpanda.com
 
Kế toán tiền lương và khoản trích theo lương tại Công ty An Minh, 9đ
Kế toán tiền lương và khoản trích theo lương tại Công ty An Minh, 9đ
Dịch Vụ Viết Bài Trọn Gói ZALO 0917193864
 
Nâng Cao Chất Lượng Nguồn Nhân Lực Tại Công Ty Tnhh Mtv Khai Thác Công Trình ...
Nâng Cao Chất Lượng Nguồn Nhân Lực Tại Công Ty Tnhh Mtv Khai Thác Công Trình ...
Dịch vụ viết thuê Luận Văn - ZALO 0932091562
 
Angularjs - Unit testing introduction
Angularjs - Unit testing introduction
Nir Kaufman
 
Hybrid application development
Hybrid application development
Knoldus Inc.
 

More Related Content

What's hot (20)

Giải pháp nâng cao lợi nhuận tại công ty cổ phần thép và vật tư công nghiệp
Giải pháp nâng cao lợi nhuận tại công ty cổ phần thép và vật tư công nghiệp
https://www.facebook.com/garmentspace
 
Phân tích báo cáo tài chính tại công ty thương mại Hữu Nghị, 9đ
Phân tích báo cáo tài chính tại công ty thương mại Hữu Nghị, 9đ
Dịch Vụ Viết Bài Trọn Gói ZALO 0917193864
 
Đề tài: Hoàn thiện tổ chức công tác tiền lương công ty Đông Phong, 9 ĐIỂM!
Đề tài: Hoàn thiện tổ chức công tác tiền lương công ty Đông Phong, 9 ĐIỂM!
Viết thuê trọn gói ZALO 0934573149
 
Kế toán chi phí và tính giá thành tại Công ty sản xuất và thương mại
Kế toán chi phí và tính giá thành tại Công ty sản xuất và thương mại
Dịch vụ viết bài trọn gói ZALO 0917193864
 
Đề tài phát triển nguồn nhân lực công ty Việt Thắng
Đề tài phát triển nguồn nhân lực công ty Việt Thắng
Dịch Vụ Viết Bài Trọn Gói ZALO 0917193864
 
Phân tích tình hình tài chính công ty thép đan việt sdt/ ZALO 09345 497 28
Phân tích tình hình tài chính công ty thép đan việt sdt/ ZALO 09345 497 28
Thư viện Tài liệu mẫu
 
Chi phí sản xuất, giá thành sản phẩm nước mắm ông sao, HAY
Chi phí sản xuất, giá thành sản phẩm nước mắm ông sao, HAY
Dịch vụ viết bài trọn gói ZALO 0917193864
 
Đề tài: Phân tích hiệu quả sử dụng vốn của công ty xây dựng, 9đ
Đề tài: Phân tích hiệu quả sử dụng vốn của công ty xây dựng, 9đ
Dịch vụ viết bài trọn gói ZALO 0917193864
 
Đề tài hoàn thiện hoạt động tuyển dụng nhân lực, 2018, HAY
Đề tài hoàn thiện hoạt động tuyển dụng nhân lực, 2018, HAY
Dịch Vụ Viết Bài Trọn Gói ZALO 0917193864
 
Đề tài: Phân tích tình hình tài chính tại Công ty thương mại vận tải
Đề tài: Phân tích tình hình tài chính tại Công ty thương mại vận tải
Dịch vụ viết bài trọn gói ZALO: 0909232620
 
Đề tài: Quản lý, sử dụng nhân sự tại Công ty sản xuất cọc bê tông
Đề tài: Quản lý, sử dụng nhân sự tại Công ty sản xuất cọc bê tông
Dịch vụ viết bài trọn gói ZALO 0917193864
 
Phân tích hiệu quả kinh doanh của công ty trách nhiệm hữu hạn một thành viên ...
Phân tích hiệu quả kinh doanh của công ty trách nhiệm hữu hạn một thành viên ...
https://www.facebook.com/garmentspace
 
Đề Tài Khóa luận 2024 Tìm hiểu quy trình lập và phát hành Báo cáo kiểm toán v...
Đề Tài Khóa luận 2024 Tìm hiểu quy trình lập và phát hành Báo cáo kiểm toán v...
lamluanvan.net Viết thuê luận văn
 
NÂNG CAO HIỆU QUẢ SỬ DỤNG VỐN LƯU ĐỘNG TẠI CÔNG TY CỔ PHẦN MAY ĐỨC GIANG
NÂNG CAO HIỆU QUẢ SỬ DỤNG VỐN LƯU ĐỘNG TẠI CÔNG TY CỔ PHẦN MAY ĐỨC GIANG
Nguyễn Công Huy
 
Đề tài: Tư vấn tài chính doanh nghiệp tại công ty Chứng khoán, 9đ - Gửi miễn ...
Đề tài: Tư vấn tài chính doanh nghiệp tại công ty Chứng khoán, 9đ - Gửi miễn ...
Dịch vụ viết bài trọn gói ZALO: 0909232620
 
Kế toán doanh thu và xác định kết quả kinh doanh ở công ty cổ phần dệt may huế
Kế toán doanh thu và xác định kết quả kinh doanh ở công ty cổ phần dệt may huế
https://www.facebook.com/garmentspace
 
Giải pháp chủ yếu nhằm nâng cao hiệu quả hoạt động sản xuất kinh doanh tại cô...
Giải pháp chủ yếu nhằm nâng cao hiệu quả hoạt động sản xuất kinh doanh tại cô...
https://www.facebook.com/garmentspace
 
Các Nhân Tố Ảnh Hưởng Đến Hành Vi Quản Trị Lợi Nhuận Trên Báo Cáo Tài Chính
Các Nhân Tố Ảnh Hưởng Đến Hành Vi Quản Trị Lợi Nhuận Trên Báo Cáo Tài Chính
Hỗ Trợ Viết Đề Tài luanvanpanda.com
 
Kế toán tiền lương và khoản trích theo lương tại Công ty An Minh, 9đ
Kế toán tiền lương và khoản trích theo lương tại Công ty An Minh, 9đ
Dịch Vụ Viết Bài Trọn Gói ZALO 0917193864
 
Nâng Cao Chất Lượng Nguồn Nhân Lực Tại Công Ty Tnhh Mtv Khai Thác Công Trình ...
Nâng Cao Chất Lượng Nguồn Nhân Lực Tại Công Ty Tnhh Mtv Khai Thác Công Trình ...
Dịch vụ viết thuê Luận Văn - ZALO 0932091562
 
Đề tài: Hoàn thiện tổ chức công tác tiền lương công ty Đông Phong, 9 ĐIỂM!
Đề tài: Hoàn thiện tổ chức công tác tiền lương công ty Đông Phong, 9 ĐIỂM!
Viết thuê trọn gói ZALO 0934573149
 
Phân tích tình hình tài chính công ty thép đan việt sdt/ ZALO 09345 497 28
Phân tích tình hình tài chính công ty thép đan việt sdt/ ZALO 09345 497 28
Thư viện Tài liệu mẫu
 
Đề Tài Khóa luận 2024 Tìm hiểu quy trình lập và phát hành Báo cáo kiểm toán v...
Đề Tài Khóa luận 2024 Tìm hiểu quy trình lập và phát hành Báo cáo kiểm toán v...
lamluanvan.net Viết thuê luận văn
 
NÂNG CAO HIỆU QUẢ SỬ DỤNG VỐN LƯU ĐỘNG TẠI CÔNG TY CỔ PHẦN MAY ĐỨC GIANG
NÂNG CAO HIỆU QUẢ SỬ DỤNG VỐN LƯU ĐỘNG TẠI CÔNG TY CỔ PHẦN MAY ĐỨC GIANG
Nguyễn Công Huy
 
Đề tài: Tư vấn tài chính doanh nghiệp tại công ty Chứng khoán, 9đ - Gửi miễn ...
Đề tài: Tư vấn tài chính doanh nghiệp tại công ty Chứng khoán, 9đ - Gửi miễn ...
Dịch vụ viết bài trọn gói ZALO: 0909232620
 
Các Nhân Tố Ảnh Hưởng Đến Hành Vi Quản Trị Lợi Nhuận Trên Báo Cáo Tài Chính
Các Nhân Tố Ảnh Hưởng Đến Hành Vi Quản Trị Lợi Nhuận Trên Báo Cáo Tài Chính
Hỗ Trợ Viết Đề Tài luanvanpanda.com
 

Viewers also liked (20)

Angularjs - Unit testing introduction
Angularjs - Unit testing introduction
Nir Kaufman
 
Hybrid application development
Hybrid application development
Knoldus Inc.
 
TDD Basics with Angular.js and Jasmine
TDD Basics with Angular.js and Jasmine
Luis Sánchez Castellanos
 
Deep dive into sass
Deep dive into sass
Knoldus Inc.
 
Unit testing in JavaScript with Jasmine and Karma
Unit testing in JavaScript with Jasmine and Karma
Andrey Kolodnitsky
 
Introduction to BDD
Introduction to BDD
Knoldus Inc.
 
Unit testing of spark applications
Unit testing of spark applications
Knoldus Inc.
 
Angular JS 2_0 BCS CTO_in_Res V3
Angular JS 2_0 BCS CTO_in_Res V3
Bruce Pentreath
 
Angular 2.0 change detection
Angular 2.0 change detection
Ran Wahle
 
Jasmine BDD for Javascript
Jasmine BDD for Javascript
Luis Alfredo Porras Páez
 
Angular2 with type script
Angular2 with type script
Ravi Mone
 
Intro to testing Javascript with jasmine
Intro to testing Javascript with jasmine
Timothy Oxley
 
Angular js 2
Angular js 2
Ran Wahle
 
Introduction to Knockout Js
Introduction to Knockout Js
Knoldus Inc.
 
Functional programming in Javascript
Functional programming in Javascript
Knoldus Inc.
 
HTML5, CSS, JavaScript Style guide and coding conventions
HTML5, CSS, JavaScript Style guide and coding conventions
Knoldus Inc.
 
Introduction to Apache Cassandra
Introduction to Apache Cassandra
Knoldus Inc.
 
Angular2 - getting-ready
Angular2 - getting-ready
Nir Kaufman
 
Introduction to Quasiquotes
Introduction to Quasiquotes
Knoldus Inc.
 
Apache Flink Crash Course by Slim Baltagi and Srini Palthepu
Apache Flink Crash Course by Slim Baltagi and Srini Palthepu
Slim Baltagi
 
Angularjs - Unit testing introduction
Angularjs - Unit testing introduction
Nir Kaufman
 
Hybrid application development
Hybrid application development
Knoldus Inc.
 
Deep dive into sass
Deep dive into sass
Knoldus Inc.
 
Unit testing in JavaScript with Jasmine and Karma
Unit testing in JavaScript with Jasmine and Karma
Andrey Kolodnitsky
 
Introduction to BDD
Introduction to BDD
Knoldus Inc.
 
Unit testing of spark applications
Unit testing of spark applications
Knoldus Inc.
 
Angular JS 2_0 BCS CTO_in_Res V3
Angular JS 2_0 BCS CTO_in_Res V3
Bruce Pentreath
 
Angular 2.0 change detection
Angular 2.0 change detection
Ran Wahle
 
Angular2 with type script
Angular2 with type script
Ravi Mone
 
Intro to testing Javascript with jasmine
Intro to testing Javascript with jasmine
Timothy Oxley
 
Angular js 2
Angular js 2
Ran Wahle
 
Introduction to Knockout Js
Introduction to Knockout Js
Knoldus Inc.
 
Functional programming in Javascript
Functional programming in Javascript
Knoldus Inc.
 
HTML5, CSS, JavaScript Style guide and coding conventions
HTML5, CSS, JavaScript Style guide and coding conventions
Knoldus Inc.
 
Introduction to Apache Cassandra
Introduction to Apache Cassandra
Knoldus Inc.
 
Angular2 - getting-ready
Angular2 - getting-ready
Nir Kaufman
 
Introduction to Quasiquotes
Introduction to Quasiquotes
Knoldus Inc.
 
Apache Flink Crash Course by Slim Baltagi and Srini Palthepu
Apache Flink Crash Course by Slim Baltagi and Srini Palthepu
Slim Baltagi
 
Ad

Similar to Unit Testing and Coverage for AngularJS (20)

Angular Unit Testing
Angular Unit Testing
Avi Engelshtein
 
JavaCro'14 - Unit testing in AngularJS – Slaven Tomac
JavaCro'14 - Unit testing in AngularJS – Slaven Tomac
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
Unit testing JavaScript: Jasmine & karma intro
Unit testing JavaScript: Jasmine & karma intro
Maurice De Beijer [MVP]
 
Unit testing of java script and angularjs application using Karma Jasmine Fra...
Unit testing of java script and angularjs application using Karma Jasmine Fra...
Samyak Bhalerao
 
JavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and Karma
Christopher Bartling
 
Angular Unit testing.pptx
Angular Unit testing.pptx
RiyaBangera
 
Slaven tomac unit testing in angular js
Slaven tomac unit testing in angular js
Slaven Tomac
 
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
Christopher Bartling
 
Angular testing
Angular testing
Raissa Ferreira
 
AngularJS Unit Testing w/Karma and Jasmine
AngularJS Unit Testing w/Karma and Jasmine
foxp2code
 
Quick tour to front end unit testing using jasmine
Quick tour to front end unit testing using jasmine
Gil Fink
 
AngularJS Unit Test
AngularJS Unit Test
Chiew Carol
 
Quick Tour to Front-End Unit Testing Using Jasmine
Quick Tour to Front-End Unit Testing Using Jasmine
Gil Fink
 
Angular Testing
Angular Testing
Kourosh Sajjadi
 
Front end unit testing using jasmine
Front end unit testing using jasmine
Gil Fink
 
An Introduction To Testing In AngularJS Applications
An Introduction To Testing In AngularJS Applications
Rohan Chandane
 
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
Ortus Solutions, Corp
 
3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API
Gavin Pickin
 
3 WAYS TO TEST YOUR COLDFUSION API -
3 WAYS TO TEST YOUR COLDFUSION API -
Ortus Solutions, Corp
 
Testing in AngularJS
Testing in AngularJS
Peter Drinnan
 
Unit testing JavaScript: Jasmine & karma intro
Unit testing JavaScript: Jasmine & karma intro
Maurice De Beijer [MVP]
 
Unit testing of java script and angularjs application using Karma Jasmine Fra...
Unit testing of java script and angularjs application using Karma Jasmine Fra...
Samyak Bhalerao
 
JavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and Karma
Christopher Bartling
 
Angular Unit testing.pptx
Angular Unit testing.pptx
RiyaBangera
 
Slaven tomac unit testing in angular js
Slaven tomac unit testing in angular js
Slaven Tomac
 
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
Christopher Bartling
 
AngularJS Unit Testing w/Karma and Jasmine
AngularJS Unit Testing w/Karma and Jasmine
foxp2code
 
Quick tour to front end unit testing using jasmine
Quick tour to front end unit testing using jasmine
Gil Fink
 
AngularJS Unit Test
AngularJS Unit Test
Chiew Carol
 
Quick Tour to Front-End Unit Testing Using Jasmine
Quick Tour to Front-End Unit Testing Using Jasmine
Gil Fink
 
Front end unit testing using jasmine
Front end unit testing using jasmine
Gil Fink
 
An Introduction To Testing In AngularJS Applications
An Introduction To Testing In AngularJS Applications
Rohan Chandane
 
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
Ortus Solutions, Corp
 
3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API
Gavin Pickin
 
3 WAYS TO TEST YOUR COLDFUSION API -
3 WAYS TO TEST YOUR COLDFUSION API -
Ortus Solutions, Corp
 
Testing in AngularJS
Testing in AngularJS
Peter Drinnan
 
Ad

More from Knoldus Inc. (20)

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

Recently uploaded (20)

Decipher SEO Solutions for your startup needs.
Decipher SEO Solutions for your startup needs.
mathai2
 
Zoneranker’s Digital marketing solutions
Zoneranker’s Digital marketing solutions
reenashriee
 
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
BradBedford3
 
Streamlining CI/CD with FME Flow: A Practical Guide
Streamlining CI/CD with FME Flow: A Practical Guide
Safe Software
 
Advance Doctor Appointment Booking App With Online Payment
Advance Doctor Appointment Booking App With Online Payment
AxisTechnolabs
 
Open Source Software Development Methods
Open Source Software Development Methods
VICTOR MAESTRE RAMIREZ
 
Download Adobe Illustrator Crack free for Windows 2025?
Download Adobe Illustrator Crack free for Windows 2025?
grete1122g
 
Threat Modeling a Batch Job Framework - Teri Radichel - AWS re:Inforce 2025
Threat Modeling a Batch Job Framework - Teri Radichel - AWS re:Inforce 2025
2nd Sight Lab
 
Key Challenges in Troubleshooting Customer On-Premise Applications
Key Challenges in Troubleshooting Customer On-Premise Applications
Tier1 app
 
Automated Migration of ESRI Geodatabases Using XML Control Files and FME
Automated Migration of ESRI Geodatabases Using XML Control Files and FME
Safe Software
 
IDM Crack with Internet Download Manager 6.42 Build 41 [Latest 2025]
IDM Crack with Internet Download Manager 6.42 Build 41 [Latest 2025]
pcprocore
 
Best Practice for LLM Serving in the Cloud
Best Practice for LLM Serving in the Cloud
Alluxio, Inc.
 
arctitecture application system design os dsa
arctitecture application system design os dsa
za241967
 
Folding Cheat Sheet # 9 - List Unfolding 𝑢𝑛𝑓𝑜𝑙𝑑 as the Computational Dual of ...
Folding Cheat Sheet # 9 - List Unfolding 𝑢𝑛𝑓𝑜𝑙𝑑 as the Computational Dual of ...
Philip Schwarz
 
Building Geospatial Data Warehouse for GIS by GIS with FME
Building Geospatial Data Warehouse for GIS by GIS with FME
Safe Software
 
Digital Transformation: Automating the Placement of Medical Interns
Digital Transformation: Automating the Placement of Medical Interns
Safe Software
 
Reimagining Software Development and DevOps with Agentic AI
Reimagining Software Development and DevOps with Agentic AI
Maxim Salnikov
 
The Anti-Masterclass Live - Peak of Data & AI 2025
The Anti-Masterclass Live - Peak of Data & AI 2025
Safe Software
 
IObit Driver Booster Pro 12 Crack Latest Version Download
IObit Driver Booster Pro 12 Crack Latest Version Download
pcprocore
 
Canva Pro Crack Free Download 2025-FREE LATEST
Canva Pro Crack Free Download 2025-FREE LATEST
grete1122g
 
Decipher SEO Solutions for your startup needs.
Decipher SEO Solutions for your startup needs.
mathai2
 
Zoneranker’s Digital marketing solutions
Zoneranker’s Digital marketing solutions
reenashriee
 
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
BradBedford3
 
Streamlining CI/CD with FME Flow: A Practical Guide
Streamlining CI/CD with FME Flow: A Practical Guide
Safe Software
 
Advance Doctor Appointment Booking App With Online Payment
Advance Doctor Appointment Booking App With Online Payment
AxisTechnolabs
 
Open Source Software Development Methods
Open Source Software Development Methods
VICTOR MAESTRE RAMIREZ
 
Download Adobe Illustrator Crack free for Windows 2025?
Download Adobe Illustrator Crack free for Windows 2025?
grete1122g
 
Threat Modeling a Batch Job Framework - Teri Radichel - AWS re:Inforce 2025
Threat Modeling a Batch Job Framework - Teri Radichel - AWS re:Inforce 2025
2nd Sight Lab
 
Key Challenges in Troubleshooting Customer On-Premise Applications
Key Challenges in Troubleshooting Customer On-Premise Applications
Tier1 app
 
Automated Migration of ESRI Geodatabases Using XML Control Files and FME
Automated Migration of ESRI Geodatabases Using XML Control Files and FME
Safe Software
 
IDM Crack with Internet Download Manager 6.42 Build 41 [Latest 2025]
IDM Crack with Internet Download Manager 6.42 Build 41 [Latest 2025]
pcprocore
 
Best Practice for LLM Serving in the Cloud
Best Practice for LLM Serving in the Cloud
Alluxio, Inc.
 
arctitecture application system design os dsa
arctitecture application system design os dsa
za241967
 
Folding Cheat Sheet # 9 - List Unfolding 𝑢𝑛𝑓𝑜𝑙𝑑 as the Computational Dual of ...
Folding Cheat Sheet # 9 - List Unfolding 𝑢𝑛𝑓𝑜𝑙𝑑 as the Computational Dual of ...
Philip Schwarz
 
Building Geospatial Data Warehouse for GIS by GIS with FME
Building Geospatial Data Warehouse for GIS by GIS with FME
Safe Software
 
Digital Transformation: Automating the Placement of Medical Interns
Digital Transformation: Automating the Placement of Medical Interns
Safe Software
 
Reimagining Software Development and DevOps with Agentic AI
Reimagining Software Development and DevOps with Agentic AI
Maxim Salnikov
 
The Anti-Masterclass Live - Peak of Data & AI 2025
The Anti-Masterclass Live - Peak of Data & AI 2025
Safe Software
 
IObit Driver Booster Pro 12 Crack Latest Version Download
IObit Driver Booster Pro 12 Crack Latest Version Download
pcprocore
 
Canva Pro Crack Free Download 2025-FREE LATEST
Canva Pro Crack Free Download 2025-FREE LATEST
grete1122g
 

Unit Testing and Coverage for AngularJS

  • 1. Unit Testing and Coverage for AngularJS Sumit Khanduri Software Consultant Knoldus Software LLP
  • 2. Introduction ● Unit testing, as the name implies, is about testing individual units of code. ● "Did I think about the logic correctly?" or "Does the sort function order the list in the right order?" ● The primary goal of unit testing is to take the smallest piece of testable software in the application.
  • 3. Testing AngularJS App`s ● AngularJS build around the concept of dependency injection & loose coupling to the DOM which make it testable. ● Angular comes with dependency injection built-in, which makes testing components much easier, because you can pass in a component's dependencies and mock them as you wish. ● Components that have their dependencies injected allow them to be easily mocked on a test by test basis, without having to mess with any global variables that could accidentally affect another test.
  • 4. TDD
  • 5. ● Karma is a JavaScript command line tool that can be used to spawn a web server which loads your application's source code and executes your tests. ● You can configure Karma to run against a number of browsers. ● Karma is a NodeJS application, and should be installed through npm.
  • 6. The main goal for Karma is to bring a productive testing environment to developers. The environment being one where they don't have to set up loads of configurations, but rather a place where developers can just write the code and get instant feedback from their tests. Because getting quick feedback is what makes you productive and creative. http://karma-runner.github.io/
  • 7. 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. And it has a clean, obvious syntax so that you can easily write tests. http://jasmine.github.io/
  • 8. Unit Test Strucuture - Specs it('should increment a variable', function () { var foo = 0; // set up the world foo++; // call your application code expect(foo).toEqual(1); // passes because foo == 1 });
  • 9. Unit Test Structure – Suites Collections of specs Your test files will contain one or more suites, each with one or more specs. describe("User Validation", function() { it("requires a name", function() { //spec content }) it("does not validate invalid names", function() { //spec content }) });
  • 10. Unit Test Structure – expect() expect() is the actual 'test' Takes a value, then 'matchers' it('should increment a variable', function () { var foo = 0; // set up the world foo++; // call your application code expect(foo).toEqual(1); // passes because foo == 1 });
  • 11. Mocking in Jasmine A test is not a unit test if: 1. It talks to the database 2. It communicates across the network 3. It touches the file system 4. It can't run correctly at the same time as any of your other unit tests 5. You have to do special things to your environment (such as editing config files) to run it. ~ Michael Feathers Need to use mocks to simulate calls to external services, files, etc Need to use mocks to reduce real dependencies in tests.
  • 12. Mock HTTP ($httpBackend) ● Fake HTTP backend implementation suitable for unit testing applications that use the $http service. ● Example: mockHttpBackend.expect('POST',config.serverURL+ 'admin/login',{'email':'','password':''}).respond({ data : {} });
  • 14. Mocking in Jasmine – Spies ● Easily inject mock/monitoring objects ● Replaces or wraps the function it's spying on ● Gives run-time statistics on the spied function ● Know how many times a function has been called ● Inspect return values ● Inspect parameters it('should run Successfully', function() { spyOn(mockState, 'go'); mockScope.addEmployee(); expect(mockState.go).toHaveBeenCalledWith('home'); });
  • 15. Spies
  • 16. Istanbul Yet another JS code coverage tool that computes statement, line, function and branch coverage with module loader hooks to transparently add coverage when running tests. Supports all JS coverage use cases including unit tests, server side functional tests https://www.npmjs.com/package/istanbul
  • 17. Istanbul-Setup ● Install karma-coverage > sudo npm install -g karma-coverage ● Install istanbul > sudo npm install istanbul ● Karma.conf.js > Inside preprocessors json object add the path to your file for which you need coverage report. E.g., preprocessors: 'app/component/add/add.ctrl.js':'coverage', > Inside reporters add a string named 'coverage', E.g., reporters: ['progress','coverage',].
  • 19. Setup Test Environment ● Install Karma > npm install -g karma –save-dev ● Install Jasmine > npm install karma-jasmine jasmine-core –save-dev ● Install ngMock > npm install angular-mocks –save-dev ● Browser > npm install karma-chrome-launcher –save-dev ● Create two folders in your working directory > mkdir app > mkdir tests
  • 20. Contd.. ● Karma.conf.js > karma init > Select jasmine as your testing framework > Select browser, whichever you like > Specify the paths to your spec files. E.g., 'app/*.js', 'test/*.js'. > Add the location of angularjs in your karma.conf.js. > Add the location of ngMock library in your karma.conf.js ● Optional commands > npm install karma --save-dev > npm install karma-jasmine karma-chrome-launcher –save-dev > npm install -g karma-cli > karma start
  • 22. References ● http://www.slideshare.net/timoxley/testing-with-jasmine?next_slideshow=1 ● http://www.slideshare.net/nirkaufman/angularjs-unit-testing ● http://java.ociweb.com/mark/other-presentations/AngularJS-Testing.pdf ● https://www.youtube.com/watch?v=f7lIBiLmISQ

Editor's Notes

  • #3: isolate it from the remainder of the code, and determine whether it behaves exactly as you expect.
  • #4: dju56ud5
  • #6: which is useful for being confident that your application works on all browsers you need to support. Karma is executed on the command line and will display the results of your tests on the command line once they have run in the browser.