-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
index.html
377 lines (280 loc) · 11.9 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<title>Mocha - the fun, simple, flexible JavaScript test framework</title>
<link rel="stylesheet" href="style.css" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script>
onload = function(){
document.body.setAttribute('class', 'onload');
};
</script>
</head>
<body>
<h1><a href="http://github.com/visionmedia/mocha">Mocha</a></h1>
<p id="tag"><em>simple</em>, <em>flexible</em>, <em>fun</em></p>
<p>Mocha is a feature-rich JavaScript test framework running on <a href="http://nodejs.org">node</a> and the browser, aiming to make async testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases.</p>
<h2>Features</h2>
<ul>
<li>proper exit status for CI support etc</li>
<li>ideal for asynchronous APIs</li>
<li>auto-detects and disables coloring for non-ttys</li>
<li>maps uncaught exceptions to the correct test case</li>
<li>async test timeout support</li>
<li>growl notification support</li>
<li>reports test durations</li>
<li>highlights slow tests</li>
<li>global variable leak detection</li>
<li>configurable test-case timeout</li>
<li>optionally run tests that match a regexp</li>
<li>auto-exit to prevent “hanging” with an active loop</li>
<li>easily meta-generate suites & test-cases</li>
<li>mocha.opts file support</li>
<li><code>mocha-debug(1)</code> for node debugger support</li>
<li>detects multiple calls to <code>done()</code></li>
<li>use any assertion library you want</li>
<li>extensible reporting, bundled with 9+ reporters</li>
<li>extensible test DSLs or “interfaces”</li>
<li>before, after, before each, after each hooks</li>
<li>TextMate bundle</li>
</ul>
<h2>Installation</h2>
<p> Install with <a href="http://npmjs.org">npm</a>:</p>
<pre><code>$ npm install -g mocha
</code></pre>
<h2>Assertions</h2>
<p>Mocha allows you to use any assertion library you want, if it throws an error, it will work! This means you can utilize libraries such as <a href="http://github.com/visionmedia/should.js">should.js</a>, node’s regular <code>assert</code> module, or others.</p>
<h2>Synchronous code</h2>
<p> When testing synchronous code, omit the callback and Mocha will automatically continue on to the next test.</p>
<pre><code>describe('Array', function(){
describe('#indexOf()', function(){
it('should return -1 when the value is not present', function(){
[1,2,3].indexOf(5).should.equal(-1);
[1,2,3].indexOf(0).should.equal(-1);
})
})
})
</code></pre>
<h2>Asynchronous code</h2>
<p>Testing asynchronous code with Mocha could not be simpler! Simply invoke the callback when your test is complete:</p>
<pre><code>describe('User', function(){
describe('#save()', function(){
it('should save without error', function(done){
var user = new User('Luna');
user.save(function(err){
if (err) throw err;
done();
});
})
})
})
</code></pre>
<p> To make things even easier, the <code>done()</code> callback accepts an error, so we may use this directly:</p>
<pre><code>describe('User', function(){
describe('#save()', function(){
it('should save without error', function(done){
var user = new User('Luna');
user.save(done);
})
})
})
</code></pre>
<h2>Pending tests</h2>
<p> Pending test-cases are simply those without a callback:</p>
<pre><code>describe('Array', function(){
describe('#indexOf()', function(){
it('should return -1 when the value is not present')
})
})
</code></pre>
<h2>mocha(1)</h2>
<pre><code>Usage: mocha [options] [files]
Options:
-h, --help output usage information
-V, --version output the version number
-r, --require <name> require the given module
-R, --reporter <name> specify the reporter to use
-u, --ui <name> specify user-interface (bdd|tdd|exports)
-g, --grep <pattern> only run tests matching <pattern>
-t, --timeout <ms> set test-case timeout in milliseconds [2000]
-s, --slow <ms> "slow" test threshold in milliseconds [75]
-G, --growl enable growl support
</code></pre>
<h2>mocha-debug(1)</h2>
<p> <code>mocha-debug(1)</code> is identical to <code>mocha(1)</code>, however it enables node’s debugger so you may step through tests with the <strong>debugger</strong> statement.</p>
<h2>Interfaces</h2>
<p> Mocha “interface” system allows developers to choose their style of DSL. Shipping with <strong>BDD</strong>, <strong>TDD</strong>, and <strong>export</strong> flavoured interfaces.</p>
<h3>BDD</h3>
<p> The “<strong>BDD</strong>” interface provides <code>describe()</code>, <code>it()</code>, <code>before()</code>, <code>after()</code>, <code>beforeEach()</code>, and <code>afterEach()</code>:</p>
<pre><code>describe('Array', function(){
before(function(){
// ...
});
describe('#indexOf()', function(){
it('should return -1 when not present', function(){
[1,2,3].indexOf(4).should.equal(-1);
});
});
});
</code></pre>
<h3>TDD</h3>
<p> The “<strong>TDD</strong>” interface provides <code>suite()</code>, <code>test()</code>, <code>setup()</code>, and <code>teardown()</code>.</p>
<pre><code>suite('Array', function(){
setup(function(){
// ...
});
suite('#indexOf()', function(){
test('should return -1 when not present', function(){
assert.equal(-1, [1,2,3].indexOf(4));
});
});
});
</code></pre>
<h3>Exports</h3>
<p> The “<strong>exports</strong>” interface is much like Mocha’s predecessor <a href="http://github.com/visionmedia/expresso">expresso</a>. The keys <code>before</code>, <code>after</code>, <code>beforeEach</code>, and <code>afterEach</code> are special-cased, object values
are suites, and function values are test-cases.</p>
<pre><code>module.exports = {
before: function(){
// ...
},
'Array': {
'#indexOf()': {
'should return -1 when not present': function(){
[1,2,3].indexOf(4).should.equal(-1);
}
}
}
};
</code></pre>
<h2>Reporters</h2>
<p> Mocha reporters adjust to the terminal window,
and always disable ansi-escape colouring when
the stdio streams are not associated with a tty.</p>
<h3>Dot Matrix</h3>
<p> The Dot Matrix reporter is simply a series of dots
that represent test cases, failures highlight in red.</p>
<p> <img src="http://f.cl.ly/items/3b3b471Z1p2U3D1P2Y1n/Screenshot.png" alt="dot matrix reporter" /></p>
<p> <img src="http://f.cl.ly/items/1P11330L033r423g1y1n/Screenshot.png" alt="dot matrix failure" /></p>
<h2>TAP</h2>
<p> The TAP reporter emits lines for a <a href="http://en.wikipedia.org/wiki/Test_Anything_Protocol">Test-Anything-Protocol</a> consumer.</p>
<p> <img src="http://f.cl.ly/items/2O0X3h0d1Q430O1t1T3p/Screenshot.png" alt="test anything protocol" /></p>
<h2>Landing Strip</h2>
<p> The Landing Strip reporter is a gimmicky test reporter simulating
a plane landing :) unicode ftw</p>
<p> <img src="http://f.cl.ly/items/0z1k400K1N1Y2G3u2u0i/Screenshot.png" alt="landing strip plane reporter" /></p>
<h2>List</h2>
<p> The “List” reporter outputs a simple specifications list as
test cases pass or fail, outputting the failure details at
the bottom of the output.</p>
<p> <img src="http://f.cl.ly/items/0Y0x1B3l3K0n3t3h3l0p/Screenshot.png" alt="list reporter" /></p>
<p> <img src="http://f.cl.ly/items/2Z0E150v20042G2d1J0i/Screenshot.png" alt="failures" /></p>
<h2>JSON</h2>
<p> The JSON reporter outputs a single large JSON object when
the tests have completed (failures or not).</p>
<h2>JSON Stream</h2>
<p> The JSON Stream reporter outputs newline-delimited JSON “events” as they occur, beginning with a “start” event, followed by test passes or failures, and then the final “end” event.</p>
<pre><code>["start",{"total":12}]
["pass",{"title":"should return -1 when not present","fullTitle":"Array #indexOf() should return -1 when not present","duration":0}]
["pass",{"title":"should return the index when present","fullTitle":"Array #indexOf() should return the index when present","duration":0}]
["fail",{"title":"should return -1 when not present","fullTitle":"Array #indexOf() should return -1 when not present"}]
["end",{"start":"2011-08-29T03:21:02.050Z","suites":13,"passes":11,"tests":12,"failures":1,"end":"2011-08-29T03:21:02.052Z","duration":2}]
</code></pre>
<h2>Doc</h2>
<p> The “doc” reporter outputs a hierarchical HTML body representation
of your tests, wrap it with a header, footer, some styling and you
have some fantastic documentation!</p>
<p> For example suppose you have the following JavaScript:</p>
<pre><code>describe('Array', function(){
describe('#indexOf()', function(){
it('should return -1 when the value is not present', function(){
[1,2,3].indexOf(5).should.equal(-1);
[1,2,3].indexOf(0).should.equal(-1);
})
})
})
</code></pre>
<p> The command <code>mocha --reporter doc array</code> would yield:</p>
<pre><code><section class="suite">
<h1>Array</h1>
<dl>
<section class="suite">
<h1>#indexOf()</h1>
<dl>
<dt>should return -1 when the value is not present</dt>
<dd><pre><code>[1,2,3].indexOf(5).should.equal(-1);
[1,2,3].indexOf(0).should.equal(-1);</code></pre></dd>
</dl>
</section>
</dl>
</section>
</code></pre>
<h3>mocha.opts</h3>
<p> Mocha will attempt to load <code>./test/mocha.opts</code>, these are concatenated with <code>process.argv</code>, though command-line args will take precedence. For example suppose you have the following <em>mocha.opts</em> file:</p>
<pre><code>--require should
--reporter dot
--ui bdd
</code></pre>
<p> This will default the reporter to <code>dot</code>, require the <code>should</code> library,
and use <code>bdd</code> as the interface. With this you may then invoke <code>mocha(1)</code>
with additional arguments, here enabling growl support and changing
the reporter to <code>spec</code>:</p>
<pre><code>$ mocha --reporter list --growl
</code></pre>
<h3>Suite merging</h3>
<p> Suites with common names are “merged” in order
to produce unified reporting, especially when
meta-generating tests.</p>
<pre><code>describe('merge', function(){
describe('stuff', function(){
describe('one', function(){
it('should do something', function(){})
})
})
})
describe('merge', function(){
describe('stuff', function(){
describe('two', function(){
it('should do something', function(){})
})
})
})
describe('merge stuff', function(){
describe('three', function(){
it('should do something', function(){})
})
})
</code></pre>
<p>will produce the following:</p>
<p> <img src="http://f.cl.ly/items/380R3S1t1t0b0O2K250V/Screenshot.png" alt="mocha suite merging" /></p>
<h2>Best practices</h2>
<h3>test/*</h3>
<p> By default <code>mocha(1)</code> will use the pattern <code>./test/*.js</code>, so
it’s usually a good place to put your tests.</p>
<h3>Makefiles</h3>
<p> Be kind and don’t make developers hunt around in your docs to figure
out how to run the tests, add a <code>make test</code> target to your <em>Makefile</em>:</p>
<pre><code> test:
./node_modules/.bin/mocha \
--reporter list
.PHONY: test
</code></pre>
<h2>Editors</h2>
<p> The following editor-related packages are available:</p>
<h3>TextMate bundle</h3>
<p> The Mocha TextMate bundle includes snippets to
make writing tests quicker and more enjoyable.
To install the bundle run:</p>
<pre><code> $ make tm
</code></pre>
<h2>Running mocha’s tests</h2>
<p> Run the tests:</p>
<pre><code> $ make test
</code></pre>
<p> Run all tests, including interfaces:</p>
<pre><code> $ make test-all
</code></pre>
<p> Alter the reporter:</p>
<pre><code> $ make test REPORTER=list
</code></pre>
</body>
</html>