Skip to content

Commit

Permalink
Merge pull request #1631 from mantoni/fix-safari-consolify
Browse files Browse the repository at this point in the history
fix(reporter/base): don't assume error message is first line of stack
  • Loading branch information
dasilvacontin committed Mar 30, 2015
2 parents ec6440e + 18d2e05 commit d9a8192
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 87 deletions.
19 changes: 13 additions & 6 deletions lib/reporters/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,26 @@ exports.list = function(failures){
var err = test.err
, message = err.message || ''
, stack = err.stack || message
, index = stack.indexOf(message) + message.length
, msg = stack.slice(0, index)
, index = stack.indexOf(message)
, actual = err.actual
, expected = err.expected
, escape = true;
if (index === -1) {
msg = message;
} else {
index += message.length;
msg = stack.slice(0, index);
// remove msg from stack
stack = stack.slice(index + 1);
}

// uncaught
if (err.uncaught) {
msg = 'Uncaught ' + msg;
}
// explicitly show diff
if (err.showDiff !== false && sameType(actual, expected)) {
if (err.showDiff !== false && sameType(actual, expected)
&& expected !== undefined) {

if ('string' !== typeof actual) {
escape = false;
Expand All @@ -198,9 +206,8 @@ exports.list = function(failures){
}
}

// indent stack trace without msg
stack = stack.slice(index ? index + 1 : index)
.replace(/^/gm, ' ');
// indent stack trace
stack = stack.replace(/^/gm, ' ');

console.log(fmt, (i + 1), test.fullTitle(), msg, stack);
});
Expand Down
164 changes: 83 additions & 81 deletions test/reporters/base.js
Original file line number Diff line number Diff line change
@@ -1,153 +1,155 @@
var Base = require('../../lib/reporters/base')
, Assert = require('assert').AssertionError;

function makeTest(err) {
return {
err: err,
fullTitle: function () {
return 'test title';
}
};
}

describe('Base reporter', function () {
var stdout
, stdoutWrite
, useColors;

beforeEach(function () {
stdout = [];
stdoutWrite = process.stdout.write;
process.stdout.write = function (string) {
stdout.push(string);
};
useColors = Base.useColors;
Base.useColors = false;
});

afterEach(function () {
process.stdout.write = stdoutWrite;
Base.useColors = useColors;
});

describe('showDiff', function() {
it('should show diffs by default', function () {
var err = new Assert({ actual: 'foo', expected: 'bar' })
, stdout = []
, stdoutWrite = process.stdout.write
, errOut;

var test = {
err: err,
fullTitle: function () {
return 'test title';
}
};

process.stdout.write = function (string) {
stdout.push(string);
};
var test = makeTest(err);

Base.list([test]);

process.stdout.write = stdoutWrite;

errOut = stdout.join('\n');
errOut.should.match(/actual/);
errOut.should.match(/expected/);
errOut.should.match(/\- actual/);
errOut.should.match(/\+ expected/);
});

it('should show diffs if property set to `true`', function () {
var err = new Assert({ actual: 'foo', expected: 'bar' })
, stdout = []
, stdoutWrite = process.stdout.write
, errOut;

err.showDiff = true;
var test = {
err: err,
fullTitle: function () {
return 'test title';
}
};
var test = makeTest(err);

process.stdout.write = function (string) {
stdout.push(string);
};

Base.list([test]);

process.stdout.write = stdoutWrite;

errOut = stdout.join('\n');
errOut.should.match(/actual/);
errOut.should.match(/expected/);
errOut.should.match(/\- actual/);
errOut.should.match(/\+ expected/);
});

it('should not show diffs when showDiff property set to `false`', function () {
var err = new Assert({ actual: 'foo', expected: 'bar' })
, stdout = []
, stdoutWrite = process.stdout.write
, errOut;

err.showDiff = false;
var test = {
err: err,
fullTitle: function () {
return 'test title';
}
};

process.stdout.write = function (string) {
stdout.push(string);
};
var test = makeTest(err);

Base.list([test]);

process.stdout.write = stdoutWrite;
errOut = stdout.join('\n');
errOut.should.not.match(/\- actual/);
errOut.should.not.match(/\+ expected/);
});

it('should not show diffs when expected is not defined', function () {
var err = new Error('ouch')
, errOut;

var test = makeTest(err);

Base.list([test]);

errOut = stdout.join('\n');
errOut.should.not.match(/actual/);
errOut.should.not.match(/expected/);
errOut.should.not.match(/\- actual/);
errOut.should.not.match(/\+ expected/);
});

});

it('should not stringify strings', function () {
var err = new Error('test'),
stdout = [],
stdoutWrite = process.stdout.write,
errOut;

err.actual = "a1";
err.expected = "e2";
err.showDiff = true;
var test = {
err: err,
fullTitle: function () {
return 'title';
}
};

process.stdout.write = function (string) {
stdout.push(string);
};
var test = makeTest(err);

Base.list([test]);

process.stdout.write = stdoutWrite;

errOut = stdout.join('\n');

errOut.should.not.match(/"/);
errOut.should.match(/test/);
errOut.should.match(/actual/);
errOut.should.match(/expected/);

errOut.should.match(/\- actual/);
errOut.should.match(/\+ expected/);
});


it('should stringify objects', function () {
var err = new Error('test'),
stdout = [],
stdoutWrite = process.stdout.write,
errOut;

err.actual = {key:"a1"};
err.expected = {key:"e1"};
err.showDiff = true;
var test = {
err: err,
fullTitle: function () {
return 'title';
}
};
var test = makeTest(err);

process.stdout.write = function (string) {
stdout.push(string);
Base.list([test]);

errOut = stdout.join('\n');
errOut.should.match(/"key"/);
errOut.should.match(/test/);
errOut.should.match(/\- actual/);
errOut.should.match(/\+ expected/);
});

it('should remove message from stack', function () {
var err = {
message: 'Error',
stack: 'Error\nfoo\nbar',
showDiff: false
};
var test = makeTest(err);

Base.list([test]);

process.stdout.write = stdoutWrite;
var errOut = stdout.join('\n').trim();
errOut.should.equal('1) test title:\n Error\n foo\n bar')
});

errOut = stdout.join('\n');
it('should not modify stack if it does not contain message', function () {
var err = {
message: 'Error',
stack: 'foo\nbar',
showDiff: false
};
var test = makeTest(err);

errOut.should.match(/"key"/);
errOut.should.match(/test/);
errOut.should.match(/actual/);
errOut.should.match(/expected/);
Base.list([test]);

var errOut = stdout.join('\n').trim();
errOut.should.equal('1) test title:\n Error\n foo\n bar')
});

});

0 comments on commit d9a8192

Please sign in to comment.