OSDN Git Service

Regular updates
[twpd/master.git] / jasmine.md
1 ---
2 title: Jasmine
3 category: JavaScript libraries
4 layout: 2017/sheet
5 weight: -1
6 ---
7
8 ## Tests
9
10 ### Writing tests
11
12 ```js
13 describe('A suite', () => {
14   it('works', () => {
15     expect(true).toBe(true)
16   })
17 })
18 ```
19
20 Note: This cheatsheet may be a little outdated. Also see the [Jest cheatsheet](./jest). Jest uses Jasmine, and therefore has similar API.
21
22 ### Expectations
23
24 ```js
25 expect(true).toBe(true)
26 expect(true).not.toBe(true)
27 ```
28
29 ```js
30 expect(a).toEqual(bar)
31 ```
32
33 ```js
34 expect(message).toMatch(/bar/)
35 expect(message).toMatch('bar')
36 ```
37
38 ```js
39 expect(a.foo).toBeDefined()
40 expect(a.foo).toBeUndefined()
41 expect(a.foo).toBeNull()
42 ```
43
44 ```js
45 expect(a.foo).toBeTruthy()
46 expect(a.foo).toBeFalsy()
47 ```
48
49 ```js
50 expect(message).toContain('hello')
51 ```
52
53 ```js
54 expect(pi).toBeGreaterThan(3)
55 expect(pi).toBeLessThan(4)
56 expect(pi).toBeCloseTo(3.1415, 0.1)
57 ```
58
59 ```js
60 expect(func).toThrow()
61 ```
62
63 ### Hooks
64
65 ```js
66 beforeEach(() => {
67   ···
68 })
69 ```
70
71 ```js
72 afterEach(() => {
73   ···
74 })
75 ```
76
77 ### Pending
78
79 ```js
80 xit('this is a pending test', () => {
81   ···
82 })
83 ```
84
85 ```js
86 xdescribe('this is a pending block', () => {
87   ···
88 })
89 ```
90
91 ### Spies
92
93 ```js
94 spyOn(foo, 'setBar')
95 spyOn(foo, 'setBar').andReturn(123)
96 spyOn(foo, 'getBar').andCallFake(function() { return 1001; })
97 foo.setBar(123)
98 ```
99
100 ```js
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)
105 ```
106
107 ### Creating spies
108
109 ```js
110 stub = jasmine.createSpy('stub')
111 stub('hello')
112 ```
113
114 ```js
115 expect(stub.identity).toEqual('stub')
116 expect(stub).toHaveBeenCalled()
117 ```
118
119 ### Async
120
121 ```js
122 test('works with promises', () => {
123   return new Promise((resolve, reject) => {
124     ···
125   })
126 })
127 ```
128
129 Make your test return a promise.
130
131 ### HTML runner
132
133 ```js
134 var jasmineEnv = jasmine.getEnv()
135 jasmineEnv.updateInterval = 250
136
137 var htmlReporter = new jasmine.HtmlReporter()
138 jasmineEnv.addReporter(htmlReporter)
139
140 $(function() { jasmineEnv.execute() })
141 ```
142
143 Jasmine jQuery
144 --------------
145
146 ### Expectations
147
148 ```js
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()
154 ```
155
156 ```js
157 expect($('#toolbar')).toBeHidden()
158 expect($('#toolbar')).toBeVisible()
159 ```
160
161 ```js
162 expect($('#popup')).toHaveCss({ margin: "10px" })
163 expect($('option')).toBeSelected()
164 ```
165
166 ```js
167 expect($('.foo')).toExist()
168 ```
169
170 ```js
171 expect($('a')).toHaveAttr('rel')
172 expect($('a')).toHaveAttr('rel', 'nofollow')
173 ```
174
175 ```js
176 expect($('a')).toHaveClass('rel')
177 expect($('a')).toHaveId('home')
178 ```
179
180 ```js
181 expect($('a')).toHaveHtml('<span></span>')
182 expect($('a')).toContainHtml('<span></span>')
183 expect($('a')).toHaveText('hi')
184 ```
185
186 ```js
187 expect($form).toHandle('submit') // event
188 expect($form).toHandleWith('submit', onSumbit)
189 ```
190
191 See: [jasmine-jquery](https://github.com/velesin/jasmine-jquery)
192
193 ### Event spies
194
195 ```js
196 spyOnEvent($('#some_element'), 'click')
197 $('#some_element').click()
198 expect('click').toHaveBeenPreventedOn($('#some_element'))
199 expect('click').toHaveBeenTriggeredOn($('#some_element'))
200 ```
201
202 ## References
203 {: .-one-column}
204
205 * Also see the [Jest cheatsheet](./jest). Jest uses Jasmine, and therefore has similar API.
206 * <https://jasmine.github.io>