3 <title>Expresso - TDD Framework For Node</title>
6 font: 13px/1.4 "Helvetica", "Lucida Grande", Arial, sans-serif;
12 padding: 14px 0 12px 0;
14 font-family: "Helvetica";
16 border-top: 1px solid rgba(0,0,0,0.2);
17 border-bottom: 1px solid rgba(0,0,0,0.2);
18 background: rgba(255,255,255,0.6) url(http://www.sencha.com/favicon.ico) no-repeat 15px 50%;
32 margin: 25px 0 15px 0;
37 border: 1px solid #eee;
42 <a href="http://github.com/visionmedia/expresso">
43 <img alt="Fork me on GitHub" id="ribbon" src="http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" />
45 <div id="header"><strong>Sencha</strong> labs</div>
49 <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>
51 <h2 id="Features">Features</h2>
55 <li>intuitive async support</li>
56 <li>intuitive test runner executable</li>
57 <li>test coverage support and reporting via <a href="http://github.com/visionmedia/node-jscoverage">node-jscoverage</a></li>
58 <li>uses and extends the core <em>assert</em> module</li>
59 <li><code>assert.eql()</code> alias of <code>assert.deepEqual()</code></li>
60 <li><code>assert.response()</code> http response utility</li>
61 <li><code>assert.includes()</code></li>
62 <li><code>assert.isNull()</code></li>
63 <li><code>assert.isUndefined()</code></li>
64 <li><code>assert.isNotNull()</code></li>
65 <li><code>assert.isDefined()</code></li>
66 <li><code>assert.match()</code></li>
67 <li><code>assert.length()</code></li>
71 <h2 id="Installation">Installation</h2>
73 <p>To install both expresso <em>and</em> node-jscoverage run
74 the command below, which will first compile node-jscoverage:</p>
76 <pre><code>$ make install
79 <p>To install expresso alone without coverage reporting run:</p>
81 <pre><code>$ make install-expresso
84 <p>Install via npm:</p>
86 <pre><code>$ npm install expresso
89 <h2 id="Examples">Examples</h2>
91 <h2 id="Examples">Examples</h2>
93 <p>To define tests we simply export several functions:</p>
95 <pre><code>exports['test String#length'] = function(assert){
96 assert.equal(6, 'foobar'.length);
100 <p>Alternatively for large numbers of tests you may want to
101 export your own object containing the tests, however this
102 is essentially the as above:</p>
104 <pre><code>module.exports = {
105 'test String#length': function(assert){
106 assert.equal(6, 'foobar'.length);
111 <p>If you prefer not to use quoted keys:</p>
113 <pre><code>exports.testsStringLength = function(assert){
114 assert.equal(6, 'foobar'.length);
118 <p>The second argument passed to each callback is <em>beforeExit</em>,
119 which is typically used to assert that callbacks have been
122 <pre><code>exports.testAsync = function(assert, beforeExit){
124 setTimeout(function(){
128 setTimeout(function(){
132 beforeExit(function(){
133 assert.equal(2, n, 'Ensure both timeouts are called');
138 <h2 id="Assert-Utilities">Assert Utilities</h2>
140 <h3 id="assert-isNull-val-msg-">assert.isNull(val[, msg])</h3>
142 <p>Asserts that the given <em>val</em> is <em>null</em>.</p>
144 <pre><code>assert.isNull(null);
147 <h3 id="assert-isNotNull-val-msg-">assert.isNotNull(val[, msg])</h3>
149 <p>Asserts that the given <em>val</em> is not <em>null</em>.</p>
151 <pre><code>assert.isNotNull(undefined);
152 assert.isNotNull(false);
155 <h3 id="assert-isUndefined-val-msg-">assert.isUndefined(val[, msg])</h3>
157 <p>Asserts that the given <em>val</em> is <em>undefined</em>.</p>
159 <pre><code>assert.isUndefined(undefined);
162 <h3 id="assert-isDefined-val-msg-">assert.isDefined(val[, msg])</h3>
164 <p>Asserts that the given <em>val</em> is not <em>undefined</em>.</p>
166 <pre><code>assert.isDefined(null);
167 assert.isDefined(false);
170 <h3 id="assert-match-str-regexp-msg-">assert.match(str, regexp[, msg])</h3>
172 <p>Asserts that the given <em>str</em> matches <em>regexp</em>.</p>
174 <pre><code>assert.match('foobar', /^foo(bar)?/);
175 assert.match('foo', /^foo(bar)?/);
178 <h3 id="assert-length-val-n-msg-">assert.length(val, n[, msg])</h3>
180 <p>Assert that the given <em>val</em> has a length of <em>n</em>.</p>
182 <pre><code>assert.length([1,2,3], 3);
183 assert.length('foo', 3);
186 <h3 id="assert-type-obj-type-msg-">assert.type(obj, type[, msg])</h3>
188 <p>Assert that the given <em>obj</em> is typeof <em>type</em>.</p>
190 <pre><code>assert.type(3, 'number');
193 <h3 id="assert-eql-a-b-msg-">assert.eql(a, b[, msg])</h3>
195 <p>Assert that object <em>b</em> is equal to object <em>a</em>. This is an
196 alias for the core <em>assert.deepEqual()</em> method which does complex
197 comparisons, opposed to <em>assert.equal()</em> which uses <em>==</em>.</p>
199 <pre><code>assert.eql('foo', 'foo');
200 assert.eql([1,2], [1,2]);
201 assert.eql({ foo: 'bar' }, { foo: 'bar' });
204 <h3 id="assert-includes-obj-val-msg-">assert.includes(obj, val[, msg])</h3>
206 <p>Assert that <em>obj</em> is within <em>val</em>. This method supports <em>Array_s
207 and </em>Strings_s.</p>
209 <pre><code>assert.includes([1,2,3], 3);
210 assert.includes('foobar', 'foo');
211 assert.includes('foobar', 'bar');
214 <h3 id="assert-response-server-req-res-fn-msg-fn-">assert.response(server, req, res|fn[, msg|fn])</h3>
216 <p>Performs assertions on the given <em>server</em>, which should <em>not</em> call
217 listen(), as this is handled internally by expresso and the server
218 is killed after all responses have completed. This method works with
219 any <em>http.Server</em> instance, so <em>Connect</em> and <em>Express</em> servers will work
222 <p>The <em>req</em> object may contain:</p>
225 <li><em>url</em> request url</li>
226 <li><em>timeout</em> timeout in milliseconds</li>
227 <li><em>method</em> HTTP method</li>
228 <li><em>data</em> request body</li>
229 <li><em>headers</em> headers object</li>
233 <p>The <em>res</em> object may be a callback function which
234 receives the response for assertions, or an object
235 which is then used to perform several assertions
236 on the response with the following properties:</p>
239 <li><em>body</em> assert response body</li>
240 <li><em>status</em> assert response status code</li>
241 <li><em>header</em> assert that all given headers match (unspecified are ignored)</li>
245 <p>When providing <em>res</em> you may then also pass a callback function
246 as the fourth argument for additional assertions.</p>
248 <p>Below are some examples:</p>
250 <pre><code>assert.response(server, {
251 url: '/', timeout: 500
256 assert.response(server, {
260 body: '{"name":"tj"}',
263 'Content-Type': 'application/json; charset=utf8',
268 assert.response(server, {
273 body: '/foo bar baz',
277 assert.response(server, {
282 body: '/foo bar baz',
285 // All done, do some more tests if needed
288 assert.response(server, {
291 assert.ok(res.body.indexOf('tj') >= 0, 'Test assert.response() callback');
295 <h2 id="expresso-1-">expresso(1)</h2>
297 <p>To run a single test suite (file) run:</p>
299 <pre><code>$ expresso test/a.test.js
302 <p>To run several suites we may simply append another:</p>
304 <pre><code>$ expresso test/a.test.js test/b.test.js
307 <p>We can also pass a whitelist of tests to run within all suites:</p>
309 <pre><code>$ expresso --only "foo()" --only "bar()"
312 <p>Or several with one call:</p>
314 <pre><code>$ expresso --only "foo(), bar()"
317 <p>Globbing is of course possible as well:</p>
319 <pre><code>$ expresso test/*
322 <p>When expresso is called without any files, <em>test/*</em> is the default,
323 so the following is equivalent to the command above:</p>
325 <pre><code>$ expresso
328 <p>If you wish to unshift a path to <code>require.paths</code> before
329 running tests, you may use the <code>-I</code> or <code>--include</code> flag.</p>
331 <pre><code>$ expresso --include lib test/*
334 <p>The previous example is typically what I would recommend, since expresso
335 supports test coverage via <a href="http://github.com/visionmedia/node-jscoverage">node-jscoverage</a> (bundled with expresso),
336 so you will need to expose an instrumented version of you library.</p>
338 <p>To instrument your library, simply run <a href="http://github.com/visionmedia/node-jscoverage">node-jscoverage</a>,
339 passing the <em>src</em> and <em>dest</em> directories:</p>
341 <pre><code>$ node-jscoverage lib lib-cov
344 <p>Now we can run our tests again, using the <em>lib-cov</em> directory that has been
345 instrumented with coverage statements:</p>
347 <pre><code>$ expresso -I lib-cov test/*
350 <p>The output will look similar to below, depending on your test coverage of course :)</p>
352 <p><img src="http://dl.dropbox.com/u/6396913/cov.png" alt="node coverage" /></p>
354 <p>To make this process easier expresso has the <em>-c</em> or <em>--cov</em> which essentially
355 does the same as the two commands above. The following two commands will
356 run the same tests, however one will auto-instrument, and unshift <em>lib-cov</em>,
357 and the other will run tests normally:</p>
359 <pre><code>$ expresso -I lib test/*
360 $ expresso -I lib --cov test/*
363 <p>Currently coverage is bound to the <em>lib</em> directory, however in the
364 future <code>--cov</code> will most likely accept a path.</p>
366 <h2 id="Async-Exports">Async Exports</h2>
368 <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>
370 <pre><code>setTimeout(function(){
371 exports['test async exports'] = function(assert){