Skip to content

Commit 50a13b5

Browse files
committed
Handle errors correctly based on suite.options.error and the number of 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.
1 parent 26e5941 commit 50a13b5

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules
22
.idea
33
.nul
4+
.hgignore
45
test/npm-debug.log

lib/vows.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ vows.__defineGetter__('reporter', function () {
3333
});
3434

3535
var stylize = require('./vows/console').stylize;
36-
var console = vows.console = require('./vows/console');
3736

37+
vows.console = require('./vows/console');
3838
vows.inspect = require('./vows/console').inspect;
3939
vows.prepare = require('./vows/extras').prepare;
4040
vows.tryEnd = require('./vows/suite').tryEnd;
@@ -65,6 +65,9 @@ function addVow(vow) {
6565

6666
// always set a listener on the event
6767
this.on(event, function () {
68+
if(vow.caughtError)
69+
return;
70+
6871
var args = Array.prototype.slice.call(arguments);
6972
// If the vow is a sub-event then we know it is an
7073
// emitted event. So I don't muck with the arguments
@@ -81,6 +84,7 @@ function addVow(vow) {
8184

8285
if (event !== 'error') {
8386
this.on("error", function (err) {
87+
vow.caughtError = true;
8488
if (vow.callback.length >= 2 || !batch.suite.options.error) {
8589
runTest(arguments, this.ctx);
8690
} else {
@@ -201,7 +205,7 @@ process.on('exit', function () {
201205
});
202206
});
203207
if (failure) {
204-
util.puts(console.result(results));
208+
util.puts(vows.console.result(results));
205209
process.exit(1);
206210
}
207211
});

lib/vows/suite.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,7 @@ this.Suite.prototype = new(function () {
134134
topic = topic.apply(ctx.env, ctx.topics);
135135
}
136136
catch (ex) {
137-
if(/ReferenceError/.test(ex)) throw ex;
138-
topic = ex;
137+
ctx.env.callback(ex);
139138
}
140139

141140
if (typeof(topic) === 'undefined') { ctx._callback = true }

test/vows-test.js

+25-3
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,38 @@ vows.describe("Vows").addBatch({
168168
"should work as expected": function (topic) {
169169
assert.isFunction(topic);
170170
assert.equal(topic(), 42);
171-
},
171+
}
172172
}
173173
},
174174
"A topic with a function that errors": {
175175
topic: function() {
176176
throw("Something wrong here");
177177
},
178-
"should error out": function(topic) {
179-
assert.equal(topic, "Something wrong here");
178+
"should return an error to a vow with two parameters": function(e, data) {
179+
assert.equal(e, "Something wrong here");
180+
}
181+
// TODO: make a test that runs a vows suite and check the error count
182+
//,
183+
// "and record the error in the test results otherwise" : function(data) {
184+
// assert.ok(!data);
185+
// }
186+
//✗ and record the error in the test results otherwise
187+
// » An unexpected error was caught: "Something wrong here"
188+
},
189+
"A topic with a built-in error": {
190+
topic: function() {
191+
bad.bad;
192+
},
193+
"should return an error to a vow with two parameters": function(e, data) {
194+
assert(e instanceof Error, "Return value " + e + " wasn't an Error.");
180195
}
196+
// TODO: make a test that runs a vows suite and check the error count
197+
//,
198+
// "and record the error in the test results otherwise" : function(data) {
199+
// assert.ok(!data);
200+
// }
201+
//✗ and record the error in the test results otherwise
202+
// » An unexpected error was caught: ReferenceError: bad is not defined
181203
},
182204
"A topic emitting an error": {
183205
topic: function () {

0 commit comments

Comments
 (0)