Permalink
Browse files

Handle exceptions thrown from test code

Treats them as a failed assertion. Not a great abstraction..
  • Loading branch information...
1 parent 7dd733a commit 38c6e9457068492e63275511f7aa6e85dee34812 @joelplane joelplane committed Jun 17, 2011
Showing with 49 additions and 9 deletions.
  1. +10 −0 examples/exceptionTest.js
  2. +8 −4 noodleTestConsole.js
  3. +31 −5 test.js
@@ -0,0 +1,10 @@
+var test = require('../noodleTest')();
+test.onFailureExitNonZero();
+
+test.context("Example Test Suite", function() {
+ this.context("Example Sub-Context", function() {
+ this.it("Test code throws exception", function(test) {
+ throw new Error('bleh');
+ });
+ });
+});
View
@@ -101,16 +101,20 @@ module.exports = (function(test){
t._failures.forEach(function(assertion){
var callString = assertion.callString();
var failureMessage = assertion.failureMessage();
- log(contextIndent, callString + ' : ' + failureMessage, 'red+bold');
+ if (callString) {
+ log(contextIndent, callString + ' : ' + failureMessage, 'red+bold');
+ } else {
+ log(contextIndent, failureMessage, 'red+bold');
+ }
log(contextIndent, assertion.stack, 'white');
log(0, '');
});
last = null;
});
- test.on('testFlunk', function(o){
- var contextIndent = o.context._depth();
- log(contextIndent, o.context + ': '+color('Failed','red')+': ' + o.message);
+ test.on('testFlunk', function(t){
+ var contextIndent = t._context._depth();
+ //log(contextIndent, color('Failed','red')+': ' + o.message);
last = null;
});
View
@@ -2,6 +2,25 @@ module.exports = (function(Assertion, testQueue, timeout){
var events = require('./events');
var EventEmitter = events.EventEmitter;
var sys = require('sys');
+
+ /* Flunk instance is called as if it were an Assertion instance */
+ var Flunk = function(test, error) {
+ this.test = test;
+ this.error = error;
+ var numberOfStackLinesToSkip = 1 /* First line which is just the name of the fake error we created: "Error" */;
+ this.stackLines = error.stack.split("\n").map(function(line){
+ return line.trim();
+ });
+ this.stack = this.stackLines.slice(numberOfStackLinesToSkip);
+ this._failureMessage = this.stackLines[0];
+ };
+ Flunk.prototype.failureMessage = function() {
+ return this._failureMessage;
+ };
+ Flunk.prototype.callString = function() {
+ return null;
+ };
+
var Test = function(context, name, testFunction) {
this._context = context;
this._name = name;
@@ -26,7 +45,9 @@ module.exports = (function(Assertion, testQueue, timeout){
Test.prototype._call = function() {
var test = this;
this._emit('testStarted', this);
+ var doneCalled = false;
this.done = function() {
+ doneCalled = true;
clearTimeout(timer);
test._emit('testDone', test);
};
@@ -36,13 +57,18 @@ module.exports = (function(Assertion, testQueue, timeout){
try {
this._testFunction.call(this, this);
} catch(error) {
- this.flunk(error.toString(), {error: error, test: this});
+ if (!doneCalled) {
+ clearTimeout(timer);
+ }
+ this._failures.push(new Flunk(this, error));
+ test._emit('testDone', this);
+ //this._emit('testFlunk', this);
+ //this.flunk();
}
};
- Test.prototype.flunk = function(message, options) {
- options = options ? options : {};
- this._emit('testFlunk', {context: this._context, message: message, options: options});
- };
+ /*Test.prototype.flunk = function() {
+ this._emit('testFlunk', this);
+ };*/
Assertion.on('assertionAdded', function(definition){
var methodName = definition['methodName'];

0 comments on commit 38c6e94

Please sign in to comment.