3 category: JavaScript libraries
13 describe('A suite', () => {
15 expect(true).toBe(true)
20 Note: This cheatsheet may be a little outdated. Also see the [Jest cheatsheet](./jest). Jest uses Jasmine, and therefore has similar API.
25 expect(true).toBe(true)
26 expect(true).not.toBe(true)
30 expect(a).toEqual(bar)
34 expect(message).toMatch(/bar/)
35 expect(message).toMatch('bar')
39 expect(a.foo).toBeDefined()
40 expect(a.foo).toBeUndefined()
41 expect(a.foo).toBeNull()
45 expect(a.foo).toBeTruthy()
46 expect(a.foo).toBeFalsy()
50 expect(message).toContain('hello')
54 expect(pi).toBeGreaterThan(3)
55 expect(pi).toBeLessThan(4)
56 expect(pi).toBeCloseTo(3.1415, 0.1)
60 expect(func).toThrow()
80 xit('this is a pending test', () => {
86 xdescribe('this is a pending block', () => {
95 spyOn(foo, 'setBar').andReturn(123)
96 spyOn(foo, 'getBar').andCallFake(function() { return 1001; })
101 expect(foo.setBar).toHaveBeenCalled()
102 expect(foo.setBar).toHaveBeenCalledWith(123)
103 expect(foo.setBar.calls.length).toEqual(2)
104 expect(foo.setBar.calls[0].args[0]).toEqual(123)
110 stub = jasmine.createSpy('stub')
115 expect(stub.identity).toEqual('stub')
116 expect(stub).toHaveBeenCalled()
122 test('works with promises', () => {
123 return new Promise((resolve, reject) => {
129 Make your test return a promise.
134 var jasmineEnv = jasmine.getEnv()
135 jasmineEnv.updateInterval = 250
137 var htmlReporter = new jasmine.HtmlReporter()
138 jasmineEnv.addReporter(htmlReporter)
140 $(function() { jasmineEnv.execute() })
149 expect($('#id')).toBe('div')
150 expect($('input[type=checkbox]')).toBeChecked()
151 expect($('input[type=checkbox]')).toBeDisabled()
152 expect($('input[type=checkbox]')).toBeFocused()
153 expect($('#menu ul')).toBeEmpty()
157 expect($('#toolbar')).toBeHidden()
158 expect($('#toolbar')).toBeVisible()
162 expect($('#popup')).toHaveCss({ margin: "10px" })
163 expect($('option')).toBeSelected()
167 expect($('.foo')).toExist()
171 expect($('a')).toHaveAttr('rel')
172 expect($('a')).toHaveAttr('rel', 'nofollow')
176 expect($('a')).toHaveClass('rel')
177 expect($('a')).toHaveId('home')
181 expect($('a')).toHaveHtml('<span></span>')
182 expect($('a')).toContainHtml('<span></span>')
183 expect($('a')).toHaveText('hi')
187 expect($form).toHandle('submit') // event
188 expect($form).toHandleWith('submit', onSumbit)
191 See: [jasmine-jquery](https://github.com/velesin/jasmine-jquery)
196 spyOnEvent($('#some_element'), 'click')
197 $('#some_element').click()
198 expect('click').toHaveBeenPreventedOn($('#some_element'))
199 expect('click').toHaveBeenTriggeredOn($('#some_element'))
205 * Also see the [Jest cheatsheet](./jest). Jest uses Jasmine, and therefore has similar API.
206 * <https://jasmine.github.io>