12
JavaScript Unit Test & Code Coverage Premnath Thimma

Unit Testing JavaScript with Jasmine

Embed Size (px)

Citation preview

Page 1: Unit Testing JavaScript with Jasmine

JavaScript Unit Test & Code Coverage

Premnath Thimma

Page 2: Unit Testing JavaScript with Jasmine

Thought

A solution is only as good as your understanding of the problem.

Page 3: Unit Testing JavaScript with Jasmine

Thought

Page 4: Unit Testing JavaScript with Jasmine

Why Unit Test?

• Promote better design – loose coupling

• Helps developers to write

– Testable unit

– Maintainable code

• Improves quality at unit level

Page 5: Unit Testing JavaScript with Jasmine

Difficulties/Problems

• Lack of structure

• Function not exposed outside

• Complexity in the function ( > 10 lines )

• Tight coupling between components

Page 6: Unit Testing JavaScript with Jasmine

Solutions from AngularJS

• Structure– Module, Model, View, Controller, Service,

Templates, Filters

• Function not exposed outside– Service, Factory, Constant, Value

• Complexity in the function ( > 10 lines )– Module, Injectable Components

• Tight coupling between components– Loose coupling with Structure, Module

Page 7: Unit Testing JavaScript with Jasmine

Tools

• JS Libraries

– Jasmine, Qunit, Mocha

• Test Runner

– Chutzpah

• Visual Studio Plug-ins

– Chutzpah Test Adapter for the Test Explorer

– Chutzpah Test Runner Context Menu Extension

– Karma Test Adapter

Page 8: Unit Testing JavaScript with Jasmine

Get to know Jasmine

• Supports BDD (User Story)

– Spec (describe, it)

– Nest-able Spec

• Powerful Matchers (+ Customer Matcher)

• Powerful concept like Spy

• Supported by Visual Studio

– Test Explorer (includes JS Unit Tests)

– Recognizes your changes to Unit Test

Page 9: Unit Testing JavaScript with Jasmine

Get to know Chutzpah

• Test Runner– Right Click and Run

– Runs automatically in Visual Studio when you make changes to Unit Test

• Code Coverage– From Visual Studio

– From Command Line

– Exclude and Include files using expression

– Context sensitive to File or Folder

Page 10: Unit Testing JavaScript with Jasmine

Demo

Page 11: Unit Testing JavaScript with Jasmine

Questions

Page 12: Unit Testing JavaScript with Jasmine

References

• https://github.com/mmanela/chutzpah

• http://chutzpah.codeplex.com/documentation

• http://jasmine.github.io/edge/introduction.html

• https://docs.angularjs.org/guide/unit-testing