-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpresentation.html
552 lines (430 loc) · 11.1 KB
/
presentation.html
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
<!DOCTYPE html>
<html>
<head>
<title>JS Unit Testing with Zuul and Tape</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" href="static/styles.css" type="text/css"> </style>
</head>
<body>
<textarea id="source">
---
# Client-Side Unit Testing with Tape and Zuul
### Amadeus Junqueira
twitter: [@_4m4deus](https://twitter.com/_4m4deus)
blarg: [www.rippityrippity.me](http://rippityrippity.me/)
---
### Most examples use Browserify, the node-like module system for the browser
If you've worked with node you should be familiar with it
```js
// hey.js
module.exports = function hey() {
return 'heyyyyyyyyyyy';
}
// ya.js
module.exports = function ya() {
return 'yaaaaaaaaaa!';
};
// mega-hit.js
var lyric1 = require('./hey');
var lyric2 = require('./ya');
function multiNationalMegaHit() {
return lyric1() + ' ' + lyric2();
};
console.log(multiNationalMegaHit());
// heyyyyyyyyyyy yaaaaaaaaaa!
```
---
class: middle, center
# Obligatory _Why unit tests?_
### ‘Caffeine & intuition only carry you so far’ – Ben Alman
<p class="img-cont">
<img src="http://media4.giphy.com/media/lCeASPqq2UN8I/200.gif" />
</p>
---
### - Your code will (likely) improve
### - Regression testing, does my change break shit?
### - Do different browsers behave differently? (hint: _Duh!_)
<p class="img-cont">
<img src="http://media1.giphy.com/media/VDZFZRTmzt7cQ/200.gif" />
</p>
---
### - Discover the presence of a previously unknown bug
<p class="img-cont">
<img width="500" src="http://media2.giphy.com/media/8va99bCj63Kus/200.gif" />
</p>
---
class: middle, center
# Need a framework?
<p class="img-cont">
<img width="500" src="http://media4.giphy.com/media/ibOEGsIGd3jtS/200.gif" />
</p>
---
# Tape by substack
A tap-producing test harness for node and browsers. TAP (test anything protocol) is a standard for test output.
###- TAP allows for the dead simple integration of many consumers
###- Keeps testing simple
<p class="img-cont">
<img src="http://substack.net/images/substack.png" />
</p>
---
### Keep it simple, stupid
Why do I need `it`, `describe`, `beforeEach`, `afterEach` adding to my cognitive load!?
Mocha
```js
var assert = require("assert")
describe('truth', function(){
beforeEach(function(){ // set something up })
it('should find the truth', function(){
assert.equal(1, 1);
})
afterEach({ // tear something down })
})
```
Tape
```js
var test = require('tape');
test('equivalence', function(t) {
t.ok(1 === 1, 'these two numbers are equal');
t.end();
});
```
---
## Testing async code with Tape
All you _really_ need is `t.plan` or `t.end`
```js
var asyncModule = require('../lib/module');
test('async1', function(t) {
asyncModule(function(){
t.ok(true, 'callback was called!');
t.end();
})
});
test('async1', function(t) {
t.plan(1);
asyncModule(function(){
t.ok(true, 'callback was called!');
})
});
```
---
### Advantages of Tape:
1. It’s easier to understand
2. The tests are _just_ code: you can often run tests with `node test/test.js`
3. No need for some `test runner` binary that contains some of the code the test really needs
4. Powerful ecosystem of TAP test consumers
---
class: middle, center
# Making your code testable in javascript!
### Basic advice from a former testing-n00b
---
class: center, middle
## Mock your ajax tests
It's often a good idea to pass in external dependencies
---
Mock your ajax tests
```js
// Don't
var xhr = require('xhr');
module.exports = function myModule() {
xhr('/route', function(err){ ... });
};
// Do
var ok = require('assert').ok
module.exports = function myModule(xhr) {
ok(!!xhr, 'required: xhr');
xhr('/route', function(err, results){
return results;
});
};
// some-test.js
var test = require('tape');
var module = require('./my-module');
test('request completes', function(t) {
module(mockXhr);
function mockXhr(route, cb){
t.ok('done' === cb(null, 'done'), 'should return results');
t.end();
}
})
```
---
class: center, middle
## Throw your own errors
aka catch programmer errors! Your unit tests should cover this.
---
```js
var ok = require('assert').ok
module.exports = function someModule(opts) {
ok(!!opts, 'required: opts');
ok(!!opts.dao, 'required: .dao');
ok(!!opts.cb, 'required: .cb');
opts.dao.get({ specific: 'data' }, opts.cb);
}
// test.js
var module = require('./some-module.js');
var test = require('tape');
test('some module arguments accepted', function(t) {
try { module(null); }
catch (e) { t.ok(true, 'opts') }
try { module({}); }
catch (e) { t.ok(true, '.dao') }
try { module({ dao: {} }); }
catch (e) { t.ok(true, '.cb') }
});
```
---
class: center, middle
# Event handling
###- Move application logic from event handlers
###- Don't pass the event object around!
---
```js
module.exports = MyApp = {
handleClick: function(event) {
this.showPopup(event.clientX, event.clientY);
},
showPopup: function(x, y) {
var popup = document.getElementById('popup');
popup.style.left = x + 'px';
popup.style.top = y + 'px';
popup.className = 'reveal';
}
};
addListener(element, 'click', function(event) {
MyApp.handleClick(event); // this is okay
});
```
```js
// test.js
var app = require('./app')
var test = require('tape');
var popup = document.createElemnt('div');
popup.id = 'popup';
document.body.appendChild(popup);
test('show popup should add correct styles', function(t) {
app.showPopup(12, 24);
t.ok(popup.style.left === 12, 'left');
t.ok(popup.style.top === 24, 'top');
t.ok(popup.classList.contains('reveal'), 'classname');
});
```
###### Extended from `Maintainable Javascript` by Zakas
---
class: center, middle
## Testing Events with dom-events
`dom-events` is a cool library!
---
```js
module.exports = function component() {
var el = document.createElement('a');
a.href = '#';
el.classList.add('active');
addListener(el, 'click', function(e){
e.preventDefault();
toggle()
})
function toggle() { el.classList.toggle('active') }
return el;
}
// test.js
var component = require('./component');
var test = require('tape');
var dom = require('dom-events');
test('component should toggle class when clicked', function(t){
t.plan(1);
var el = component();
dom.emit(el, 'click')
ok(!ok.classList.contains('active'), 'no active class after click');
})
```
---
class: center, middle
# Keep it functional!
Input, output, test, repeat.
---
Keep it functional!
```js
// Don't.
module.exports = {
lyric: 'hey',
getLyric: function getLyric() {
return this.thaHook();
},
thaHook: function thaHook() {
return this.lyric + ' ya!';
}
}
//Do
module.exports = {
lyric: 'hey',
getLyric: function getLyric() {
return this.thaHook(this.lyric);
},
thaHook: function thaHook(lyric) {
return lyric + ' ya!';
}
}
// test.js
var lyric = require('./lyric');
var test = require('tape');
test('tha hook should be a multinational megahit', function(t) {
t.ok(lyric.thaHook('hell') === 'hell ya!', 'still probably a hit');
})
```
---
# Testability with module systems
<p class="img-cont">
<img style="width:100%" src='https://www.evernote.com/shard/s471/sh/6d64ac7f-f7f8-4390-bcf7-8736c0e02b46/c2525649460de075ec2e15a45f0c38c9/deep/0/Derick-Bailey-on-Twitter--"-autodoc--"Hey,-this-function-is-private-but-I-want-to-test-it-anyway."-...--sigh-NO".png' />
</p>
---
# A private function of great responsibility
```js
module.exports = React.createClass({
render: function render() {
var list = getFilteredList(this.props.lists, this.state);
return (
<div>{list}</div>
);
}
})
// SHIT this is reeeallly important I want to test it!
function getFilteredList(lists, state) {
var neither = !state.filterOne && !state.filterTwo;
if (neither) return lists.one.map(mapFn).concat(lists.two.map(mapFn));
else if (state.filterOne) return lists.one.map(mapFn);
else if (state.filterTwo) return lists.two.map(mapFn);
return [];
}
function mapFn(obj){ return obj.name; }
```
---
### If a private method's logic is complex and important, move it to it's own module and test it
Don't: expose private functions just to test them!
```js
module.exports = React.createClass({...})
module.exports._private = {
getFilteredList: getFilteredList
};
```
Do: create a new module!
```bash
$ touch get-filtered-list.js
$ vi get-filtered-list.js
```
```js
module.exports = function getFilteredList(lists, state) {
var neither = !state.filterOne && !state.filterTwo;
if (neither) return lists.one.map(mapFn).concat(lists.two.map(mapFn));
else if (state.filterOne) return lists.one.map(mapFn);
else if (state.filterTwo) return lists.two.map(mapFn);
return [];
}
```
---
class: center, middle
# A Node-centric testing workflow
aka almost done yall!
<p class="img-cont">
<img src="http://media3.giphy.com/media/TNQHoWzmW8h9K/200.gif" />
</p>
---
## The possibilities are endless. Unix pipe ftw
```bash
$ tape test/*.js # run all tests in this directory
```
```bash
$ browserify test/*.js > bundle.js | testling # run tests in browser with testling
```
```bash
$ browserify -t coverify test.js | testling | coverify # coverify will help you!
TAP version 13
# beep boop
ok 1 should be equal
1..1
# tests 1
# pass 1
# ok
# /tmp/example/test.js: line 7, column 16-28
if (err) deadCode();
^^^^^^^^^^^
```
---
# Zuul
"Don't just claim your js supports 'all browsers', prove it with tests!"
<p class="img-cont">
<img src="https://f.cloud.github.com/assets/71256/1669799/fb463296-5c81-11e3-818a-26776dc7a256.jpg" />
</p>
---
## Zuul Basics
Install
```bash
$ npm install zuul -g
```
Run tests locally in the browser. Helpful for debugging!
```bash
$ zuul --local 9000 -- ./test/*.js
open the following url in a browser:
http://localhost:9000/__zuul
```
Run tests in the headless browser phantomjs
```bash
$ zuul --phantomjs -- ./test/*.js
```
---
## Zuul + Saucelabs
```yaml
# app_directory/.zuul.yml
ui: tape
browserify:
- transform: reactify
browsers:
# your browsers here, for example:
- name: chrome
version: latest
```
```yaml
# ~/.zuulrc
sauce_username: username
sauce_key: sauce_key
```
<p class="img-cont">
<img width="400" src="https://www.evernote.com/shard/s471/sh/22ac43d5-3a5f-4588-8010-34213bf63ada/986ca65f91163f032d39559a96631b15/deep/0/substack-tape.png" />
</p>
---
# Putting it all together with npm!
A _super chill_ npm workflow
package.json
```json
{
...
"scripts": {
"postinstall": "npm test",
"test": "./node_modules/zuul/bin/zuul.js -- ./test/*.js",
"test-headless": "./node_modules/zuul/bin/zuul.js --phantomjs -- ./test/*.js",
"test-local": "./node_modules/zuul/bin/zuul.js --local 9000 -- ./test/*.js"
}
...
}
```
---
class: middle, center
# Thanks!
### Amadeus Junqueira
twitter: [@_4m4deus](https://twitter.com/_4m4deus)
blarg: [www.rippityrippity.me](http://rippityrippity.me/)
</textarea>
<script src="http://gnab.github.io/remark/downloads/remark-latest.min.js" type="text/javascript">
</script>
<script type="text/javascript">
var slideshow = remark.create({'highlightStyle': 'solarized_light'});
</script>
</body>
</html>