Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

showDiff: don’t stringify strings

Marcello Bastea-Fortee300ececa2fd
diff --git a/lib/reporters/base.js b/lib/reporters/base.js
index 5dd083df..97b12ba5 100644
--- a/lib/reporters/base.js
+++ b/lib/reporters/base.js
@@ -178,13 +178,13 @@ exports.list = function(failures){
// explicitly show diff
if (err.showDiff && sameType(actual, expected)) {
- escape = false;
- err.actual = actual = utils.stringify(actual);
- err.expected = expected = utils.stringify(expected);
- }
- // actual / expected diff
- if (err.showDiff && 'string' == typeof actual && 'string' == typeof expected) {
+ if ('string' !== typeof actual) {
+ escape = false;
+ 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/);
msg = '\n ' + color('error message', match ? match[1] : msg);
diff --git a/test/reporters/base.js b/test/reporters/base.js
index 49d2f168..d285bf88 100644
--- a/test/reporters/base.js
+++ b/test/reporters/base.js
@@ -34,6 +34,74 @@ describe('Base reporter', function () {
});
+
+ 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);
+ };
+
+ 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/);
+
+ });
+
+
+ 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';
+ }
+ };
+
+ process.stdout.write = function (string) {
+ stdout.push(string);
+ };
+
+ Base.list([test]);
+
+ process.stdout.write = stdoutWrite;
+
+ errOut = stdout.join('\n');
+
+ errOut.should.match(/"key"/);
+ errOut.should.match(/test/);
+ errOut.should.match(/actual/);
+ errOut.should.match(/expected/);
+
+ });
+
it('should not show diffs when showDiff property set', function () {
var err = new Error('test'),
stdout = [],