Jasmine – Behavior Driven Development Test Framework for JavaScript



Jasmine – Behavior Driven Development Test Framework for JavaScript

0 0


mcjug-jasmine

Jasmine Presentation for the MCJUG made in reveal.js

On Github Gleeble / mcjug-jasmine

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

Background About Jasmine Test Setup Validators Spies Testing the Clock Additional Utilities 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()
beforeEach and afterEach act as the setup and tearDown methods for the tests. They will be run progressively down the describe blocks for each set of specs. The describe() element defines a set of tests and allows for describe blocks to be nested to keep logical functionalities together. The it() function defines the test cases. The describe() and it() functions take in a descriptive string along with a function containing the functionality for the test. You can ignore tests by prefixing the suite or test functions for `x`. Jasmine 2.0 adds additional changes to provide for asynchronous testing, passing in a done function as a parameter to the beforeEach, it, and afterEach functions. Additionally, the xit and xdescribe no longer just ignore tests but they are marked as pending in the test results.

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()

.toMatch() Matches a string against a regular expression. .toContain() checks against an array for an object. .toBeCloseTo() Checks against a # to see if the result is close to it. jasmine.any() can be used to check against any object types. ex. jasmine.any(Function), jasmine.any(Number), jasmine.any(Object). this cannot be used with .toBe() but does work with .toEqual() Jasmine 2.0 also adds the jasmine.objectContaining() to check that an object contains certain values.

Custom Validators

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

Spies

Spies create a mock function in place of an existing function. Jasmine keeps track of the original and after running each spec it puts the original function back on the object as necessary. The mock functions contain a number of additional properties and ability to define how the function acts when called.

Spy Creation

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

Spy Usage

.andReturn(val)
.andCallFake(function(){})
.andCallThrough()
.andThrow(err)
In Jasmine 2.0 the syntax for these functions has changed. and.returnValue() and.callFake() and.callThrough() and.throwError() and.stub()

Spy Validators

.toHaveBeenCalled()
.toHaveBeenCalledWith(arguments)

Spy Properties

spy.calls[]
spy.calls[0].args[]
spy.mostRecentCall
spy.callCount
                        
Jasmine 2.0 adds a whole bunch of new utility functions to better access the calls and arguments in the spy as well as reset the spy's call information.

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();
});

                    
Jasmine 2.0 has additional configuration for this.

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