Quick Jasmine 2 Reference

The Jasmine 2 Introduction is a wonderful combination of a demo and reference. However I’d rather not have to scroll through to find something.


Jasmine Description Example
not Negation expect(‘foo’).not.toBe(‘bar’)
toBe() === expect(‘1’).not.toBe(1)
toEqual() Smarter check; should handle simple objects. expect(‘1’).toEqual(1)
toMatch() Regex expect(‘1’).toMatch(/\d/)
toBeDefined() !== undefined expect(1).toBeDefined()
toBeUndefined() === undefined expect().toBeUndefined()
toBeNull() === null expect(null).toBeNull()
toBeTruthy() true expect(‘1’).toBeTruthy()
toBeFalsy() false expect(”).toBeFalsy()
toContain() in array expect([1,2]).toContain(2)
toBeLessThan() < expect(1).toBeLessThan(2)
toBeGreaterThan() > expect(1).toBeGreaterThan(0)
toBeCloseTo() floating point comparison expect(2.0/2.0).toBeCloseTo(1)
toThrow() pass in function reference expect(func).toThrow()

Notes on toThrow()

toThrow() takes a function reference, which it calls with no parameters. If you need to pass parameters, you can wrap it in another function(){}.

If called with no parameters, it passes on any exception. If called with a parameter, the exception must equal the parameter.

Leave a Reply