2. IMPORTANCE OF UNIT TEST
Makes the process Agile
Improves the Quality of Code
Finds application issues/bugs early
Detect Changes that may break the design contract in refactoring
Provide debugging process
Provides documentation
Reduces cost
3. AVAILABLE UNIT TEST FRAMEWORKS
Find more unit test frameworks - https://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#JavaScript
4. WHY JASMINE FRAMEWORK
Behaviour driven development framework, also supports TDD
Used for synchronous and asynchronous javascript code
Does not rely on DOM or any browser
Has built in assertion library and command line utility to run the tests
Has simple syntax
5. HELLO WORLD EXAMPLE
Javascript function –
- return ‘Hello World’ String
Unit test case for Javascript function –
- Calls actual function and matches the
output with expected output
6. INSTALL JASMINE FRAMEWORK
Download latest version of Jasmine framework – click here
Extract all the files
Open ‘SpecRunner.html’ file
7. JASMINE FILE STRUCTURE
Boot Js –
this file "boots" Jasmine,
performing all of the
necessary initialization
before executing the
loaded environment and
all of a project's specs.
Spec folder -
Contains the Javascript
tesintg files unit test
files
Src folder -
Contains the Javascript
source files
Jasmine-html.js –
Javascript library which
has pure js functions to
capture the running
statistics of suits / specs
Jasmine.js –
Jasmine’s core engine
which responsible to
manage test life cycle
SpecRunner.html–
Test case runner HTML
file
8. CREATE TEST AND TEST SUITE
describe(string suiteName) - Declare test suites using this function
Group of all respective it() /test cases
it (string testName) – Declare test case
Write multiple it() calls in a single describe() function
The string you passed to the describe() as the first parameter will be concatenated with the string passed to the
it(), to describe the complete name of the spec.
Syntax :
describe(‘Suite Name – Scenario name’, function() {
it(‘test case 1 name’, function() {
...
};
it(‘test case 2 name’, function() {
...
};
})
9. MATCHERS
• Any matcher can evaluate to a negative
assertion by chaining the call to expect with
a not before calling the matcher
• Each matcher implements a boolean
comparison between the actual value and
the expected value.
• It is responsible for reporting to Jasmine if
the expectation is true or false. Jasmine will
then pass or fail the spec.
11. SETUP AND TEARDOWN METHODS
beforeEach() and afterEach() – runs before and after for every suite
beforeAll() and afterAll() – runs before and after all the suites
before All afterAll
beforeEach
Test
afterEach
Test Suits
13. EXAMPLE - MATH.JS
• Create new file in SRC Folder
• Set file name as ‘Math.js’
• Create these functions for addition,
subtraction
• Make an entry in specRunner.html file
14. EXAMPLE - MATH-SPEC.JS
• Create new file in ‘spec’ Folder
• Set file name as ‘Math-spec.js’
• Make an entry in SpecRunner.html
file
15. JASMINE SPEC-RUNNER DASHBOARD
• Open SpecRunner.html file in any browser
• Calculator is the name of the ‘Test suite’
• Name start with ‘should’ are the spec /scenario
description
16. FAILING SPEC
The fail () function causes a spec to fail.
It can take a failure message or an Error object as a parameter.