Created by Adam Swift / @Gleeble
“Any application that can be written in JavaScript, will eventually be written in JavaScript. - Atwood's Law (Jeff Atwood)”
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”
describe(description, suiteFunction)
beforeEach(setupFunction)
it(description, testFunction)
afterEach(tearDownFunction)
xdescribe() | xit()
expect(variable).toBe(expectedVariable);
.toMatch(), .toBeTruthy(), .toBeFalsy(), .toContain(), .toBeLessThan(), .toBeGreaterThan(), .toBeCloseTo() .toThrow(), jasmine.any()
beforeEach(function() {
this.addMatchers({
toBeLessThan: function(expected) {
return this.actual < expected;
}
});
});
expect(lowNum).toBeLessThan(5);
spyOn(object, "functionName")
jasmine.createSpy("spyName")
jasmine.createSpyObj("spyName",["functionNames"])
.andReturn(val)
.andCallFake(function(){})
.andCallThrough()
.andThrow(err)
.toHaveBeenCalled()
.toHaveBeenCalledWith(arguments)
spy.calls[]
spy.calls[0].args[]
spy.mostRecentCall
spy.callCount
spyOn($, 'ajax).andCallFake(function(url, settings){
var deferred = $.Deferred();
deferred.resolve(); //or deferred.reject()
return deferred.promise();
};
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();
});