Jasmine

Behavior Driven Development Test Framework for JavaScript

Created by Adam Swift / @Gleeble

Introduction

  • Currently a Software Engineer for 5AM Solutions
  • Focus on Front End Web and Mobile Development
  • 10 Years Java Experience
  • 3 Years JavaScript

Agenda

  1. Background
  2. About Jasmine
  3. Test Setup
  4. Validators
  5. Spies
  6. Testing the Clock
  7. Additional Utilities
  8. Alternatives

Background

“Any application that can be written in JavaScript, will eventually be written in JavaScript. - Atwood's Law (Jeff Atwood)”
  • Where do you find JS?
    • Web pages
    • Node.js
    • Mobile Development
  • How do you test JS?

About Jasmine

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.

“You will love Jasmine. You will want to marry Jasmine. - Jim Weir”

Test Setup

describe(description, suiteFunction)
beforeEach(setupFunction)
it(description, testFunction)
afterEach(tearDownFunction)
xdescribe() | xit()

Validators


                        expect(variable).toBe(expectedVariable);
                    

Primary Validators

  • .toBe()
  • .toEqual()
  • .toBeDefined()
  • .toBeUndefined()
  • .toBeNull()
  • .not

Additional Validators

.toMatch(), .toBeTruthy(), .toBeFalsy(), .toContain(), .toBeLessThan(), .toBeGreaterThan(), .toBeCloseTo() .toThrow(), jasmine.any()

Custom Validators


beforeEach(function() {
    this.addMatchers({
        toBeLessThan: function(expected) {
            return this.actual < expected;
        }
    });
});
                        
expect(lowNum).toBeLessThan(5);

Spies

Spy Creation

spyOn(object, "functionName")
jasmine.createSpy("spyName")
jasmine.createSpyObj("spyName",["functionNames"])

Spy Usage

.andReturn(val)
.andCallFake(function(){})
.andCallThrough()
.andThrow(err)

Spy Validators

.toHaveBeenCalled()
.toHaveBeenCalledWith(arguments)

Spy Properties


spy.calls[]
spy.calls[0].args[]
spy.mostRecentCall
spy.callCount
                        

Using Spies to test AJAX


spyOn($, 'ajax).andCallFake(function(url, settings){
    var deferred = $.Deferred();
    deferred.resolve(); //or deferred.reject()
    return deferred.promise();
};
                        

Testing the Clock


var timerCallback;

beforeEach(function() {
    timerCallback = jasmine.createSpy('timerCallback');
    jasmine.Clock.useMock();
});

it("causes a timeout to be called synchronously", function() {
    setTimeout(function() {
        timerCallback();
    }, 100);
    
    expect(timerCallback).not.toHaveBeenCalled();
    jasmine.Clock.tick(101);
    expect(timerCallback).toHaveBeenCalled();
});

                    

Additional Utilities

https://github.com/pivotal/jasmine/wiki/Related-projects

Javascript Testing Alternatives

Questions?

Contact Info

aswift@5amsolutions.com

@Gleeble


http://gleeble.github.io/mcjug-jasmine

https://github.com/Gleeble/mcjug-jasmine