Testing-with-NPM.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <base href="../../" />
  6. <script src="list.js"></script>
  7. <script src="page.js"></script>
  8. <link type="text/css" rel="stylesheet" href="page.css" />
  9. </head>
  10. <body>
  11. <h1>[name]</h1>
  12. <p class="desc">
  13. This article shows how to get three.js into a [link:https://nodejs.org/en/ node.js] environment so that you
  14. can execute automated tests. Tests can be run on the command line, or by automated
  15. CI tools like [link:https://travis-ci.org/ Travis].
  16. </p>
  17. <h2>The short version</h2>
  18. <p>
  19. If you're comfortable with node and npm,
  20. <code>
  21. $ npm install three --save-dev
  22. </code>
  23. and add
  24. <code>
  25. var THREE = require('three');
  26. </code>
  27. to your test.
  28. </p>
  29. <h2>Create a testable project from scratch</h2>
  30. <p>
  31. If you're not familiar with these tools, here's a quick guide (for linux, the installation process
  32. will be slightly different using windows, but the NPM commands are identical).
  33. </p>
  34. <h3>Basic setup</h3>
  35. <div>
  36. <ol>
  37. <li>
  38. Install [link:https://www.npmjs.org/ npm] and nodejs. The shortest path typically looks something like
  39. <code>
  40. $ sudo apt-get install -y npm nodejs-legacy
  41. # fix any problems with SSL in the default registry URL
  42. $ npm config set registry http://registry.npmjs.org/
  43. </code>
  44. </li>
  45. <li>
  46. Make a new project directory
  47. <code>
  48. $ mkdir test-example; cd test-example
  49. </code>
  50. </li>
  51. <li>
  52. Ask npm to create a new project file for you:
  53. <code>
  54. $ npm init
  55. </code>
  56. and accept all defaults by hitting Enter on all the prompts.
  57. This will create package.json.
  58. </li><br />
  59. <li>
  60. Try and start the test feature with
  61. <code>
  62. $ npm test
  63. </code>
  64. This will fail, which is expected.
  65. If you look in the package.json, the definition of the test script is
  66. <code>
  67. "test": "echo \"Error: no test specified\" && exit 1"
  68. </code>
  69. </li>
  70. </ol>
  71. </div>
  72. <h2>Add mocha</h2>
  73. <div>
  74. We're going to use [link:https://mochajs.org/ mocha].
  75. <ol>
  76. <li>
  77. Install mocha with
  78. <code>
  79. $ npm install mocha --save-dev
  80. </code>
  81. Notice that node_modules/ is created and your dependencies appear in there.
  82. Also notice that your package.json has been updated: the property devDependencies
  83. is added and updated by the use of --save-dev.
  84. </li><br />
  85. <li>
  86. Edit package.json to use mocha for testing. When test is invoked, we just want to run
  87. mocha and specify a verbose reporter. By default this will run anything in test/
  88. (not having directory test/ can run into npm ERR!, create it by mkdir test)
  89. <code>
  90. "test": "mocha --reporter list"
  91. </code>
  92. </li>
  93. <li>
  94. Rerun the test with
  95. <code>
  96. $ npm test
  97. </code>
  98. This should now succeed, reporting 0 passing (1ms)
  99. or similar.
  100. </li>
  101. </ol>
  102. </div>
  103. <h2>Add three.js</h2>
  104. <div>
  105. <ol>
  106. <li>
  107. Let's pull in our three.js dependency with
  108. <code>
  109. $ npm install three --save-dev
  110. </code>
  111. <ul>
  112. <li>
  113. If you need a different three version, use
  114. <code>
  115. $ npm show three versions
  116. </code>
  117. to see
  118. what's available. To tell npm the right one, use
  119. <code>
  120. $ npm install [email protected] --save
  121. </code>
  122. (0.84.0 in this example). --save makes this a dependency of this project, rather than
  123. dev dependency. See the docs [link:https://www.npmjs.org/doc/json.html here] for more info.
  124. </li>
  125. </ul>
  126. </li>
  127. <li>
  128. Mocha will look for tests in test/, so let's
  129. <code>
  130. $ mkdir test
  131. </code>
  132. </li>
  133. <li>
  134. Finally we actually need a JS test to run. Let's add a simple test that will verify that
  135. the three.js object is available and working. Create test/verify-three.js containing:
  136. <code>
  137. var THREE = require('three');
  138. var assert = require("assert");
  139. describe('The THREE object', function() {
  140. it('should have a defined BasicShadowMap constant', function() {
  141. assert.notEqual('undefined', THREE.BasicShadowMap);
  142. }),
  143. it('should be able to construct a Vector3 with default of x=0', function() {
  144. var vec3 = new THREE.Vector3();
  145. assert.equal(0, vec3.x);
  146. })
  147. })
  148. </code>
  149. </li>
  150. <li>
  151. Finally let's test again with $ npm test. This should run the tests above and succeed,
  152. showing something like:
  153. <code>
  154. The THREE object should have a defined BasicShadowMap constant: 0ms
  155. The THREE object should be able to construct a Vector3 with default of x=0: 0ms
  156. 2 passing (8ms)
  157. </code>
  158. </li>
  159. </ol>
  160. </div>
  161. <h2>Add your own code</h2>
  162. <div>
  163. You need to do three things:
  164. <ol>
  165. <li>
  166. Write a test for the expected behaviour of your code, and place it under test/.
  167. [link:https://github.com/air/encounter/blob/master/test/Physics-test.js Here] is an example from a real project.
  168. </li>
  169. <li>
  170. Export your functional code in such a way that nodejs can see it, for use in conjunction with require.
  171. See it [link:https://github.com/air/encounter/blob/master/js/Physics.js here].
  172. </li>
  173. <li>
  174. Require your code into the test file, in the same way we did a require('three') in the example above.
  175. </li>
  176. </ol>
  177. <p>
  178. Items 2 and 3 will vary depending on how you manage your code. In the example of Physics.js
  179. given above, the export part is right at the end. We assign an object to module.exports:
  180. </p>
  181. <code>
  182. //=============================================================================
  183. // make available in nodejs
  184. //=============================================================================
  185. if (typeof exports !== 'undefined')
  186. {
  187. module.exports = Physics;
  188. }
  189. </code>
  190. </div>
  191. <h2>Dealing with dependencies</h2>
  192. <div>
  193. <p>
  194. If you're already using something clever like require.js or browserify, skip this part.
  195. </p>
  196. <p>
  197. Typically a three.js project is going to run in the browser. Module loading is hence done by
  198. the browser executing a bunch of script tags. Your individual files don't have to worry
  199. about dependencies. In a nodejs context however, there is no index.html binding everything
  200. together, so you have to be explicit.
  201. </p>
  202. <p>
  203. If you're exporting a module that depends on other files, you're going to have to tell node to load them.
  204. Here is one approach:
  205. </p>
  206. <ol>
  207. <li>
  208. At the start of your module, check to see if you're in a nodejs environment.
  209. </li>
  210. <li>
  211. If so, explicitly declare your dependencies.
  212. </li>
  213. <li>
  214. If not, you're probably in a browser so you don't need to do anything else.
  215. </li>
  216. </ol>
  217. Example code from Physics.js:
  218. <code>
  219. //=============================================================================
  220. // setup for server-side testing
  221. //=============================================================================
  222. if (typeof require === 'function') // test for nodejs environment
  223. {
  224. var THREE = require('three');
  225. var MY3 = require('./MY3.js');
  226. }
  227. </code>
  228. </div>
  229. </body>
  230. </html>