Skip to content

Commit

Permalink
Handle errors correctly based on suite.options.error and the number o…
Browse files Browse the repository at this point in the history
…f parameters expected by the vow: When suite.options.error is set to false or a vow expects two or more parameters, get the error as the first argument and don't report it; When suite.options.error is set to true and a vow expects zero or one parameters, report the error and do not run the vow.
  • Loading branch information
adamstallard committed Jan 23, 2013
1 parent 26e5941 commit 50a13b5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
.idea
.nul
.hgignore
test/npm-debug.log
8 changes: 6 additions & 2 deletions lib/vows.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ vows.__defineGetter__('reporter', function () {
});

var stylize = require('./vows/console').stylize;
var console = vows.console = require('./vows/console');

vows.console = require('./vows/console');
vows.inspect = require('./vows/console').inspect;
vows.prepare = require('./vows/extras').prepare;
vows.tryEnd = require('./vows/suite').tryEnd;
Expand Down Expand Up @@ -65,6 +65,9 @@ function addVow(vow) {

// always set a listener on the event
this.on(event, function () {
if(vow.caughtError)
return;

var args = Array.prototype.slice.call(arguments);
// If the vow is a sub-event then we know it is an
// emitted event. So I don't muck with the arguments
Expand All @@ -81,6 +84,7 @@ function addVow(vow) {

if (event !== 'error') {
this.on("error", function (err) {
vow.caughtError = true;
if (vow.callback.length >= 2 || !batch.suite.options.error) {
runTest(arguments, this.ctx);
} else {
Expand Down Expand Up @@ -201,7 +205,7 @@ process.on('exit', function () {
});
});
if (failure) {
util.puts(console.result(results));
util.puts(vows.console.result(results));
process.exit(1);
}
});
Expand Down
3 changes: 1 addition & 2 deletions lib/vows/suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ this.Suite.prototype = new(function () {
topic = topic.apply(ctx.env, ctx.topics);
}
catch (ex) {
if(/ReferenceError/.test(ex)) throw ex;
topic = ex;
ctx.env.callback(ex);
}

if (typeof(topic) === 'undefined') { ctx._callback = true }
Expand Down
28 changes: 25 additions & 3 deletions test/vows-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,38 @@ vows.describe("Vows").addBatch({
"should work as expected": function (topic) {
assert.isFunction(topic);
assert.equal(topic(), 42);
},
}
}
},
"A topic with a function that errors": {
topic: function() {
throw("Something wrong here");
},
"should error out": function(topic) {
assert.equal(topic, "Something wrong here");
"should return an error to a vow with two parameters": function(e, data) {
assert.equal(e, "Something wrong here");
}
// TODO: make a test that runs a vows suite and check the error count
//,
// "and record the error in the test results otherwise" : function(data) {
// assert.ok(!data);
// }
//✗ and record the error in the test results otherwise
// » An unexpected error was caught: "Something wrong here"
},
"A topic with a built-in error": {
topic: function() {
bad.bad;
},
"should return an error to a vow with two parameters": function(e, data) {
assert(e instanceof Error, "Return value " + e + " wasn't an Error.");
}
// TODO: make a test that runs a vows suite and check the error count
//,
// "and record the error in the test results otherwise" : function(data) {
// assert.ok(!data);
// }
//✗ and record the error in the test results otherwise
// » An unexpected error was caught: ReferenceError: bad is not defined
},
"A topic emitting an error": {
topic: function () {
Expand Down

0 comments on commit 50a13b5

Please sign in to comment.