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');
@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