Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I agree with David Weldon's post, but I'm going to add some examples.</p> <p>You should be writing your Node modules as normal. If I'm writing a Calculator that might be used in other parts of the code my Calculator module might look something like this:</p> <pre><code>Calculator = { add: function(a, b) { return a + b; } }; module.exports = Calculator; </code></pre> <p>You mention that using <code>module.exports</code> seems costly and obtrusive. There are a couple of things you can do about this. <code>module.exports</code> is the best and recommended way to have your code accessible in another file. To make it simple you can wrap your functions and variables in an object as I did with the Calculator and only export the one object.</p> <p>Note that there are two ways to use "exports". You can do this:</p> <pre><code>var a = function() { ... }; var b = 10; var c = null; exports.a = a; exports.b = b; exports.c = c; </code></pre> <p>Which adds things to the special <code>exports</code> object for the module. Or you can do this:</p> <pre><code>objectToExport = { a: function() { ... }, b: 10, c: null }; module.exports = objectToExport; </code></pre> <p>Which will <strong>replace</strong> the <code>exports</code> object. If you did <code>exports = objectToExport</code> it wouldn't work and you would end up with an empty object when you tried to <code>require</code> the module in another file.</p> <p>When I test with Mocha I put all my test files in a <code>test</code> directory that mirrors the structure of my code directories.</p> <pre><code>project +- lib +- calculator.js +- test +- lib +- calculator.js </code></pre> <p>My test/lib/calculator.js file is my Mocha tests for the lib/calculator.js file</p> <pre><code>var Calculator = require('../../lib/calculator.js'); var assert = require('assert'); describe('Calculator', function() { describe('add', function() { it('should add two numbers', function() { var res = Calculator.add(2,2); assert.equal(res, 4); }); }); }); </code></pre> <p>By default mocha will look for the <code>/test</code> directory in the directory you run mocha. If you have subdirectories like in the example above you can add <code>--recursive</code> to the command line to find all your tests. So if you running in the <code>project</code> directory above your command would be <code>mocha --recursive</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload