Permalink
Browse files

Handle errors correctly based on suite.options.error and the number o…

…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...
1 parent 26e5941 commit 50a13b506e31740cd210ea0c39f5dbadc16db761 @adamstallard adamstallard committed Jan 23, 2013
Showing with 33 additions and 7 deletions.
  1. +1 −0 .gitignore
  2. +6 −2 lib/vows.js
  3. +1 −2 lib/vows/suite.js
  4. +25 −3 test/vows-test.js
View
@@ -1,4 +1,5 @@
node_modules
.idea
.nul
+.hgignore
test/npm-debug.log
View
@@ -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;
@@ -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
@@ -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 {
@@ -201,7 +205,7 @@ process.on('exit', function () {
});
});
if (failure) {
- util.puts(console.result(results));
+ util.puts(vows.console.result(results));
process.exit(1);
}
});
View
@@ -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 }
View
@@ -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 () {

0 comments on commit 50a13b5

Please sign in to comment.