Skip to content

Instantly share code, notes, and snippets.

@Krxtopher
Forked from tobius/omit-callback-unit-test.js
Last active December 26, 2015 15:18
Show Gist options
  • Save Krxtopher/7171382 to your computer and use it in GitHub Desktop.
Save Krxtopher/7171382 to your computer and use it in GitHub Desktop.
Here's my simplified version of Toby's original solution. This solution takes advantage of Jasmine's built-in toThrow() matcher which is similar to matchers in the should.js, expect.js, and chai assertion libraries which all work with Mocha.
// 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