Permalink
Browse files

pass emitted errors if test is expecting it

  • Loading branch information...
1 parent d79b6d5 commit e97b946deeaae1f25d0b710369e2179ac53f450e cloudhead committed May 13, 2010
Showing with 57 additions and 11 deletions.
  1. +24 −7 lib/vows.js
  2. +33 −4 test/vows-test.js
View
@@ -89,12 +89,34 @@ function addVow(/* description & callback */) {
}
return this.addListener("success", function () {
+ var args = Array.prototype.slice.call(arguments);
+ // If the callback is expecting two or more arguments,
+ // pass the error as the first (null) and the result after.
+ if (vow.callback.length >= 2) {
+ args.unshift(null);
+ }
+ runTest(args);
+
+ }).addListener("error", function (err) {
+ var exception;
+
+ if (vow.callback.length >= 2) {
+ runTest([err]);
+ } else {
+ exception = " * " + stylize('The promise returned an error: ' +
+ stylize(err, 'bold'), 'red');
+ errored++;
+ output('- ' + stylize(vow.description, 'red'), exception + "\n");
+ }
+ });
+
+ function runTest(args) {
var title = ' - ', exception, topic, msg;
// Run the test, and try to catch `AssertionError`s and other exceptions;
// increment counters accordingly.
try {
- vow.callback.apply(args[0].binding || null, arguments);
+ vow.callback.apply(vow.binding || null, args);
title += stylize(vow.description, 'green');
honored++;
} catch (e) {
@@ -110,12 +132,7 @@ function addVow(/* description & callback */) {
}
}
output(title, exception);
- }).addListener("error", function (err) {
- var exception = " * " + stylize('The promise returned an error: ' +
- stylize(err, 'bold'), 'red');
- errored++;
- output('- ' + stylize(vow.description, 'red'), exception + "\n");
- });
+ }
function output(title, exception) {
if (exception || !vows.options.brief) {
View
@@ -130,13 +130,42 @@ vows.describe("Vows").addVows({
}
}
},
- "Non-functions as subjects": {
+ "Non-functions as topics": {
topic: 45,
- "should work as expected": function (subject) {
- assert.equal(subject, 45);
+ "should work as expected": function (topic) {
+ assert.equal(topic, 45);
}
- }
+ },
+ "A topic emitting an error": {
+ topic: function () {
+ var promise = new(events.EventEmitter);
+ process.nextTick(function () {
+ promise.emit("error", 404);
+ });
+ return promise;
+ },
+ "shouldn't raise an exception if the test expects it": function (e, res) {
+ assert.equal(e, 404);
+ assert.ok(! res);
+ }
+ },
+ "A topic not emitting an error": {
+ topic: function () {
+ var promise = new(events.EventEmitter);
+ process.nextTick(function () {
+ promise.emit("success", true);
+ });
+ return promise;
+ },
+ "should pass `null` as first argument, if the test is expecting an error": function (e, res) {
+ assert.strictEqual(e, null);
+ assert.equal(res, true);
+ },
+ "should pass the result as first argument if the test isn't expecting an error": function (res) {
+ assert.equal(res, true);
+ }
+ },
}).addVows({
"A 2nd test suite": {
topic: function () {

0 comments on commit e97b946

Please sign in to comment.