Skip to content

Commit

Permalink
fix(reporter/base): issue mochajs#1241
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m committed Apr 14, 2015
1 parent f9fad1b commit d55221b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
6 changes: 4 additions & 2 deletions lib/reporters/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,10 @@ exports.list = function(failures){
&& expected !== undefined) {

escape = false;
err.actual = actual = utils.stringify(actual);
err.expected = expected = utils.stringify(expected);
if (!(utils.isString(actual) && utils.isString(expected))) {
err.actual = actual = utils.stringify(actual);
err.expected = expected = utils.stringify(expected);
}

fmt = color('error title', ' %s) %s:\n%s') + color('error stack', '\n%s\n');
var match = message.match(/^([^:]+): expected/);
Expand Down
11 changes: 11 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ exports.forEach = function(arr, fn, scope){
fn.call(scope, arr[i], i);
};

/**
* Test if the given obj is type of string
*
* @param {Object} obj
* @returns Boolean
*/

exports.isString = function(obj) {
return 'string' === typeof obj;
};

/**
* Array#map (<=IE8)
*
Expand Down
33 changes: 18 additions & 15 deletions test/reporters/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,24 +88,27 @@ describe('Base reporter', function () {

});

it('should show string diff as raw data', function () {
var err = new Error('test'),
errOut;

err.actual = 'foo\nbar';
err.expected = 'foo\nbaz';
err.showDiff = true;
var test = makeTest(err);
describe('Getting two strings', function() {
// Fix regression V1.2.1(see: issue #1241)
it('should show strings diff as is', function () {
var err = new Error('test'),
errOut;

err.actual = 'foo\nbar';
err.expected = 'foo\nbaz';
err.showDiff = true;
var test = makeTest(err);

Base.list([test]);
Base.list([test]);

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

errOut.should.match(/"foo\\nbar"/);
errOut.should.match(/"foo\\nbaz"/);
errOut.should.match(/test/);
errOut.should.match(/actual/);
errOut.should.match(/expected/);
errOut.should.not.match(/"foo\\nbar"/);
errOut.should.match(/foo/).and.match(/bar/);
errOut.should.match(/test/);
errOut.should.match(/actual/);
errOut.should.match(/expected/);
});
});

it('should stringify objects', function () {
Expand Down

0 comments on commit d55221b

Please sign in to comment.