Skip to content

Instantly share code, notes, and snippets.

@dmikis
Created July 26, 2013 22:42
Show Gist options
  • Save dmikis/6092739 to your computer and use it in GitHub Desktop.
Save dmikis/6092739 to your computer and use it in GitHub Desktop.
Thought on Node.js modules testing: instead of creating separate file with tests just write test within module. For example, here `b.js` - some module, and `a.js` uses it. If we call `node a.js`, it'll just output `Hello, world` without running assertions in `b.js`. But if we call `node b.js` it'll call assertions as module tests.
var b = require('./b');
console.log(b.hello('world'));
var hello = exports.hello = function (v) {
return 'Hello, ' + v;
};
if (require('path').resolve(process.cwd(), process.argv[1]) === __filename) {
console.assert(hello('b') === 'Hello, b', 'hello method doesn\'t work!');
console.assert(hello('b') === 'Goodbye!', 'broken assertion for demonstration');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment