☕️ Classic, reliable, trusted test framework for Node.js and the browser
Added basic diff support for the terminal
TJ Holowaychuk84bb6b91b097
diff --git a/lib/reporters/base.js b/lib/reporters/base.jsindex ba04e35c..8b93e035 100644--- a/lib/reporters/base.js+++ b/lib/reporters/base.js@@ -3,7 +3,8 @@* Module dependencies.*/-var tty = require('tty');+var tty = require('tty')+ , diff = require('diff');/*** Check if both stdio streams are associated with a tty.@@ -44,6 +45,9 @@ exports.colors = {, 'slow': 31, 'green': 32, 'light': 90+ , 'diff gutter': 90+ , 'diff added': 41+ , 'diff removed': 42};/**@@ -131,7 +135,41 @@ exports.list = function(failures){, message = err.message || '', stack = err.stack || message, index = stack.indexOf(message) + message.length- , msg = stack.slice(0, index);+ , msg = stack.slice(0, index)+ , actual = err.actual+ , expected = err.expected;++ // 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');++ // linenos+ var lines = msg.split('\n');+ if (lines.length > 4) {+ var width = String(lines.length).length;+ msg = lines.map(function(str, i){+ return pad(++i, width) + ' |' + ' ' + str;+ }).join('\n');+ }++ // legend+ msg = '\n'+ + color('diff removed', 'actual')+ + ' '+ + color('diff added', 'expected')+ + '\n\n'+ + msg+ + '\n';++ // indent+ msg = msg.replace(/^/gm, ' ');++ fmt = color('error title', ' %s) %s:\n%s')+ + color('error stack', '\n%s\n');+ }// indent stack trace without msgstack = stack.slice(index + 1)@@ -234,3 +272,48 @@ Base.prototype.epilogue = function(){console.log(fmt, stats.tests || 0, stats.duration);console.log();};++/**+ * Pad the given `str` to `len`.+ *+ * @param {String} str+ * @param {String} len+ * @return {String}+ * @api private+ */++function pad(str, len) {+ str = String(str);+ return Array(len - str.length + 1).join(' ') + str;+}++/**+ * Return a character diff for `err`.+ *+ * @param {Error} err+ * @return {String}+ * @api private+ */++function errorDiff(err, type) {+ return diff['diff' + type](err.actual, err.expected).map(function(str){+ if (str.added) return colorLines('diff added', str.value);+ if (str.removed) return colorLines('diff removed', str.value);+ return str.value;+ }).join('');+}++/**+ * Color lines for `str`, using the color `name`.+ *+ * @param {String} name+ * @param {String} str+ * @return {String}+ * @api private+ */++function colorLines(name, str) {+ return str.split('\n').map(function(str){+ return color(name, str);+ }).join('\n');+}diff --git a/package.json b/package.jsonindex f8f8ea7a..3f42288f 100644--- a/package.json+++ b/package.json@@ -15,6 +15,7 @@"commander": "0.5.x", "growl": "1.5.x", "jade": "0.20.3"+ , "diff": "1.0.2", "debug": "*"}, "devDependencies": {diff --git a/test/acceptance/diffs.js b/test/acceptance/diffs.jsnew file mode 100644index 00000000..297a1f1f--- /dev/null+++ b/test/acceptance/diffs.js@@ -0,0 +1,24 @@++var fs = require('fs')+ , cssin = fs.readFileSync('test/acceptance/fixtures/css.in', 'ascii')+ , cssout = fs.readFileSync('test/acceptance/fixtures/css.out', 'ascii');++describe('diffs', function(){+ it('should display a char diff for small strings', function(){+ var a = 'foo bar baz'+ , b = 'foo rar baz';++ // a.should.equal(b);+ })++ it('should display a word diff for medium strings', function(){+ var a = 'foo bar baz\nfoo bar baz\nfoo bar baz'+ , b = 'foo bar baz\nfoo rar baz\nfoo bar raz';++ // a.should.equal(b);+ })++ it('should display a word diff for large strings', function(){+ // cssin.should.equal(cssout);+ })+})\ No newline at end of filediff --git a/test/acceptance/fixtures/css.in b/test/acceptance/fixtures/css.innew file mode 100644index 00000000..29fb2746--- /dev/null+++ b/test/acceptance/fixtures/css.in@@ -0,0 +1,9 @@+body {+ font: "Helvetica Neue", Helvetica, arial, sans-serif;+ background: black;+ color: white;+}++a {+ color: blue+}\ No newline at end of filediff --git a/test/acceptance/fixtures/css.out b/test/acceptance/fixtures/css.outnew file mode 100644index 00000000..643692a4--- /dev/null+++ b/test/acceptance/fixtures/css.out@@ -0,0 +1,13 @@+body {+ font: "Helvetica Neue", Helvetica, arial, sans-serif;+ background: black;+ color: #fff;+}++a {+ color: blue;+}++foo {+ bar: 'baz';+}\ No newline at end of file