Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle errors correctly based on suite.options.error and the number of parameters expected by the vow #263

Merged
merged 24 commits into from May 13, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
26e5941
fix typo in comment
adamstallard Jan 23, 2013
50a13b5
Handle errors correctly based on suite.options.error and the number o…
adamstallard Jan 23, 2013
4acd17e
Check that tests report errors like they should
adamstallard Jan 23, 2013
98470cb
revert accidental inclusion of print statement
adamstallard Jan 23, 2013
e10dc94
Make the xunit reporter follow the established pattern of using the v…
adamstallard Feb 19, 2013
d6604c3
Don't print out extra blank lines in the spec reporter when end of su…
adamstallard Feb 20, 2013
eb66421
Set the return value to 'undefined' on an unexpected error (since we …
adamstallard Apr 2, 2013
a23a1c9
continue to run subtopics even if a parent topic has an error in it (…
adamstallard Apr 17, 2013
2d77a7a
update vows package
adamstallard Aug 19, 2013
ae4a214
Merge from Upstream
adamstallard Aug 19, 2013
e1d3d16
remove accidental commit
adamstallard Aug 19, 2013
d6e2872
Added tag master for changeset be18031783bf
adamstallard Aug 19, 2013
7595734
Moved tag default/master to changeset 45e0b0d31984 (from changeset 57…
adamstallard Aug 19, 2013
eaaf51a
Moved tag master to changeset 45e0b0d31984 (from changeset be18031783bf)
adamstallard Aug 19, 2013
f031545
Added tag default for changeset 162f542dd244
adamstallard Aug 19, 2013
e86bddd
Backed out changeset: 19f9533f9bae
adamstallard Aug 19, 2013
c8c2ff0
Backed out changeset: be18031783bf
adamstallard Aug 19, 2013
383acd6
Merge
adamstallard Aug 19, 2013
ce6c2c6
update vows package
adamstallard Aug 19, 2013
ecac185
Moved tag default/master to changeset 45104b15b3a8 (from changeset 57…
adamstallard Aug 19, 2013
45e9fdf
Moved tag master to changeset 45104b15b3a8 (from changeset 45e0b0d31984)
adamstallard Aug 19, 2013
e6bcc64
update vows package
adamstallard Aug 19, 2013
ef5e76b
ignore hg files
adamstallard Aug 19, 2013
42f23c3
refactor: remove line that was made redundant after the last merge fr…
adamstallard Aug 21, 2013
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
@@ -1,4 +1,8 @@
node_modules node_modules
.idea .idea
.nul .nul
.hgignore
.hgsubstate
.hg
.hgsub
test/npm-debug.log test/npm-debug.log
4 changes: 4 additions & 0 deletions lib/vows.js
Expand Up @@ -64,6 +64,9 @@ function addVow(vow) {


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

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


if (event !== 'error') { if (event !== 'error') {
this.on("error", function (err) { this.on("error", function (err) {
vow.caughtError = true;
if (vow.callback.length >= 2 || !batch.suite.options.error) { if (vow.callback.length >= 2 || !batch.suite.options.error) {
runTest(arguments, this.ctx); runTest(arguments, this.ctx);
} else { } else {
Expand Down
2 changes: 1 addition & 1 deletion lib/vows/extras.js
@@ -1,6 +1,6 @@
var events = require('events'); var events = require('events');
// //
// Wrap a Node.js style async function into an EventEmmitter // Wrap a Node.js style async function into an EventEmitter
// //
this.prepare = function (obj, targets) { this.prepare = function (obj, targets) {
targets.forEach(function (target) { targets.forEach(function (target) {
Expand Down
7 changes: 6 additions & 1 deletion lib/vows/reporters/xunit.js
Expand Up @@ -4,7 +4,9 @@
// added, see: http://ant.1045680.n5.nabble.com/schema-for-junit-xml-output-td1375274.html // added, see: http://ant.1045680.n5.nabble.com/schema-for-junit-xml-output-td1375274.html
// //


var puts = require('util').puts; var options = { tail: '\n', raw: true };
var console = require('../../vows/console');
var puts = console.puts(options);


var buffer = [], var buffer = [],
curSubject = null; curSubject = null;
Expand Down Expand Up @@ -47,6 +49,9 @@ function cdata(data) {
} }


this.name = 'xunit'; this.name = 'xunit';
this.setStream = function (s) {
options.stream = s;
};
this.report = function (data) { this.report = function (data) {
var event = data[1]; var event = data[1];


Expand Down
23 changes: 14 additions & 9 deletions lib/vows/suite.js
Expand Up @@ -129,13 +129,14 @@ this.Suite.prototype = new(function () {
} }


// Run the topic, passing the previous context topics // Run the topic, passing the previous context topics
// If topic `throw`s an exception, pass it down as a value
try { try {
topic = topic.apply(ctx.env, ctx.topics); topic = topic.apply(ctx.env, ctx.topics);
} }
// If an unexpected error occurs in the topic, set the return
// value to 'undefined' and call back with the error
catch (ex) { catch (ex) {
if(/ReferenceError/.test(ex)) throw ex; ctx.env.callback(ex);
topic = ex; topic = undefined;
} }


if (typeof(topic) === 'undefined') { ctx._callback = true } if (typeof(topic) === 'undefined') { ctx._callback = true }
Expand Down Expand Up @@ -229,13 +230,17 @@ this.Suite.prototype = new(function () {
if (topic && if (topic &&
ctx.name !== 'on' && ctx.name !== 'on' &&
(!topic._vowsEmitedEvents || !topic._vowsEmitedEvents.hasOwnProperty(ctx.event))) { (!topic._vowsEmitedEvents || !topic._vowsEmitedEvents.hasOwnProperty(ctx.event))) {
topic.on(ctx.event, function (ctx) { var runInnerContext = function(ctx){
return function (val) { return function(val){
return run(new(Context)(vow, ctx, env), lastTopic); return run(new (Context)(vow, ctx, env), lastTopic);
}; };
}(ctx)); }(ctx);
} else { topic.on(ctx.event, runInnerContext);
run(new(Context)(vow, ctx, env), lastTopic); // Run an inner context if the outer context fails, too.
topic.on('error', runInnerContext);
}
else {
run(new (Context)(vow, ctx, env), lastTopic);
} }
} }
}); });
Expand Down
75 changes: 73 additions & 2 deletions test/vows-error-test.js
Expand Up @@ -2,7 +2,8 @@ var path = require('path'),
events = require('events'), events = require('events'),
assert = require('assert'), assert = require('assert'),
fs = require('fs'), fs = require('fs'),
vows = require('../lib/vows'); vows = require('../lib/vows'),
silent = require('../lib/vows/reporters/silent');


function doSomethingAsync(callback) { function doSomethingAsync(callback) {
var err = null; var err = null;
Expand Down Expand Up @@ -48,4 +49,74 @@ vows.describe('vows/error').addBatch({
assert.equal(testValue, 'a'); assert.equal(testValue, 'a');
} }
} }
}).export(module) }).export(module)

vows.describe('Error Handling').addBatch({
"A topic with a function that errors": {
topic: function() {
throw("Something wrong here");
},
"should return an error to a vow with two parameters": function(e, data) {
assert.equal(e, "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.");
}
},
"The previous two topics run in their own suite," : {
"connected to two vows expecting one argument each" : {
topic: function(){
vows.describe().addBatch({
"A topic with a function that errors": {
topic: function() {
throw("Something wrong here");
},
"should record the error in the test results" : function(data) {
assert.ok(!data);
}
//» An unexpected error was caught: "Something wrong here"
},
"A topic with a built-in error": {
topic: function() {
bad.bad;
},
"should record the error in the test results" : function(data) {
assert.ok(!data);
}
//» An unexpected error was caught: ReferenceError: bad is not defined
}
}).run({reporter : silent}, this.callback);
},
"should have an errored count of two" : function(results, unused) {
assert.equal(results.errored, 2);
},
"should have a total count of two" : function(results, unused) {
assert.equal(results.total, 2);
},
"should have an honored count of zero" : function(results, unused){
assert.equal(results.honored, 0);
}
}
},
"A topic with an error in it" : {
topic : function(){
throw('awesome');
},
"should error" : function(error, result){
assert.equal(error, 'awesome');
},
"containing a subtopic" : {
topic : function(){
return 52;
},
"should reach a vow in the subtopic" : function(){
}
}
}
}).export(module);

10 changes: 1 addition & 9 deletions test/vows-test.js
Expand Up @@ -168,15 +168,7 @@ vows.describe("Vows").addBatch({
"should work as expected": function (topic) { "should work as expected": function (topic) {
assert.isFunction(topic); assert.isFunction(topic);
assert.equal(topic(), 42); 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");
} }
}, },
"A topic emitting an error": { "A topic emitting an error": {
Expand Down