aboutsummaryrefslogtreecommitdiffstats
path: root/tools/node_modules/expresso/docs/index.html
blob: 587f9331cf5129a61fd6736777c8bf672388d3ff (plain)
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
<html>
	<head>
		<title>Expresso - TDD Framework For Node</title>
		<style>
			body {
				font: 13px/1.4 "Helvetica", "Lucida Grande", Arial, sans-serif;
				text-align: center;
			}
			#ribbon {
				position: absolute;
				top: 0;
				right: 0;
				z-index: 10;
			}
			#wrapper {
			  margin: 0 auto;
				padding: 50px 80px;
				width: 700px;
				text-align: left;
			}
			h1, h2, h3 {
				margin: 25px 0 15px 0;
			}
			h1 {
			  font-size: 35px;
			}
			pre {
				margin: 0 5px;
				padding: 15px;
				border: 1px solid #eee;
			}
			a {
			  color: #00aaff;
			}
		</style>
	</head>
	<body>
		<a href="http://github.com/visionmedia/expresso">
			<img alt="Fork me on GitHub" id="ribbon" src="http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" />
		</a>
		<div id="wrapper">
			<h1>Expresso 0.9.0</h1>
<html><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><h2 id="Features">Features</h2><ul><li>light-weight</li><li>intuitive async support</li><li>intuitive test runner executable</li><li>test coverage support and reporting via <a href="http://github.com/visionmedia/node-jscoverage">node-jscoverage</a></li><li>uses and extends the core <code>assert</code> module</li><li><code>assert.eql()</code> alias of <code>assert.deepEqual()</code></li><li><code>assert.response()</code> http response utility</li><li><code>assert.includes()</code></li><li><code>assert.isNull()</code></li><li><code>assert.isUndefined()</code></li><li><code>assert.isNotNull()</code></li><li><code>assert.isDefined()</code></li><li><code>assert.match()</code></li><li><code>assert.length()</code></li></ul><h2 id="Installation">Installation</h2><p>To install both expresso <em>and</em> node-jscoverage run
the command below, which will first compile node-jscoverage:</p><pre><code>$ make install</code></pre><p>To install expresso alone without coverage reporting run:</p><pre><code>$ make install-expresso</code></pre><p>Install via npm:</p><pre><code>$ npm install expresso</code></pre><h2 id="Examples">Examples</h2><p>To define tests we simply export several functions:</p><pre><code>exports['test String#length'] = function(){
    assert.equal(6, 'foobar'.length);
};</code></pre><p>Alternatively for large numbers of tests you may want to
export your own object containing the tests, however this
is essentially the as above:</p><pre><code>module.exports = {
    'test String#length': function(beforeExit, assert) {
      assert.equal(6, 'foobar'.length);
    }
};</code></pre><p>If you prefer not to use quoted keys:</p><pre><code>exports.testsStringLength = function(beforeExit, assert) {
    assert.equal(6, 'foobar'.length);
};</code></pre><p>The argument passed to each callback is <code>beforeExit</code> and <code>assert</code>.
The context ("<code>this</code>") of each test function is a <em>Test</em> object. You can pass a function to <code>beforeExit</code> to make sure the assertions are run before the tests exit. This is can be used to verify that tests have indeed been run. <code>beforeExit</code> is a shortcut for listening to the <code>exit</code> event on <code>this</code>. The second parameter <code>assert</code> is the <code>assert</code> object localized to that test. It makes sure that assertions in asynchronous callbacks are associated with the correct test.</p><pre><code>exports.testAsync = function(beforeExit, assert) {
    var n = 0;
    setTimeout(function() {
        ++n;
        assert.ok(true);
    }, 200);
    setTimeout(function() {
        ++n;
        assert.ok(true);
    }, 200);

    // When the tests are finished, the exit event is emitted.
    this.on('exit', function() {
        assert.equal(2, n, 'Ensure both timeouts are called');
    });

    // Alternatively, you can use the beforeExit shortcut.
    beforeExit(function() {
        assert.equal(2, n, 'Ensure both timeouts are called');
    });
};</code></pre><h2 id="Assert-Utilities">Assert Utilities</h2><h3 id="assert-isNull-val-msg">assert.isNull(val[, msg])</h3><p>Asserts that the given <code>val</code> is <code>null</code>.</p><pre><code>assert.isNull(null);</code></pre><h3 id="assert-isNotNull-val-msg">assert.isNotNull(val[, msg])</h3><p>Asserts that the given <code>val</code> is not <code>null</code>.</p><pre><code>assert.isNotNull(undefined);
assert.isNotNull(false);</code></pre><h3 id="assert-isUndefined-val-msg">assert.isUndefined(val[, msg])</h3><p>Asserts that the given <code>val</code> is <code>undefined</code>.</p><pre><code>assert.isUndefined(undefined);</code></pre><h3 id="assert-isDefined-val-msg">assert.isDefined(val[, msg])</h3><p>Asserts that the given <code>val</code> is not <code>undefined</code>.</p><pre><code>assert.isDefined(null);
assert.isDefined(false);</code></pre><h3 id="assert-match-str-regexp-msg">assert.match(str, regexp[, msg])</h3><p>Asserts that the given <code>str</code> matches <code>regexp</code>.</p><pre><code>assert.match('foobar', /^foo(bar)?/);
assert.match('foo', /^foo(bar)?/);</code></pre><h3 id="assert-length-val-n-msg">assert.length(val, n[, msg])</h3><p>Assert that the given <code>val</code> has a length of <code>n</code>.</p><pre><code>assert.length([1,2,3], 3);
assert.length('foo', 3);</code></pre><h3 id="assert-type-obj-type-msg">assert.type(obj, type[, msg])</h3><p>Assert that the given <code>obj</code> is typeof <code>type</code>.</p><pre><code>assert.type(3, 'number');</code></pre><h3 id="assert-eql-a-b-msg">assert.eql(a, b[, msg])</h3><p>Assert that object <code>b</code> is equal to object <code>a</code>. This is an
alias for the core <code>assert.deepEqual()</code> method which does complex
comparisons, opposed to <code>assert.equal()</code> which uses <code>==</code>.</p><pre><code>assert.eql('foo', 'foo');
assert.eql([1,2], [1,2]);
assert.eql({ foo: 'bar' }, { foo: 'bar' });</code></pre><h3 id="assert-includes-obj-val-msg">assert.includes(obj, val[, msg])</h3><p>Assert that <code>obj</code> is within <code>val</code>. This method supports <code>Array</code>s
and <code>Strings</code>s.</p><pre><code>assert.includes([1,2,3], 3);
assert.includes('foobar', 'foo');
assert.includes('foobar', 'bar');</code></pre><h3 id="assert-response-server-req-res-fn-msg-fn">assert.response(server, req, res|fn[, msg|fn])</h3><p>Performs assertions on the given <code>server</code>, which should <em>not</em> call
<code>listen()</code>, as this is handled internally by expresso and the server
is killed after all responses have completed. This method works with
any <code>http.Server</code> instance, so <em>Connect</em> and <em>Express</em> servers will work
as well.</p><p>The <strong><code>req</code></strong> object may contain:</p><ul><li><code>url</code>: request url</li><li><code>timeout</code>: timeout in milliseconds</li><li><code>method</code>: HTTP method</li><li><code>data</code>: request body</li><li><code>headers</code>: headers object</li></ul><p>The <strong><code>res</code></strong> object may be a callback function which
receives the response for assertions, or an object
which is then used to perform several assertions
on the response with the following properties:</p><ul><li><code>body</code>: assert response body (regexp or string)</li><li><code>status</code>: assert response status code</li><li><code>header</code>: assert that all given headers match (unspecified are ignored, use a regexp or string)</li></ul><p>When providing <code>res</code> you may then also pass a callback function
as the fourth argument for additional assertions.</p><p>Below are some examples:</p><pre><code>assert.response(server, {
    url: '/', timeout: 500
}, {
    body: 'foobar'
});

assert.response(server, {
    url: '/',
    method: 'GET'
}, {
    body: '{"name":"tj"}',
    status: 200,
    headers: {
        'Content-Type': 'application/json; charset=utf8',
        'X-Foo': 'bar'
    }
});

assert.response(server, {
    url: '/foo',
    method: 'POST',
    data: 'bar baz'
}, {
    body: '/foo bar baz',
    status: 200
}, 'Test POST');

assert.response(server, {
    url: '/foo',
    method: 'POST',
    data: 'bar baz'
}, {
    body: '/foo bar baz',
    status: 200
}, function(res){
    // All done, do some more tests if needed
});

assert.response(server, {
    url: '/'
}, function(res){
    assert.ok(res.body.indexOf('tj') &gt;= 0, 'Test assert.response() callback');
});</code></pre><p>This function will fail when it receives no response or when the timeout (default is 30 seconds) expires.</p><h2 id="expresso-1">expresso(1)</h2><p>To run a single test suite (file) run:</p><pre><code>$ expresso test/a.test.js</code></pre><p>To run several suites we may simply append another:</p><pre><code>$ expresso test/a.test.js test/b.test.js</code></pre><p>We can also pass a whitelist of tests to run within all suites:</p><pre><code>$ expresso --only "foo()" --only "bar()"</code></pre><p>Or several with one call:</p><pre><code>$ expresso --only "foo(), bar()"</code></pre><p>Globbing is of course possible as well:</p><pre><code>$ expresso test/*</code></pre><p>When expresso is called without any files, <em>test/*</em> is the default,
so the following is equivalent to the command above:</p><pre><code>$ expresso</code></pre><p>If you wish to unshift a path to <code>require.paths</code> before
running tests, you may use the <code>-I</code> or <code>--include</code> flag.</p><pre><code>$ expresso --include lib test/*</code></pre><p>The previous example is typically what I would recommend, since expresso
supports test coverage via <a href="http://github.com/visionmedia/node-jscoverage">node-jscoverage</a> (bundled with expresso),
so you will need to expose an instrumented version of you library.</p><p>To instrument your library, simply run <a href="http://github.com/visionmedia/node-jscoverage">node-jscoverage</a>,
passing the <em>src</em> and <em>dest</em> directories:</p><pre><code>$ node-jscoverage lib lib-cov</code></pre><p>Now we can run our tests again, using the <em>lib-cov</em> directory that has been
instrumented with coverage statements:</p><pre><code>$ expresso -I lib-cov test/*</code></pre><p>The output will look similar to below, depending on your test coverage of course :)</p><p><img alt="node coverage" src="http://dl.dropbox.com/u/6396913/cov.png" /></p><p>To make this process easier expresso has the <code>-c</code> or <code>--cov</code> which essentially
does the same as the two commands above. The following two commands will
run the same tests, however one will auto-instrument, and unshift <code>lib-cov</code>,
and the other will run tests normally:</p><pre><code>$ expresso -I lib test/*
$ expresso -I lib --cov test/*</code></pre><p>Currently coverage is bound to the <code>lib</code> directory, however in the
future <code>--cov</code> will most likely accept a path.</p><p>If you would like code coverage reports suitable for automated parsing, pass the <code>--json [output file]</code> option:</p><pre><code>$ expresso -I lib test/*
$ expresso -I lib --cov --json coverage.json test/*</code></pre><p>You should then see the json coverage details in the file you specified:</p><pre><code>{
    "LOC": 20,
    "SLOC": 7,
    "coverage": "71.43",
    "files": {
        "bar.js": {
            "LOC": 4,
            "SLOC": 2,
            "coverage": "100.00",
            "totalMisses": 0
        },
        "foo.js": {
            "LOC": 16,
            "SLOC": 5,
            "coverage": "60.00",
            "totalMisses": 2
        }
    },
    "totalMisses": 2
}</code></pre><h2 id="Async-Exports">Async Exports</h2><p>Sometimes it is useful to postpone running of tests until a callback or event has fired, currently the <code>exports.foo = function() {};</code> syntax is supported for this:</p><pre><code>setTimeout(function() {
    exports['test async exports'] = function(){
        assert.ok('wahoo');
    };
}, 100);</code></pre><p>Note that you only have one "shot" at exporting. You have to export all of your test functions in the same loop as the first one. That means you can't progressively add more test functions to the <code>exports</code> object.</p></html>
		</div>
	</body>
</html>