PageRenderTime 467ms CodeModel.GetById 54ms RepoModel.GetById 0ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/expresso/docs/index.html

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
HTML | 377 lines | 286 code | 91 blank | 0 comment | 0 complexity | 3a1fc58058fbe5d204797387fad45297 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  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</h1>
  43. <div class='mp'>
  44. <h2 id="NAME">NAME</h2>
  45. <p class="man-name">
  46. <code>index</code>
  47. </p>
  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>
  49. <h2 id="Features">Features</h2>
  50. <ul>
  51. <li>light-weight</li>
  52. <li>intuitive async support</li>
  53. <li>intuitive test runner executable</li>
  54. <li>test coverage support and reporting via <a href="http://github.com/visionmedia/node-jscoverage">node-jscoverage</a></li>
  55. <li>uses and extends the core <em>assert</em> module</li>
  56. <li><code>assert.eql()</code> alias of <code>assert.deepEqual()</code></li>
  57. <li><code>assert.response()</code> http response utility</li>
  58. <li><code>assert.includes()</code></li>
  59. <li><code>assert.isNull()</code></li>
  60. <li><code>assert.isUndefined()</code></li>
  61. <li><code>assert.isNotNull()</code></li>
  62. <li><code>assert.isDefined()</code></li>
  63. <li><code>assert.match()</code></li>
  64. <li><code>assert.length()</code></li>
  65. </ul>
  66. <h2 id="Installation">Installation</h2>
  67. <p>To install both expresso <em>and</em> node-jscoverage run
  68. the command below, which will first compile node-jscoverage:</p>
  69. <pre><code>$ make install
  70. </code></pre>
  71. <p>To install expresso alone without coverage reporting run:</p>
  72. <pre><code>$ make install-expresso
  73. </code></pre>
  74. <p>Install via npm:</p>
  75. <pre><code>$ npm install expresso
  76. </code></pre>
  77. <h2 id="Examples">Examples</h2>
  78. <p>To define tests we simply export several functions:</p>
  79. <pre><code>exports['test String#length'] = function(){
  80. assert.equal(6, 'foobar'.length);
  81. };
  82. </code></pre>
  83. <p>Alternatively for large numbers of tests you may want to
  84. export your own object containing the tests, however this
  85. is essentially the as above:</p>
  86. <pre><code>module.exports = {
  87. 'test String#length': function(){
  88. assert.equal(6, 'foobar'.length);
  89. }
  90. };
  91. </code></pre>
  92. <p>If you prefer not to use quoted keys:</p>
  93. <pre><code>exports.testsStringLength = function(){
  94. assert.equal(6, 'foobar'.length);
  95. };
  96. </code></pre>
  97. <p>The argument passed to each callback is <em>beforeExit</em>,
  98. which is typically used to assert that callbacks have been
  99. invoked.</p>
  100. <pre><code>exports.testAsync = function(beforeExit){
  101. var n = 0;
  102. setTimeout(function(){
  103. ++n;
  104. assert.ok(true);
  105. }, 200);
  106. setTimeout(function(){
  107. ++n;
  108. assert.ok(true);
  109. }, 200);
  110. beforeExit(function(){
  111. assert.equal(2, n, 'Ensure both timeouts are called');
  112. });
  113. };
  114. </code></pre>
  115. <h2 id="Assert-Utilities">Assert Utilities</h2>
  116. <h3 id="assert-isNull-val-msg-">assert.isNull(val[, msg])</h3>
  117. <p>Asserts that the given <em>val</em> is <em>null</em>.</p>
  118. <pre><code>assert.isNull(null);
  119. </code></pre>
  120. <h3 id="assert-isNotNull-val-msg-">assert.isNotNull(val[, msg])</h3>
  121. <p>Asserts that the given <em>val</em> is not <em>null</em>.</p>
  122. <pre><code>assert.isNotNull(undefined);
  123. assert.isNotNull(false);
  124. </code></pre>
  125. <h3 id="assert-isUndefined-val-msg-">assert.isUndefined(val[, msg])</h3>
  126. <p>Asserts that the given <em>val</em> is <em>undefined</em>.</p>
  127. <pre><code>assert.isUndefined(undefined);
  128. </code></pre>
  129. <h3 id="assert-isDefined-val-msg-">assert.isDefined(val[, msg])</h3>
  130. <p>Asserts that the given <em>val</em> is not <em>undefined</em>.</p>
  131. <pre><code>assert.isDefined(null);
  132. assert.isDefined(false);
  133. </code></pre>
  134. <h3 id="assert-match-str-regexp-msg-">assert.match(str, regexp[, msg])</h3>
  135. <p>Asserts that the given <em>str</em> matches <em>regexp</em>.</p>
  136. <pre><code>assert.match('foobar', /^foo(bar)?/);
  137. assert.match('foo', /^foo(bar)?/);
  138. </code></pre>
  139. <h3 id="assert-length-val-n-msg-">assert.length(val, n[, msg])</h3>
  140. <p>Assert that the given <em>val</em> has a length of <em>n</em>.</p>
  141. <pre><code>assert.length([1,2,3], 3);
  142. assert.length('foo', 3);
  143. </code></pre>
  144. <h3 id="assert-type-obj-type-msg-">assert.type(obj, type[, msg])</h3>
  145. <p>Assert that the given <em>obj</em> is typeof <em>type</em>.</p>
  146. <pre><code>assert.type(3, 'number');
  147. </code></pre>
  148. <h3 id="assert-eql-a-b-msg-">assert.eql(a, b[, msg])</h3>
  149. <p>Assert that object <em>b</em> is equal to object <em>a</em>. This is an
  150. alias for the core <em>assert.deepEqual()</em> method which does complex
  151. comparisons, opposed to <em>assert.equal()</em> which uses <em>==</em>.</p>
  152. <pre><code>assert.eql('foo', 'foo');
  153. assert.eql([1,2], [1,2]);
  154. assert.eql({ foo: 'bar' }, { foo: 'bar' });
  155. </code></pre>
  156. <h3 id="assert-includes-obj-val-msg-">assert.includes(obj, val[, msg])</h3>
  157. <p>Assert that <em>obj</em> is within <em>val</em>. This method supports <em>Array_s
  158. and </em>Strings_s.</p>
  159. <pre><code>assert.includes([1,2,3], 3);
  160. assert.includes('foobar', 'foo');
  161. assert.includes('foobar', 'bar');
  162. </code></pre>
  163. <h3 id="assert-response-server-req-res-fn-msg-fn-">assert.response(server, req, res|fn[, msg|fn])</h3>
  164. <p>Performs assertions on the given <em>server</em>, which should <em>not</em> call
  165. listen(), as this is handled internally by expresso and the server
  166. is killed after all responses have completed. This method works with
  167. any <em>http.Server</em> instance, so <em>Connect</em> and <em>Express</em> servers will work
  168. as well.</p>
  169. <p>The <em>req</em> object may contain:</p>
  170. <ul>
  171. <li><em>url</em> request url</li>
  172. <li><em>timeout</em> timeout in milliseconds</li>
  173. <li><em>method</em> HTTP method</li>
  174. <li><em>data</em> request body</li>
  175. <li><em>headers</em> headers object</li>
  176. </ul>
  177. <p>The <em>res</em> object may be a callback function which
  178. receives the response for assertions, or an object
  179. which is then used to perform several assertions
  180. on the response with the following properties:</p>
  181. <ul>
  182. <li><em>body</em> assert response body (regexp or string)</li>
  183. <li><em>status</em> assert response status code</li>
  184. <li><em>header</em> assert that all given headers match (unspecified are ignored, use a regexp or string)</li>
  185. </ul>
  186. <p>When providing <em>res</em> you may then also pass a callback function
  187. as the fourth argument for additional assertions.</p>
  188. <p>Below are some examples:</p>
  189. <pre><code>assert.response(server, {
  190. url: '/', timeout: 500
  191. }, {
  192. body: 'foobar'
  193. });
  194. assert.response(server, {
  195. url: '/',
  196. method: 'GET'
  197. },{
  198. body: '{"name":"tj"}',
  199. status: 200,
  200. headers: {
  201. 'Content-Type': 'application/json; charset=utf8',
  202. 'X-Foo': 'bar'
  203. }
  204. });
  205. assert.response(server, {
  206. url: '/foo',
  207. method: 'POST',
  208. data: 'bar baz'
  209. },{
  210. body: '/foo bar baz',
  211. status: 200
  212. }, 'Test POST');
  213. assert.response(server, {
  214. url: '/foo',
  215. method: 'POST',
  216. data: 'bar baz'
  217. },{
  218. body: '/foo bar baz',
  219. status: 200
  220. }, function(res){
  221. // All done, do some more tests if needed
  222. });
  223. assert.response(server, {
  224. url: '/'
  225. }, function(res){
  226. assert.ok(res.body.indexOf('tj') &gt;= 0, 'Test assert.response() callback');
  227. });
  228. </code></pre>
  229. <h2 id="expresso-1-">expresso(1)</h2>
  230. <p>To run a single test suite (file) run:</p>
  231. <pre><code>$ expresso test/a.test.js
  232. </code></pre>
  233. <p>To run several suites we may simply append another:</p>
  234. <pre><code>$ expresso test/a.test.js test/b.test.js
  235. </code></pre>
  236. <p>We can also pass a whitelist of tests to run within all suites:</p>
  237. <pre><code>$ expresso --only "foo()" --only "bar()"
  238. </code></pre>
  239. <p>Or several with one call:</p>
  240. <pre><code>$ expresso --only "foo(), bar()"
  241. </code></pre>
  242. <p>Globbing is of course possible as well:</p>
  243. <pre><code>$ expresso test/*
  244. </code></pre>
  245. <p>When expresso is called without any files, <em>test/*</em> is the default,
  246. so the following is equivalent to the command above:</p>
  247. <pre><code>$ expresso
  248. </code></pre>
  249. <p>If you wish to unshift a path to <code>require.paths</code> before
  250. running tests, you may use the <code>-I</code> or <code>--include</code> flag.</p>
  251. <pre><code>$ expresso --include lib test/*
  252. </code></pre>
  253. <p>The previous example is typically what I would recommend, since expresso
  254. supports test coverage via <a href="http://github.com/visionmedia/node-jscoverage">node-jscoverage</a> (bundled with expresso),
  255. so you will need to expose an instrumented version of you library.</p>
  256. <p>To instrument your library, simply run <a href="http://github.com/visionmedia/node-jscoverage">node-jscoverage</a>,
  257. passing the <em>src</em> and <em>dest</em> directories:</p>
  258. <pre><code>$ node-jscoverage lib lib-cov
  259. </code></pre>
  260. <p>Now we can run our tests again, using the <em>lib-cov</em> directory that has been
  261. instrumented with coverage statements:</p>
  262. <pre><code>$ expresso -I lib-cov test/*
  263. </code></pre>
  264. <p>The output will look similar to below, depending on your test coverage of course :)</p>
  265. <p><img src="http://dl.dropbox.com/u/6396913/cov.png" alt="node coverage" /></p>
  266. <p>To make this process easier expresso has the <em>-c</em> or <em>--cov</em> which essentially
  267. does the same as the two commands above. The following two commands will
  268. run the same tests, however one will auto-instrument, and unshift <em>lib-cov</em>,
  269. and the other will run tests normally:</p>
  270. <pre><code>$ expresso -I lib test/*
  271. $ expresso -I lib --cov test/*
  272. </code></pre>
  273. <p>Currently coverage is bound to the <em>lib</em> directory, however in the
  274. future <code>--cov</code> will most likely accept a path.</p>
  275. <h2 id="Async-Exports">Async Exports</h2>
  276. <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>
  277. <pre><code>setTimeout(function(){
  278. exports['test async exports'] = function(){
  279. assert.ok('wahoo');
  280. };
  281. }, 100);
  282. </code></pre>
  283. </div>
  284. </div>
  285. </body>
  286. </html>