-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.html
More file actions
413 lines (369 loc) · 13.7 KB
/
Copy pathjest.html
File metadata and controls
413 lines (369 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>React Testing with Jest</title>
<meta name="apple-mobile-web-app-capable" content="yes"/>
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"/>
<meta name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="reveal.js/css/reveal.css">
<!--<link rel="stylesheet" href="reveal.js/css/theme/white.css" id="theme">-->
<!--<link rel="stylesheet" href="reveal.js/css/theme/black.css" id="theme">-->
<!--<link rel="stylesheet" href="reveal.js/css/theme/night.css" id="theme">-->
<link rel="stylesheet" href="reveal.js/css/theme/simple.css" id="theme">
<!-- Code syntax highlighting -->
<link rel="stylesheet" href="reveal.js/lib/css/zenburn.css">
<style>
.right-img {
margin-left: 10px !important;
float: right;
height: 500px;
}
.todo:before {
content: 'TODO: ';
}
.todo {
color: red !important;
}
code span.line-number {
color: lightcoral;
}
.reveal pre code {
max-height: 1000px !important;
}
img {
border: 0 !important;
box-shadow: 0 0 0 0 !important;
}
</style>
<!-- Printing and PDF exports -->
<script>
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match(/print-pdf/gi) ? 'reveal.js/css/print/pdf.css' : 'reveal.js/css/print/paper.css';
document.getElementsByTagName('head')[0].appendChild(link);
</script>
<!--[if lt IE 9]>
<script src="reveal.js/lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h1>Jest Test Runner</h1>
<ul>
<li><a href="https://facebook.github.io/jest/">https://facebook.github.io/jest/</a></li>
<li>Enthält "alles": Test Runner, Assertions, Mock, Reporter, ...</li>
<li>Besonderheit: Snapshot Testing von React (und JSON Objekten)</li>
</ul>
</section>
<section>
<h2>Testen in JavaScript</h2>
<p>Zwei mögliche Stacks</p>
<table>
<thead>
<tr>
<th>Aufgabe</th>
<th>Mocha</th>
<th>Jest</th>
</tr>
</thead>
<tbody>
<tr>
<td>Runner</td>
<td>Mocha</td>
<td>Jest</td>
</tr>
<tr>
<td>Reporter</td>
<td>Mocha</td>
<td>Jest</td>
</tr>
<tr>
<td>Code coverage</td>
<td>Istanbul</td>
<td>Jest</td>
</tr>
<tr>
<td>Specs</td>
<td>Mocha</td>
<td>Jest</td>
</tr>
<tr>
<td>Assertions</td>
<td>Chai</td>
<td>Jest</td>
</tr>
<tr>
<td>Mock & Spy</td>
<td>Sinon</td>
<td>Jest</td>
</tr>
<tr>
<td>Rendern & Navigieren im DOM</td>
<td>Enzyme</td>
<td>Enzyme</td>
</tr>
</tbody>
</table>
</section>
<section>
<h2>Testcases</h2>
<ul>
<li><code>test</code> oder <code>it</code>
<pre><code data-trim contenteditable>test('it should work', () => {
expect('...').toBe('...);
});</code></pre>
<pre><code data-trim contenteditable>it('it should also work', () => {
expect('...').toBe('...);
});</code></pre>
</li>
<li class="fragment">Kann ein Promise für asynchrones testen zurückliefern:
<pre><code data-trim contenteditable>test('it should work', () => {
return someFunctionReturningAPromise()
.then( value => { expect(value).toBe('...') });
});</code></pre>
</li>
</ul>
</section>
<section>
<h2>Testsuites</h2>
<ul>
<li>Testsuites (optional, müssen nicht verwendet werden)
<pre><code data-trim contenteditable>describe('some feature', () => {
test(() => {...});
test(() => {...});
test(() => {...});
});</code></pre>
</li>
<li class="fragment">Überspringen (<code>skip</code>) / Auswählen (<code>only</code>) einzelner Tests:
<pre><code data-trim contenteditable>describe.only('some feature', () => { } );
describe.skip('another feature', () => { });
test.only('should work', () => { });
test.skip('does not work, skip for now', () => { });
</code></pre>
</li>
</ul>
</section>
<section>
<h2>Lifecycle</h2>
<ul>
<li>Einrichten der Testumgebung/-daten und Mocks</li>
<li>Aufräumen nach den Tests</li>
<li>Können global oder innerhalb einer Testsuite definiert werden</li>
<li>Können Promises zurückliefern</li>
</ul>
<pre><code data-trim contenteditable>beforeAll(() => {...});
beforeEach(() => {...});
afterEach(() => {...});
afterall(() => {...});
describe('...', () => {
beforeEach(...);
afterEach(...);
...
});
</code></pre>
</section>
<section>
<h2>Expectations und Matcher</h2>
<ul>
<li><a href="https://facebook.github.io/jest/docs/expect.html#content">https://facebook.github.io/jest/docs/expect.html#content</a>
</li>
<li class="fragment"><code>expect()</code> liefert eine Expectation zurück, auf der Matcher definiert
sind:
<pre><code data-trim contenteditable>expect(actual).toXyz(expected);
// for example:
expect("Hello Jest").toBe("Hello Jest"); // => ok
</code></pre>
</li>
<li class="fragment">Einige Matcher
<pre><code data-trim contenteditable>// Vergleich auf Identität
expect(actual).toBe(expected);
// Inhaltsvergleich:
expect(actual).toEqual(expected);
// true / false / null:
expect(actual).toBeTruthy();
expect(actual).toBeFalsy();
expect(actual).toBeNull();
// Länge (Array oder String)
expect(actual).toHaveLength(123);
</code></pre>
</li>
</ul>
</section>
<section>
<h2>Snapshot testing</h2>
<p>(Mit Enzyme)</p>
<pre><code data-trim contenteditable>import {shallow} from 'enzyme';
test('render correctly', () => {
// 1. shallow render component with Enzyme...
const component = shallow(<MyReactComponent />);
// 2. snapshot matching:
expect(component).toMatchSnapshot();
});
</code></pre>
<p class="fragment">Rendert die Komponente in ein Snapshot-File und vergleicht damit bei folgenden Testausführungen</p>
<p class="fragment"><b>Snapshot sagt nicht, ob UI richtig oder falsch gerendert wird, sondern nur, ob sie geändert wurde!</b></p>
</section>
<section>
<h2>Überblick: Snapshot testing</h2>
<ul>
<li class="fragment">Bei <b>erster</b> Ausführung: legt Snapshot-File in <code>__snapshot__</code> an
(<a href="images/jest-snapshot-file.png" target="_blank">Beispiel</a>)
</li>
<li class="fragment">Bei <b>folgenden</b> Ausführungen: erzeugt neuen Snapshot und vergleicht mit
gespeichtertem Snapshot
</li>
<li class="fragment">Wenn Snapshots unterschiedlich
<ul>
<li>Fehler samt Diff (<a href="images/jest-snapshot-diff.png" target="_blank">Beispiel</a>)</li>
<li>Im Watch Mode kann Snapshot aktualisiert werden
<img src="images/jest-snapshot-update.png"></li>
</ul>
</li>
<li class="fragment">Snapshotdateien werden in Git versioniert</li>
<li class="fragment">Für Enzyme wird Serializer benötigt: <a href="https://github.com/adriantoine/enzyme-to-json"
target="_blank">enzyme-to-json</a></li>
</ul>
</section>
<section>
<h2>Mock Funktionen</h2>
<ul>
<li class="fragment"><code>jest.fn()</code> erzeugt eine Mock-Funktion
<pre><code data-trim contenteditable>// Liefert undefined zurück, wenn ausgeführt
const aMockFn = jest.fn();
aMockFn(); // => undefined
</code></pre>
<li class="fragment">Implementierung der Mockfunktion kann als Parameter übergeben werden:
<pre><code data-trim contenteditable>
const aMockFn = jest.fn( param => `Hello, ${param}` );
console.log(aMockFn('World'));
// => Hello, World
</code></pre>
</li>
</ul>
</section>
<section>
<h2>Mock Funktionen</h2>
<li class="fragment">Aufrufe sicherstellen
<pre><code data-trim contenteditable>
const aMockFn = jest.fn( param => `Hello, ${param}` );
// use aMockFn somewhere in your code, then assert:
expect(aMockFn).toHaveBeenCalled();
expect(aMockFn).toHaveBeenCalledWith('World');
</code></pre>
</li>
<li class="fragment">Aufrufe: <code>fn.mock.calls</code>
<pre><code data-trim contenteditable>
const mockFn = jest.fn();
mockFn('Hello', 'World');
expect(mockFn.mock.calls).toHaveLength(1);
expect(mockFn.mock.calls[0].toBe(['Hello', 'World']);
</code></pre>
</li>
<li class="fragment">Zurücksetzen: Reset
<pre><code data-trim contenteditable>
const mockFn = jest.fn();
mockFn('Hello', 'World');
expect(mockFn.mock.calls).toHaveLength(1);
jest.restAllMocks(); // or: mockFn.mockReset();
expect(mockFn.mock.calls).toHaveLength(0);
</code></pre>
</li>
</section>
<section>
<h2>Mock modules</h2>
<li class="fragment">Ein Modul mocken:
<pre><code data-trim contenteditable>
// external module:
jest.mock('react-dom');
// internal module:
jest.mock('../../app/myDataService');
// alle Funktionen sind jetzt gemockt,
// "leere" Implementierung, return undefined)
</code></pre>
</li>
<li class="fragment">Implementierung für Mockfunktion:
<pre><code data-trim contenteditable>
jest.mock('../../app/myDataService', () => ({
loadData: jest.fn( () => ({data: 'mock data'}) );
});
console.log(myDataService.loadData());
// => {data: 'mock data'}
</code></pre>
</li>
</section>
<section>
<h2>Mock modules</h2>
<li class="fragment">Funktionen aus gemockten Modulen sind Jest Mock Funktionen
<pre><code data-trim contenteditable>
import myDataService from '../../app/myDataService';
jest.mock('../../app/myDataService');
// do something in code, that uses my dataservice
expect(myDataService).toHaveBeenCalled();
</code></pre>
</li>
</section>
</div>
</div>
<script src="reveal.js/lib/js/head.min.js"></script>
<script src="reveal.js/js/reveal.js"></script>
<script src="lib/jquery-2.2.4.js"></script>
<script>
Reveal.addEventListener('ready', function (event) {
if (window.location.hostname.indexOf('localhost') !== -1) {
// only applies to presentation version
Reveal.configure({controls: false});
} else {
// only applies to public version
$('.fragment').removeClass('fragment');
}
// applies to all versions
$('code').addClass('line-numbers');
});
</script>
<script>
// Full list of configuration options available at:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
transition: 'slide', // none/fade/slide/convex/concave/zoom
// Optional reveal.js plugins
dependencies: [
{
src: 'reveal.js/lib/js/classList.js', condition: function () {
return !document.body.classList;
}
},
{
src: 'reveal.js/plugin/markdown/marked.js', condition: function () {
return !!document.querySelector('[data-markdown]');
}
},
{
src: 'reveal.js/plugin/markdown/markdown.js', condition: function () {
return !!document.querySelector('[data-markdown]');
}
},
{
src: 'reveal.js/plugin/highlight/highlight.js', async: true, condition: function () {
return !!document.querySelector('pre code');
}, callback: function () {
hljs.initHighlightingOnLoad();
}
},
{src: 'reveal.js/plugin/zoom-js/zoom.js', async: true},
{src: 'reveal.js/plugin/notes/notes.js', async: true},
{src: 'lib/js/line-numbers.js'}
]
});
</script>
</body>
</html>