Skip to content

Commit

Permalink
better error output
Browse files Browse the repository at this point in the history
  • Loading branch information
E.Azer Koçulu committed Aug 14, 2012
1 parent 3fefb09 commit f25982c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
63 changes: 53 additions & 10 deletions lib/highkick.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var colors = require('colors'),
path = require('path'),
tty = require('tty');
tty = require('tty'),
assert = require('assert'),
fs = require('fs');

delete require.cache[module.filename];

Expand Down Expand Up @@ -33,14 +35,15 @@ function logger(testsuite){
};
}

function ok(test, testsuite){
function ok(test, testsuite, callback){
testsuite.printTestResult(test, format('OK', 'green'));
callback();
}

function fail(test, testsuite, error){
function fail(test, testsuite, error, callback){
testsuite.fail++;
testsuite.printTestResult(test, format('FAIL', 'red'));
testsuite.log( format('ERROR: ','bold', 'red') + (error.message ? format(error.message, 'bold') : '') + '\n' + error.stack + ')' );
printErrorStack(test, testsuite, error, callback);
}

function childTestModule(options){
Expand Down Expand Up @@ -108,8 +111,7 @@ function genFinish(testsuite){

function genTestCallback(testsuite, test, callback){
return function testCallback(error){
callback();
(error ? fail : ok)(test, testsuite, error);
(error ? fail : ok)(test, testsuite, error || callback, callback);
};
}

Expand Down Expand Up @@ -157,6 +159,7 @@ function genTestCaller(testsuite, test){

var args = [];
var cb = genTestCallback(testsuite, test, function(error){

if(error){
callback(error);
return;
Expand All @@ -165,6 +168,9 @@ function genTestCaller(testsuite, test){
testsuite.initArgs(function(error, initArgs){

callAfterEach.call(undefined, args, function(error){
if(test.line == testsuite.len){
testsuite.finish();
}
!testsuite.options.async && callback(error);
});

Expand Down Expand Up @@ -256,13 +262,50 @@ function genPrintTestResult(testsuite){
+ format(', ', 'grey')
+ format(( duration + 's'), 'white', 'bold')
+ format(') ', 'grey'));

if(test.line == testsuite.len){
testsuite.finish();
}
};
};

function printErrorStack(test, testsuite, error, callback){
var line = error.stack.match(/\(([^\(\)]+)\)/)[1].split(':'),
path = line[0],
num = Number(line[1]),

stack = error.stack.split('\n'),
message = stack[0],
output = [ ' ', format('ERROR: ', 'bold', 'red') + message, ' ' + path + ':' + num ];

fs.readFile(path, function(error, buffer){

if(!error){

buffer.toString()
.split('\n')
.slice(num-2, num+1)
.forEach(function(el, ind){

if( ind == 1 ){
output.push( ' ' + format( num + '. ', 'bold', 'red') + format(el, 'bold') );
} else {
output.push( ' ' + format( ( num + ind - 1) + '. ' + el) );
}

});

};

output.push('');
output.push.apply(output, stack);

output.forEach(function(line){
( testsuite || console ).log(line);
});

callback && callback();

});

}

function kick(/* [path], [options], callback */){

var filename, options,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name":"highkick",
"version":"1.5.4",
"version":"1.6.0",
"description":"Asynchronous, no-style, super simple testing tool.",
"author":"Azer Koculu <azer@kodfabrik.com>",
"keywords":["testing", "test"],
Expand Down

0 comments on commit f25982c

Please sign in to comment.