Skip to content

About Jasmine

Akshay Prasad edited this page Dec 25, 2016 · 16 revisions

Jasmine

Jasmine is an open source 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.

These are the main concepts:

  • Suites— describe(string, function) functions, take a title and a function containing one or more specs.
  • Specs— it(string, function) functions, take a title and a function containing one or more expectations.
  • Expectations— are assertions that evaluate to true or false. Basic syntax reads expect(actual).toBe(expected)
  • **Matchers **— are predefined helpers for common assertions. Eg: toBe(expected), toEqual(expected). Find a complete list here.

A good practice to avoid code duplication on our specs is to include a setup code setting some local variables to be re-used.

use beforeEach and afterEach to do changes before and after each spec

Jasmine offers four handlers to add our setup and teardown code: beforeEach, afterEach executed for each spec and beforeAll, afterAll executed once per suite.

// single line
beforeEach(module('appmodule'));

// multiple lines
beforeEach(function(){
  module('appmodule');
  //...
});

Jasmine inject function uses dependency injection to resolve common services or providers, like $rootScope, $controller, $q (promises mock), $httpBackend ($http mock), and match them to the corresponding parameters. Common notations for inject are:

// Using _serviceProvider_ notation
var $q;
beforeEach(inject(function (_$q_) {
    $q = _$q_;
}));

// Using $injector
var $q;
beforeEach(inject(function ($injector) {
    $q = $injector.get('$q');
}));

// Using an alias Eg: $$q, q, _q
var $$q;
beforeEach(inject(function ($q) {
    $$q = $q;
}));

Check out the official Angular documentation on Unit Testing for more details.

Default Matchers

expect(fn).toThrow(e);
expect(instance).toBe(instance);
expect(mixed).toBeDefined();
expect(mixed).toBeFalsy();
expect(number).toBeGreaterThan(number);
expect(number).toBeLessThan(number);
expect(mixed).toBeNull();
expect(mixed).toBeTruthy();
expect(mixed).toBeUndefined();
expect(array).toContain(member);
expect(string).toContain(substring);
expect(mixed).toEqual(mixed);
expect(mixed).toMatch(pattern);

These are Jasmine’s default set of matchers.

Jasmine Matchers

  • toEqual checks for equality, not necessarily the same object.
var a=10; 
expect(a).toEqual(10)
  • toBe checks if two objects are the same.
var a=10,b=10; 
expect(a).toBe(b)
  • toBeTruthy checks if a value is true (else returns false ).
  • toBeFalsy checks if a value is false (else returns false ).
  • toContain checks if a value is inside another(checks for the substring in a String).
   Var a= “Hello World”;
   expect(a).toContain(“World”)
  • toBeDefined() checks if a value is defined.
  • toBeUndefined() checks if a value is undefined.
  • toBeNull() checks if a value is null.
  • toBeCloseTo() checks decimal proximity.

https://github.com/JamieMason/Jasmine-Matchers

Clone this wiki locally