Skip to content

Instantly share code, notes, and snippets.

@andrewk
Last active January 3, 2018 21:49
Show Gist options
  • Save andrewk/e26f193ac2aa0c06d8a8 to your computer and use it in GitHub Desktop.
Save andrewk/e26f193ac2aa0c06d8a8 to your computer and use it in GitHub Desktop.
Jasmine 2.0 quick reference

Spies

Create a spy

// "bare" spy
var spy = jasmine.createSpy('spyName');

// Mock object of spies: spy.next(), spy.current(), etc
var spy = jasmine.createSpyObj('spy', ['next', 'current', 'count', 'index']);

// Most common use: replace object method with spy
var spy = spyOn(myObject, 'methodName');
spyOn(myObject, 'methodName');

// Add spy but also call original method
var spy = spyOn(myObject, 'methodName').and.callThrough();

// Spy on and replace a method
var spy = spyOn(myObject, 'methodName').and.callFake(function() {
  return 'blergh';
});

Spy expectations

expect(spy).toHaveBeenCalled();
expect(spy).toHaveBeenCalledWith(jasmine.any(Object), 'foo');
expect(spy.calls.mostRecent().args[1]).toEqual('foo');
expect(spy.calls.first().args[1]).toEqual('qux');
expect(spy.calls.count()).toEqual(2);

Expectations and Matchers

expect(true).toBe(true);
expect(false).not.toBe(true);
expect([1,2,3].length).toEqual(3);
expect(undefined).not.toBeDefined();
expect(null).toBeNull();
expect([1,2,3]).toEqual(jasmine.any(Array));
// jasmine.any takes any constructor as its argument, allowing matching types
expect(function(cb) { cb(); }).toEqual(jasmine.any(Function));
expect(results).toEqual(jasmine.objectContaining({'foo': 'bar'}));
expect([1,2,3]).toContain(2);
expect(true).toBeTruthy(); // casts to boolean
expect(false).toBeFalsy();
expect(Math.PI).toBeGreaterThan(3);
expect(Math.PI).toBeLessThan(3.2);
expect(Math.PI).toBeCloseTo(3.14, 2); // second param is precision
expect({} + {}).toBeNan();
expect('hullo this is foo').toMatch(/foo$/);
expect('hullo this is foo').toMatch('hullo');

jasmine-jquery adds a lot of excellent matchers. As of this writing, all 99designs apps use jasmine-jquery

XMLHttpRequest

Setup

beforeEach(function() {
  jasmine.Ajax.install();
});

afterEach(function() {
  jasmine.Ajax.uninstall();
});

Inspecting and responding to request

var request = jasmine.Ajax.requests.mostRecent();
expect(request.url).toEqual('http://example.com/customers?query=Frank');
expect(request.method).toEqual('GET');

request.respondWith({'foo' : 'bar'});

Date and Timers

Setup

beforeEach(function() {
  jasmine.clock().install();
});

afterEach(function() {
  jasmine.clock().uninstall();
});

Manipulating time

var timeSpy = jasmine.createSpy('timeSpy');
setTimeout(timeSpy, 200);
expect(timeSpy).not.toHaveBeenCalled();
jasmine.clock().tick(201);
expect(timeSpy).toHaveBeenCalled();

Jasmine Suite Functions

describe('description', function(){});
ddescribe('description', function(){}); // will only run this
xdescribe('description', function(){}); // won't run this

it('description', function(){});
iit('description', function(){}); // will only run this
xit('description', function(){}); // won't run this

// it() with only one argument is a pending spec.
// Use this to outline your design
it('puts the lotion in the basket');
@dannymidnight
Copy link

@jamos and myself have just been dealing with testing some async functions. It's handy to know that a done callback is passed into it, beforeEach and afterEach which can optionally be executed to confirm that all processing has been completed.

http://jasmine.github.io/2.0/introduction.html#section-Asynchronous_Support

@andrewk
Copy link
Author

andrewk commented Jan 13, 2015

@dannymidnight I'm interested to see those tests and investigate if they really need to be async. I've refactored out all async specs both in Projects and in my full-async node automation library. Async specs are harder to read and modify, with that in mind I try to avoid them.

@jamosonic
Copy link

A note about mocking window (or not mocking window as the case may be) would be handy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment