3 <title>Expresso - TDD Framework For Node</title>
6 font: 13px/1.4 "Helvetica", "Lucida Grande", Arial, sans-serif;
22 margin: 25px 0 15px 0;
30 border: 1px solid #eee;
38 <a href="http://github.com/visionmedia/expresso">
39 <img alt="Fork me on GitHub" id="ribbon" src="http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" />
44 <h2 id="NAME">NAME</h2>
48 <p><a href="http://github.com/visionmedia/expresso">Expresso</a> is a JavaScript <a href="http://en.wikipedia.org/wiki/Test-driven_development">TDD</a> framework written for <a href="http://nodejs.org">nodejs</a>. Expresso is extremely fast, and is packed with features such as additional assertion methods, code coverage reporting, CI support, and more.</p>
50 <h2 id="Features">Features</h2>
54 <li>intuitive async support</li>
55 <li>intuitive test runner executable</li>
56 <li>test coverage support and reporting via <a href="http://github.com/visionmedia/node-jscoverage">node-jscoverage</a></li>
57 <li>uses and extends the core <em>assert</em> module</li>
58 <li><code>assert.eql()</code> alias of <code>assert.deepEqual()</code></li>
59 <li><code>assert.response()</code> http response utility</li>
60 <li><code>assert.includes()</code></li>
61 <li><code>assert.isNull()</code></li>
62 <li><code>assert.isUndefined()</code></li>
63 <li><code>assert.isNotNull()</code></li>
64 <li><code>assert.isDefined()</code></li>
65 <li><code>assert.match()</code></li>
66 <li><code>assert.length()</code></li>
70 <h2 id="Installation">Installation</h2>
72 <p>To install both expresso <em>and</em> node-jscoverage run
73 the command below, which will first compile node-jscoverage:</p>
75 <pre><code>$ make install
78 <p>To install expresso alone without coverage reporting run:</p>
80 <pre><code>$ make install-expresso
83 <p>Install via npm:</p>
85 <pre><code>$ npm install expresso
88 <h2 id="Examples">Examples</h2>
90 <p>To define tests we simply export several functions:</p>
92 <pre><code>exports['test String#length'] = function(){
93 assert.equal(6, 'foobar'.length);
97 <p>Alternatively for large numbers of tests you may want to
98 export your own object containing the tests, however this
99 is essentially the as above:</p>
101 <pre><code>module.exports = {
102 'test String#length': function(){
103 assert.equal(6, 'foobar'.length);
108 <p>If you prefer not to use quoted keys:</p>
110 <pre><code>exports.testsStringLength = function(){
111 assert.equal(6, 'foobar'.length);
115 <p>The argument passed to each callback is <em>beforeExit</em>,
116 which is typically used to assert that callbacks have been
119 <pre><code>exports.testAsync = function(beforeExit){
121 setTimeout(function(){
125 setTimeout(function(){
129 beforeExit(function(){
130 assert.equal(2, n, 'Ensure both timeouts are called');
135 <h2 id="Assert-Utilities">Assert Utilities</h2>
137 <h3 id="assert-isNull-val-msg-">assert.isNull(val[, msg])</h3>
139 <p>Asserts that the given <em>val</em> is <em>null</em>.</p>
141 <pre><code>assert.isNull(null);
144 <h3 id="assert-isNotNull-val-msg-">assert.isNotNull(val[, msg])</h3>
146 <p>Asserts that the given <em>val</em> is not <em>null</em>.</p>
148 <pre><code>assert.isNotNull(undefined);
149 assert.isNotNull(false);
152 <h3 id="assert-isUndefined-val-msg-">assert.isUndefined(val[, msg])</h3>
154 <p>Asserts that the given <em>val</em> is <em>undefined</em>.</p>
156 <pre><code>assert.isUndefined(undefined);
159 <h3 id="assert-isDefined-val-msg-">assert.isDefined(val[, msg])</h3>
161 <p>Asserts that the given <em>val</em> is not <em>undefined</em>.</p>
163 <pre><code>assert.isDefined(null);
164 assert.isDefined(false);
167 <h3 id="assert-match-str-regexp-msg-">assert.match(str, regexp[, msg])</h3>
169 <p>Asserts that the given <em>str</em> matches <em>regexp</em>.</p>
171 <pre><code>assert.match('foobar', /^foo(bar)?/);
172 assert.match('foo', /^foo(bar)?/);
175 <h3 id="assert-length-val-n-msg-">assert.length(val, n[, msg])</h3>
177 <p>Assert that the given <em>val</em> has a length of <em>n</em>.</p>
179 <pre><code>assert.length([1,2,3], 3);
180 assert.length('foo', 3);
183 <h3 id="assert-type-obj-type-msg-">assert.type(obj, type[, msg])</h3>
185 <p>Assert that the given <em>obj</em> is typeof <em>type</em>.</p>
187 <pre><code>assert.type(3, 'number');
190 <h3 id="assert-eql-a-b-msg-">assert.eql(a, b[, msg])</h3>
192 <p>Assert that object <em>b</em> is equal to object <em>a</em>. This is an
193 alias for the core <em>assert.deepEqual()</em> method which does complex
194 comparisons, opposed to <em>assert.equal()</em> which uses <em>==</em>.</p>
196 <pre><code>assert.eql('foo', 'foo');
197 assert.eql([1,2], [1,2]);
198 assert.eql({ foo: 'bar' }, { foo: 'bar' });
201 <h3 id="assert-includes-obj-val-msg-">assert.includes(obj, val[, msg])</h3>
203 <p>Assert that <em>obj</em> is within <em>val</em>. This method supports <em>Array_s
204 and </em>Strings_s.</p>
206 <pre><code>assert.includes([1,2,3], 3);
207 assert.includes('foobar', 'foo');
208 assert.includes('foobar', 'bar');
211 <h3 id="assert-response-server-req-res-fn-msg-fn-">assert.response(server, req, res|fn[, msg|fn])</h3>
213 <p>Performs assertions on the given <em>server</em>, which should <em>not</em> call
214 listen(), as this is handled internally by expresso and the server
215 is killed after all responses have completed. This method works with
216 any <em>http.Server</em> instance, so <em>Connect</em> and <em>Express</em> servers will work
219 <p>The <em>req</em> object may contain:</p>
222 <li><em>url</em> request url</li>
223 <li><em>timeout</em> timeout in milliseconds</li>
224 <li><em>method</em> HTTP method</li>
225 <li><em>data</em> request body</li>
226 <li><em>headers</em> headers object</li>
230 <p>The <em>res</em> object may be a callback function which
231 receives the response for assertions, or an object
232 which is then used to perform several assertions
233 on the response with the following properties:</p>
236 <li><em>body</em> assert response body (regexp or string)</li>
237 <li><em>status</em> assert response status code</li>
238 <li><em>header</em> assert that all given headers match (unspecified are ignored, use a regexp or string)</li>
242 <p>When providing <em>res</em> you may then also pass a callback function
243 as the fourth argument for additional assertions.</p>
245 <p>Below are some examples:</p>
247 <pre><code>assert.response(server, {
248 url: '/', timeout: 500
253 assert.response(server, {
257 body: '{"name":"tj"}',
260 'Content-Type': 'application/json; charset=utf8',
265 assert.response(server, {
270 body: '/foo bar baz',
274 assert.response(server, {
279 body: '/foo bar baz',
282 // All done, do some more tests if needed
285 assert.response(server, {
288 assert.ok(res.body.indexOf('tj') >= 0, 'Test assert.response() callback');
292 <h2 id="expresso-1-">expresso(1)</h2>
294 <p>To run a single test suite (file) run:</p>
296 <pre><code>$ expresso test/a.test.js
299 <p>To run several suites we may simply append another:</p>
301 <pre><code>$ expresso test/a.test.js test/b.test.js
304 <p>We can also pass a whitelist of tests to run within all suites:</p>
306 <pre><code>$ expresso --only "foo()" --only "bar()"
309 <p>Or several with one call:</p>
311 <pre><code>$ expresso --only "foo(), bar()"
314 <p>Globbing is of course possible as well:</p>
316 <pre><code>$ expresso test/*
319 <p>When expresso is called without any files, <em>test/*</em> is the default,
320 so the following is equivalent to the command above:</p>
322 <pre><code>$ expresso
325 <p>If you wish to unshift a path to <code>require.paths</code> before
326 running tests, you may use the <code>-I</code> or <code>--include</code> flag.</p>
328 <pre><code>$ expresso --include lib test/*
331 <p>The previous example is typically what I would recommend, since expresso
332 supports test coverage via <a href="http://github.com/visionmedia/node-jscoverage">node-jscoverage</a> (bundled with expresso),
333 so you will need to expose an instrumented version of you library.</p>
335 <p>To instrument your library, simply run <a href="http://github.com/visionmedia/node-jscoverage">node-jscoverage</a>,
336 passing the <em>src</em> and <em>dest</em> directories:</p>
338 <pre><code>$ node-jscoverage lib lib-cov
341 <p>Now we can run our tests again, using the <em>lib-cov</em> directory that has been
342 instrumented with coverage statements:</p>
344 <pre><code>$ expresso -I lib-cov test/*
347 <p>The output will look similar to below, depending on your test coverage of course :)</p>
349 <p><img src="http://dl.dropbox.com/u/6396913/cov.png" alt="node coverage" /></p>
351 <p>To make this process easier expresso has the <em>-c</em> or <em>--cov</em> which essentially
352 does the same as the two commands above. The following two commands will
353 run the same tests, however one will auto-instrument, and unshift <em>lib-cov</em>,
354 and the other will run tests normally:</p>
356 <pre><code>$ expresso -I lib test/*
357 $ expresso -I lib --cov test/*
360 <p>Currently coverage is bound to the <em>lib</em> directory, however in the
361 future <code>--cov</code> will most likely accept a path.</p>
363 <h2 id="Async-Exports">Async Exports</h2>
365 <p>Sometimes it is useful to postpone running of tests until a callback or event has fired, currently the <em>exports.foo = function(){};</em> syntax is supported for this:</p>
367 <pre><code>setTimeout(function(){
368 exports['test async exports'] = function(){