You are on page 1of 8

Jasmine

Presented by Salman ul khair


Introduction
• Jasmine is one of the most popular JavaScript unit testing frameworks
which is capable of testing synchronous and asynchronous JavaScript
code.
• It is used in BDD (behavior-driven development) programming which
focuses more on the business value than on the technical details.
Jasmine Suite and Specs
• Suite
describe("Test Suite", function() {
//.....
});
• Spec

describe("Test Suite", function() {


it("test spec", function() {
expect( expression ).toEqual(true);
});
});
Setup and Teardown
describe("A suite with some shared setup", function() {
var foo = 0;
beforeEach(function() {
foo += 1;
});

afterEach(function() {
foo = 0;
});

beforeAll(function() {
foo = 1;
});
afterAll(function() {
foo = 0;
});

});
Jasmine Matchers
• MATCHER PURPOSE
• toBe() passed if the actual value is of the same type and value as that of the expected value. It compares with === operator
• toEqual() works for simple literals and variables;
• toMatch() to check whether a value matches a string or a regular expression
• toBeDefined() to ensure that a property or a value is defined
• toBeUndefined() to ensure that a property or a value is undefined
• toBeNull() to ensure that a property or a value is null.
• toBeTruthy() to ensure that a property or a value is true
• ToBeFalsy() to ensure that a property or a value is false
• toContain() to check whether a string or array contains a substring or an item.
• toBeLessThan() for mathematical comparisons of less than
• toBeGreaterThan() for mathematical comparisons of greater than
• toBeCloseTo() for precision math comparison
• toThrow() for testing if a function throws an exception
• toThrowError() for testing a specific thrown exception
• Use not for negation
Jasmine Spies
describe("MathUtils", //Test for sum operation
function() { it("should be able to calculate sum of 3
and 5", function() {
var calc; //call any method
calc.sum(3,5);
beforeEach(function() {
//verify it got executed
calc = new MathUtils();
expect(calc.sum).toHaveBeenCalled();
spyOn(calc, 'sum');
expect(calc.sum).toHaveBeenCalledWith(3,5)
}); ;
});

describe("when calc is used


to peform basic math });
operations", function(){ });
Asynchronous Support
• Jasmine also has support for running specs that require testing
asynchronous operations. The functions that you pass to beforeAll,
afterAll, beforeEach, afterEach, and it can be asynchronous. There are
three different ways to indicate that a function is asynchronous: by
taking an optional callback parameter, by returning a promise, or by
using the async keyword in environments that support it.
Thank you
Any Questions..

You might also like