Skip to content

Instantly share code, notes, and snippets.

@tobius
Created October 25, 2013 18:35
Show Gist options
  • Save tobius/7159602 to your computer and use it in GitHub Desktop.
Save tobius/7159602 to your computer and use it in GitHub Desktop.
Write a javascript function in node.js that requires a callback and handle omitting the callback in a unit test.
// node module
var assert = require('better-assert');
/**
* make something
*
* @param {Function} callback(err, something)
*/
var makeSomething = function(callback){
var something = 1;
if (arguments.length >= 1){
something++;
callback(err, something);
} else {
// nothing = fail
// do not throw error = fail
// do not throw custom error message = fail
// throw custom error message = pass
throw new Error('Missing parameters');
}
};
describe('makeSomething()', function(){
it('should be a function', function(){
assert(makeSomething !== undefined, 'must not be undefined');
assert(typeof makeSomething === 'function', 'must be a function');
});
describe('making something to callback to nothing', function(){
it('should throw a meaningful error', function(done){
var err;
try {
makeSomething();
} catch(e){
err = e;
assert(err !== undefined, 'must throw a catchable error');
assert(err.toString() !== 'Error', 'must throw a meaningful error');
} finally {
assert(err !== undefined, 'must throw an error');
done();
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment