Skip to content

armgit / mocha

Public Imported from GitHub
0

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

build

TJ Holowaychukff4e613c083e
diff --git a/mocha.js b/mocha.js
index 1ce0dca8..b27c2df7 100644
--- a/mocha.js
+++ b/mocha.js
@@ -1462,14 +1462,22 @@ exports.list = function(failures){
, index = stack.indexOf(message) + message.length
, msg = stack.slice(0, index)
, actual = err.actual
- , expected = err.expected;
+ , expected = err.expected
+ , escape = true;
+
+ // explicitly show diff
+ if (err.showDiff) {
+ escape = false;
+ err.actual = actual = JSON.stringify(actual, null, 2);
+ err.expected = expected = JSON.stringify(expected, null, 2);
+ }
// actual / expected diff
if ('string' == typeof actual && 'string' == typeof expected) {
var len = Math.max(actual.length, expected.length);
- if (len < 20) msg = errorDiff(err, 'Chars');
- else msg = errorDiff(err, 'Words');
+ if (len < 20) msg = errorDiff(err, 'Chars', escape);
+ else msg = errorDiff(err, 'Words', escape);
// linenos
var lines = msg.split('\n');
@@ -1645,12 +1653,14 @@ function pad(str, len) {
* @api private
*/
-function errorDiff(err, type) {
+function errorDiff(err, type, escape) {
return diff['diff' + type](err.actual, err.expected).map(function(str){
- str.value = str.value
- .replace(/\t/g, '<tab>')
- .replace(/\r/g, '<CR>')
- .replace(/\n/g, '<LF>\n');
+ if (escape) {
+ str.value = str.value
+ .replace(/\t/g, '<tab>')
+ .replace(/\r/g, '<CR>')
+ .replace(/\n/g, '<LF>\n');
+ }
if (str.added) return colorLines('diff added', str.value);
if (str.removed) return colorLines('diff removed', str.value);
return str.value;