☕️ Classic, reliable, trusted test framework for Node.js and the browser
docs
Tj Holowaychuk8285c43cb41d
diff --git a/index.html b/index.htmlindex f092e9f5..08553a09 100644--- a/index.html+++ b/index.html@@ -4,12 +4,10 @@<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 src="jquery.js"></script>- <script src="highlight.js"></script><script>- $(function(){- $('body').addClass('onload');- })+ onload = function(){+ document.body.setAttribute('class', 'onload');+ };</script></head><body>@@ -32,33 +30,34 @@<li>highlights slow tests</li><li>global variable leak detection</li><li>optionally run tests that match a regexp</li>-<li>auto-exit to prevent "hanging" with an active loop</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>extensible test DSLs or “interfaces”</li><li>before, after, before each, after each hooks</li><li>TextMate bundle</li><li>and more!</li></ul>+<h2>Installation</h2>-<p>Install with <a href="http://npmjs.org">npm</a>:</p>+<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>+<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>+<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(){@@ -87,7 +86,7 @@})</code></pre>-<p>To make things even easier, the <code>done()</code> callback accepts an error, so we may use this directly:</p>+<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(){@@ -99,7 +98,7 @@})</code></pre>-<p>All "hooks", that is <code>before()</code>, <code>after()</code>, <code>beforeEach()</code>, <code>afterEach()</code> may be sync or async as well, behaving much like a regular test-case. For example you may wish to populate database with dummy content before each test:</p>+<p> All “hooks”, that is <code>before()</code>, <code>after()</code>, <code>beforeEach()</code>, <code>afterEach()</code> may be sync or async as well, behaving much like a regular test-case. For example you may wish to populate database with dummy content before each test:</p><pre><code>describe('Connection', function(){var db = new Connection@@ -128,7 +127,7 @@<h2>Pending tests</h2>-<p>Pending test-cases are simply those without a callback:</p>+<p> Pending test-cases are simply those without a callback:</p><pre><code>describe('Array', function(){describe('#indexOf()', function(){@@ -154,17 +153,48 @@ Options:-G, --growl enable growl support</code></pre>+<h3>-r, —require <name></h3>++<p> The <code>--require</code> option is useful for libraries such as <a href="http://github.com/visionmedia/should.js">should.js</a>, so you may simply <code>--require should</code> instead of manually invoking <code>require('should')</code> within each test file.</p>++<h3>-u, —ui <name></h3>++<p> The <code>--ui</code> option lets you specify the interface to use, defaulting to “bdd”.</p>++<h3>-R, —reporter <name></h3>++<p> The <code>--reporter</code> option allows you to specify the reporter that will be used, defaulting to “dot”.</p>++<h3>-g, —grep <pattern></h3>++<p> The <code>--grep</code> option when specified will trigger mocha to only run tests matching the given <code>pattern</code> which is internally compiled to a <code>RegExp</code>.</p>++<p> Suppose for example you have “api” related tests, as well as “app” related tests, as shown in the following snippet; One could use <code>--grep api</code> or <code>--grep app</code> to run one or the other. The same goes for any other part of a suite or test-case title, <code>--grep users</code> would be valid as well, or even <code>--grep GET</code>.</p>++<pre><code>describe('api', function(){+ describe('GET /api/users', function(){+ it('respond with an array of users')+ })+})++describe('app', function(){+ describe('GET /users', function(){+ it('respond with an array of users')+ })+})+</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>+<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>+<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>+<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(){@@ -181,7 +211,7 @@ Options:<h3>TDD</h3>-<p>The "<strong>TDD</strong>" interface provides <code>suite()</code>, <code>test()</code>, <code>setup()</code>, and <code>teardown()</code>.</p>+<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(){@@ -198,7 +228,7 @@ Options:<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+<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 valuesare suites, and function values are test-cases.</p><pre><code>module.exports = {@@ -218,62 +248,62 @@ Options:<h2>Reporters</h2>-<p>Mocha reporters adjust to the terminal window,+<p> Mocha reporters adjust to the terminal window,and always disable ansi-escape colouring whenthe stdio streams are not associated with a tty.</p><h3>Dot Matrix</h3>-<p>The Dot Matrix reporter is simply a series of dots+<p> The Dot Matrix reporter is simply a series of dotsthat represent test cases, failures highlight in red,pending in blue, slow as yellow.</p>-<p><img src="images/reporter-dot.png" alt="dot matrix reporter" title="" /></p>+<p> <img src="images/reporter-dot.png" alt="dot matrix reporter" /></p><h3>TAP</h3>-<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> 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" title="" /></p>+<p> <img src="http://f.cl.ly/items/2O0X3h0d1Q430O1t1T3p/Screenshot.png" alt="test anything protocol" /></p><h3>Landing Strip</h3>-<p>The Landing Strip reporter is a gimmicky test reporter simulating+<p> The Landing Strip reporter is a gimmicky test reporter simulatinga plane landing :) unicode ftw</p>-<p><img src="images/reporter-landing.png" alt="landing strip plane reporter" title="" />- <img src="images/reporter-landing-fail.png" alt="landing strip with failure" title="" /></p>+<p> <img src="images/reporter-landing.png" alt="landing strip plane reporter" />+ <img src="images/reporter-landing-fail.png" alt="landing strip with failure" /></p><h3>List</h3>-<p>The "List" reporter outputs a simple specifications list as- test cases pass or fail, outputting the failure details at+<p> The “List” reporter outputs a simple specifications list as+ test cases pass or fail, outputting the failure details atthe bottom of the output.</p>-<p><img src="images/reporter-list.png" alt="list reporter" title="" /></p>+<p> <img src="images/reporter-list.png" alt="list reporter" /></p><h3>JSON</h3>-<p>The JSON reporter outputs a single large JSON object when+<p> The JSON reporter outputs a single large JSON object whenthe tests have completed (failures or not).</p>-<p><img src="images/reporter-json.png" alt="json reporter" title="" /></p>+<p> <img src="images/reporter-json.png" alt="json reporter" /></p><h3>JSON Stream</h3>-<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>+<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>-<p><img src="images/reporter-json-stream.png" alt="json stream reporter" title="" /></p>+<p> <img src="images/reporter-json-stream.png" alt="json stream reporter" /></p><h3>Doc</h3>-<p>The "doc" reporter outputs a hierarchical HTML body representation+<p> The “doc” reporter outputs a hierarchical HTML body representationof your tests, wrap it with a header, footer, some styling and youhave some fantastic documentation!</p>-<p><img src="images/reporter-doc.png" alt="doc reporter" title="" /></p>+<p> <img src="images/reporter-doc.png" alt="doc reporter" /></p>-<p>For example suppose you have the following JavaScript:</p>+<p> For example suppose you have the following JavaScript:</p><pre><code>describe('Array', function(){describe('#indexOf()', function(){@@ -285,7 +315,7 @@ Options:})</code></pre>-<p>The command <code>mocha --reporter doc array</code> would yield:</p>+<p> The command <code>mocha --reporter doc array</code> would yield:</p><pre><code><section class="suite"><h1>Array</h1>@@ -304,14 +334,14 @@ Options:<h3>HTML</h3>-<p>The <strong>HTML</strong> reporter is currently the only browser reporter+<p> The <strong>HTML</strong> reporter is currently the only browser reportersupported by Mocha, and it looks like this:</p>-<p><img src="images/reporter-html.png" alt="HTML test reporter" title="" /></p>+<p> <img src="images/reporter-html.png" alt="HTML test reporter" /></p><h2>Browser support</h2>-<p>Mocha runs in the browser. Every release of Mocha will have new builds of <em>./mocha.js</em> and <em>./mocha.css</em> for use in the browser. To setup Mocha for browser use all you have to do is include the script, stylesheet, tell Mocha which interface you wish to use, and then run the tests. A typical setup might look something like the following, where we call <code>mocha.setup('bdd')</code> to use the <strong>BDD</strong> interface before loading the test scripts, running them <code>onload</code> with <code>mocha.run()</code>.</p>+<p> Mocha runs in the browser. Every release of Mocha will have new builds of <em>./mocha.js</em> and <em>./mocha.css</em> for use in the browser. To setup Mocha for browser use all you have to do is include the script, stylesheet, tell Mocha which interface you wish to use, and then run the tests. A typical setup might look something like the following, where we call <code>mocha.setup('bdd')</code> to use the <strong>BDD</strong> interface before loading the test scripts, running them <code>onload</code> with <code>mocha.run()</code>.</p><pre><code><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js" type="text/javascript"></script><link rel="stylesheet" href="style.css" />@@ -325,14 +355,14 @@ Options:<h2>mocha.opts</h2>-<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>+<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,+<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 changingthe reporter to <code>spec</code>:</p>@@ -342,7 +372,7 @@ Options:<h2>Suite merging</h2>-<p>Suites with common names are "merged" in order+<p> Suites with common names are “merged” in orderto produce unified reporting, especially whenmeta-generating tests.</p>@@ -369,20 +399,20 @@ describe('merge stuff', function(){})</code></pre>-<p>Instead of reporting these as distinct suites, they are merged, yielding the following: </p>+<p>Instead of reporting these as distinct suites, they are merged, yielding the following:</p>-<p><img src="http://f.cl.ly/items/380R3S1t1t0b0O2K250V/Screenshot.png" alt="mocha suite merging" title="" /></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>+<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+<p> Be kind and don’t make developers hunt around in your docs to figureout how to run the tests, add a <code>make test</code> target to your <em>Makefile</em>:</p><pre><code> test:@@ -394,11 +424,11 @@ describe('merge stuff', function(){<h2>Editors</h2>-<p>The following editor-related packages are available:</p>+<p> The following editor-related packages are available:</p><h3>TextMate bundle</h3>-<p>The Mocha TextMate bundle includes snippets to+<p> The Mocha TextMate bundle includes snippets tomake writing tests quicker and more enjoyable.To install the bundle run:</p>@@ -407,29 +437,30 @@ describe('merge stuff', function(){<h2>Example test suites</h2>-<p>The following test suites are from real projects putting Mocha to use,+<p> The following test suites are from real projects putting Mocha to use,so they serve as good examples:</p><ul>-<li><a href="https://github.com/visionmedia/express/tree/master/test">Express</a></li>-<li><a href="https://github.com/senchalabs/connect/tree/master/test">Connect</a></li>-<li><a href="https://github.com/visionmedia/superagent/tree/master/test/node">SuperAgent</a></li>-<li><a href="https://github.com/LearnBoost/websocket.io/tree/master/test">WebSocket.io</a></li>+<li> <a href="https://github.com/visionmedia/express/tree/master/test">Express</a></li>+<li> <a href="https://github.com/senchalabs/connect/tree/master/test">Connect</a></li>+<li> <a href="https://github.com/visionmedia/superagent/tree/master/test/node">SuperAgent</a></li>+<li> <a href="https://github.com/LearnBoost/websocket.io/tree/master/test">WebSocket.io</a></li></ul>-<h2>Running mocha's tests</h2>-<p>Run the tests:</p>+<h2>Running mocha’s tests</h2>++<p> Run the tests:</p><pre><code> $ make test</code></pre>-<p>Run all tests, including interfaces:</p>+<p> Run all tests, including interfaces:</p><pre><code> $ make test-all</code></pre>-<p>Alter the reporter:</p>+<p> Alter the reporter:</p><pre><code> $ make test REPORTER=list</code></pre>diff --git a/index.md b/index.mdindex 1d61a114..01d0c332 100644--- a/index.md+++ b/index.md@@ -128,6 +128,36 @@ Testing asynchronous code with Mocha could not be simpler! Simply invoke the cal-s, --slow <ms> "slow" test threshold in milliseconds [75]-G, --growl enable growl support+### -r, --require <name>++ The `--require` option is useful for libraries such as [should.js](http://github.com/visionmedia/should.js), so you may simply `--require should` instead of manually invoking `require('should')` within each test file.++### -u, --ui <name>++ The `--ui` option lets you specify the interface to use, defaulting to "bdd".++### -R, --reporter <name>++ The `--reporter` option allows you to specify the reporter that will be used, defaulting to "dot".++### -g, --grep <pattern>++ The `--grep` option when specified will trigger mocha to only run tests matching the given `pattern` which is internally compiled to a `RegExp`.++ Suppose for example you have "api" related tests, as well as "app" related tests, as shown in the following snippet; One could use `--grep api` or `--grep app` to run one or the other. The same goes for any other part of a suite or test-case title, `--grep users` would be valid as well, or even `--grep GET`.++ describe('api', function(){+ describe('GET /api/users', function(){+ it('respond with an array of users')+ })+ })++ describe('app', function(){+ describe('GET /users', function(){+ it('respond with an array of users')+ })+ })+## mocha-debug(1)`mocha-debug(1)` is identical to `mocha(1)`, however it enables node's debugger so you may step through tests with the __debugger__ statement.