PageRenderTime 25ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules/expresso/docs/index.html

https://gitlab.com/stefan.pataky/tinyrpg
HTML | 174 lines | 168 code | 6 blank | 0 comment | 0 complexity | 96b9514e14f7b71e5b881513041955ee MD5 | raw file
  1. <html>
  2. <head>
  3. <title>Expresso - TDD Framework For Node</title>
  4. <style>
  5. body {
  6. font: 13px/1.4 "Helvetica", "Lucida Grande", Arial, sans-serif;
  7. text-align: center;
  8. }
  9. #ribbon {
  10. position: absolute;
  11. top: 0;
  12. right: 0;
  13. z-index: 10;
  14. }
  15. #wrapper {
  16. margin: 0 auto;
  17. padding: 50px 80px;
  18. width: 700px;
  19. text-align: left;
  20. }
  21. h1, h2, h3 {
  22. margin: 25px 0 15px 0;
  23. }
  24. h1 {
  25. font-size: 35px;
  26. }
  27. pre {
  28. margin: 0 5px;
  29. padding: 15px;
  30. border: 1px solid #eee;
  31. }
  32. a {
  33. color: #00aaff;
  34. }
  35. </style>
  36. </head>
  37. <body>
  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" />
  40. </a>
  41. <div id="wrapper">
  42. <h1>Expresso 0.9.0</h1>
  43. <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
  44. 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(){
  45. assert.equal(6, 'foobar'.length);
  46. };</code></pre><p>Alternatively for large numbers of tests you may want to
  47. export your own object containing the tests, however this
  48. is essentially the as above:</p><pre><code>module.exports = {
  49. 'test String#length': function(beforeExit, assert) {
  50. assert.equal(6, 'foobar'.length);
  51. }
  52. };</code></pre><p>If you prefer not to use quoted keys:</p><pre><code>exports.testsStringLength = function(beforeExit, assert) {
  53. assert.equal(6, 'foobar'.length);
  54. };</code></pre><p>The argument passed to each callback is <code>beforeExit</code> and <code>assert</code>.
  55. 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) {
  56. var n = 0;
  57. setTimeout(function() {
  58. ++n;
  59. assert.ok(true);
  60. }, 200);
  61. setTimeout(function() {
  62. ++n;
  63. assert.ok(true);
  64. }, 200);
  65. // When the tests are finished, the exit event is emitted.
  66. this.on('exit', function() {
  67. assert.equal(2, n, 'Ensure both timeouts are called');
  68. });
  69. // Alternatively, you can use the beforeExit shortcut.
  70. beforeExit(function() {
  71. assert.equal(2, n, 'Ensure both timeouts are called');
  72. });
  73. };</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);
  74. 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);
  75. 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)?/);
  76. 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);
  77. 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
  78. alias for the core <code>assert.deepEqual()</code> method which does complex
  79. comparisons, opposed to <code>assert.equal()</code> which uses <code>==</code>.</p><pre><code>assert.eql('foo', 'foo');
  80. assert.eql([1,2], [1,2]);
  81. 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
  82. and <code>Strings</code>s.</p><pre><code>assert.includes([1,2,3], 3);
  83. assert.includes('foobar', 'foo');
  84. 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
  85. <code>listen()</code>, as this is handled internally by expresso and the server
  86. is killed after all responses have completed. This method works with
  87. any <code>http.Server</code> instance, so <em>Connect</em> and <em>Express</em> servers will work
  88. 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
  89. receives the response for assertions, or an object
  90. which is then used to perform several assertions
  91. 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
  92. as the fourth argument for additional assertions.</p><p>Below are some examples:</p><pre><code>assert.response(server, {
  93. url: '/', timeout: 500
  94. }, {
  95. body: 'foobar'
  96. });
  97. assert.response(server, {
  98. url: '/',
  99. method: 'GET'
  100. }, {
  101. body: '{"name":"tj"}',
  102. status: 200,
  103. headers: {
  104. 'Content-Type': 'application/json; charset=utf8',
  105. 'X-Foo': 'bar'
  106. }
  107. });
  108. assert.response(server, {
  109. url: '/foo',
  110. method: 'POST',
  111. data: 'bar baz'
  112. }, {
  113. body: '/foo bar baz',
  114. status: 200
  115. }, 'Test POST');
  116. assert.response(server, {
  117. url: '/foo',
  118. method: 'POST',
  119. data: 'bar baz'
  120. }, {
  121. body: '/foo bar baz',
  122. status: 200
  123. }, function(res){
  124. // All done, do some more tests if needed
  125. });
  126. assert.response(server, {
  127. url: '/'
  128. }, function(res){
  129. assert.ok(res.body.indexOf('tj') &gt;= 0, 'Test assert.response() callback');
  130. });</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,
  131. 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
  132. 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
  133. supports test coverage via <a href="http://github.com/visionmedia/node-jscoverage">node-jscoverage</a> (bundled with expresso),
  134. 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>,
  135. 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
  136. 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
  137. does the same as the two commands above. The following two commands will
  138. run the same tests, however one will auto-instrument, and unshift <code>lib-cov</code>,
  139. and the other will run tests normally:</p><pre><code>$ expresso -I lib test/*
  140. $ expresso -I lib --cov test/*</code></pre><p>Currently coverage is bound to the <code>lib</code> directory, however in the
  141. 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/*
  142. $ 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>{
  143. "LOC": 20,
  144. "SLOC": 7,
  145. "coverage": "71.43",
  146. "files": {
  147. "bar.js": {
  148. "LOC": 4,
  149. "SLOC": 2,
  150. "coverage": "100.00",
  151. "totalMisses": 0
  152. },
  153. "foo.js": {
  154. "LOC": 16,
  155. "SLOC": 5,
  156. "coverage": "60.00",
  157. "totalMisses": 2
  158. }
  159. },
  160. "totalMisses": 2
  161. }</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() {
  162. exports['test async exports'] = function(){
  163. assert.ok('wahoo');
  164. };
  165. }, 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>
  166. </div>
  167. </body>
  168. </html>