References
- Also see the Jest cheatsheet. Jest uses Jasmine and therefore has a similar API.
- https://jasmine.github.io
- Jasmine Cheat Sheet
[Tests] Event spies
spyOnEvent($('#some_element'), 'click')
$('#some_element').click()
expect('click').toHaveBeenPreventedOn($('#some_element'))
expect('click').toHaveBeenTriggeredOn($('#some_element'))
[Tests] HTML runner
var jasmineEnv = jasmine.getEnv()
jasmineEnv.updateInterval = 250
var htmlReporter = new jasmine.HtmlReporter()
jasmineEnv.addReporter(htmlReporter)
$(function() { jasmineEnv.execute() })
Jasmine jQuery
[Tests] Async
test('works with promises', () => {
return new Promise((resolve, reject) => {
···
})
})
Make your test return a promise.
[Tests] Creating spies
stub = jasmine.createSpy('stub')
stub('hello')
expect(stub.identity).toEqual('stub')
expect(stub).toHaveBeenCalled()
[Tests] Spies
spyOn(foo, 'setBar')
spyOn(foo, 'setBar').andReturn(123)
spyOn(foo, 'getBar').andCallFake(function() { return 1001; })
foo.setBar(123)
expect(foo.setBar).toHaveBeenCalled()
expect(foo.setBar).toHaveBeenCalledWith(123)
expect(foo.setBar.calls.length).toEqual(2)
expect(foo.setBar.calls[0].args[0]).toEqual(123)
[Tests] Pending
xit('this is a pending test', () => {
···
})
xdescribe('this is a pending block', () => {
···
})
[Tests] Hooks
beforeEach(() => {
···
})
afterEach(() => {
···
})
[Tests] Expectations
expect(true).toBe(true)
expect(true).not.toBe(true)
expect(a).toEqual(bar)
expect(message).toMatch(/bar/)
expect(message).toMatch('bar')
expect(a.foo).toBeDefined()
expect(a.foo).toBeUndefined()
expect(a.foo).toBeNull()
expect(a.foo).toBeTruthy()
expect(a.foo).toBeFalsy()
expect(message).toContain('hello')
expect(pi).toBeGreaterThan(3)
expect(pi).toBeLessThan(4)
expect(pi).toBeCloseTo(3.1415, 0.1)
expect(func).toThrow()
[Tests] Writing tests
describe('A suite', () => {
it('works', () => {
expect(true).toBe(true)
})
})
Top comments (0)