karma-mocha-reporter
Advanced tools
Comparing version 1.1.6 to 1.2.0
160
index.js
@@ -53,2 +53,5 @@ 'use strict'; | ||
// set diff output | ||
config.mochaReporter.showDiff = config.mochaReporter.showDiff || false; | ||
var colors = { | ||
@@ -73,2 +76,13 @@ success: { | ||
// check if mocha is installed when showDiff is enabled | ||
if (config.mochaReporter.showDiff) { | ||
try { | ||
var mocha = require('mocha'); | ||
var diff = require('diff'); | ||
} catch (e) { | ||
self.write(colors.error.print('Error loading module mocha!\nYou have enabled diff output. That only works with karma-mocha and mocha installed!\nRun the following command in your command line:\n npm install karma-mocha mocha\n')); | ||
return; | ||
} | ||
} | ||
function getLogSymbol (color) { | ||
@@ -79,2 +93,105 @@ return chalk.enabled ? color.print(color.symbol) : chalk.stripColor(color.symbol); | ||
/** | ||
* Returns a unified diff between two strings. | ||
* | ||
* @param {Error} err with actual/expected | ||
* @return {string} The diff. | ||
*/ | ||
function unifiedDiff (err) { | ||
var indent = ' '; | ||
function cleanUp (line) { | ||
if (line[0] === '+') { | ||
return indent + colors.success.print(line); | ||
} | ||
if (line[0] === '-') { | ||
return indent + colors.error.print(line); | ||
} | ||
if (line.match(/\@\@/)) { | ||
return null; | ||
} | ||
if (line.match(/\\ No newline/)) { | ||
return null; | ||
} | ||
return indent + line; | ||
} | ||
function notBlank (line) { | ||
return line !== null; | ||
} | ||
var msg = diff.createPatch('string', err.actual, err.expected); | ||
var lines = msg.split('\n').splice(4); | ||
return '\n ' + | ||
colors.success.print('+ expected') + ' ' + | ||
colors.error.print('- actual') + | ||
'\n\n' + | ||
lines.map(cleanUp).filter(notBlank).join('\n'); | ||
} | ||
/** | ||
* Return a character diff for `err`. | ||
* | ||
* @param {Error} err | ||
* @param {string} type | ||
* @return {string} | ||
*/ | ||
function errorDiff (err, type) { | ||
var actual = err.actual; | ||
var expected = err.expected; | ||
return diff['diff' + type](actual, expected).map(function (str) { | ||
if (str.added) { | ||
return colors.success.print(str.value); | ||
} | ||
if (str.removed) { | ||
return colors.error.print(str.value); | ||
} | ||
return str.value; | ||
}).join(''); | ||
} | ||
/** | ||
* Pad the given `str` to `len`. | ||
* | ||
* @param {string} str | ||
* @param {string} len | ||
* @return {string} | ||
*/ | ||
function pad (str, len) { | ||
str = String(str); | ||
return Array(len - str.length + 1).join(' ') + str; | ||
} | ||
/** | ||
* Returns an inline diff between 2 strings with coloured ANSI output | ||
* | ||
* @param {Error} err with actual/expected | ||
* @return {string} Diff | ||
*/ | ||
function inlineDiff (err) { | ||
var msg = errorDiff(err, 'WordsWithSpace'); | ||
// 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' + | ||
colors.success.print('expected') + | ||
' ' + | ||
colors.error.print('actual') + | ||
'\n\n' + | ||
msg + | ||
'\n'; | ||
// indent | ||
msg = msg.replace(/^/gm, ' '); | ||
return msg; | ||
} | ||
/** | ||
* Returns a formatted time interval | ||
@@ -187,5 +304,38 @@ * | ||
item.log.forEach(function (err) { | ||
line += colors.error.print(formatError(err, repeatString(' ', depth))); | ||
}); | ||
// print diff | ||
if (config.mochaReporter.showDiff && item.assertionErrors) { | ||
var log = item.log[0].split('\n'); | ||
var errorMessage = log.splice(0, 1)[0]; | ||
// print error message before diff | ||
line += colors.error.print(repeatString(' ', depth) + errorMessage + '\n'); | ||
var expected = item.assertionErrors[0].expected; | ||
var actual = item.assertionErrors[0].actual; | ||
var utils = mocha.utils; | ||
var err = { | ||
actual: actual, | ||
expected: expected | ||
}; | ||
// ensure that actual and expected are strings | ||
if (!(utils.isString(actual) && utils.isString(expected))) { | ||
err.actual = utils.stringify(actual); | ||
err.expected = utils.stringify(expected); | ||
} | ||
// create diff | ||
var diff = config.mochaReporter.showDiff === 'inline' ? inlineDiff(err) : unifiedDiff(err); | ||
line += diff + '\n'; | ||
// print formatted stack trace after diff | ||
log.forEach(function (err) { | ||
line += colors.error.print(formatError(err)); | ||
}); | ||
} else { | ||
item.log.forEach(function (err) { | ||
line += colors.error.print(formatError(err, repeatString(' ', depth))); | ||
}); | ||
} | ||
} | ||
@@ -227,3 +377,2 @@ | ||
*/ | ||
function getTestNounFor (testCount) { | ||
@@ -305,2 +454,5 @@ if (testCount === 1) { | ||
item.log = result.log; | ||
// add assertion errors if available (currently in karma-mocha) | ||
item.assertionErrors = result.assertionErrors; | ||
} | ||
@@ -307,0 +459,0 @@ |
{ | ||
"name": "karma-mocha-reporter", | ||
"description": "Karma reporter with mocha style logging.", | ||
"version": "1.1.6", | ||
"version": "1.2.0", | ||
"homepage": "http://www.litixsoft.de/modules-karmamochareporter", | ||
@@ -48,5 +48,7 @@ "author": "Litixsoft GmbH <info@litixsoft.de> (http://www.litixsoft.de)", | ||
"scripts": { | ||
"test": "grunt test" | ||
"test": "grunt test", | ||
"demo": "grunt demo --force" | ||
}, | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"grunt": "^0.4.5", | ||
@@ -57,3 +59,4 @@ "grunt-contrib-copy": "^0.8.2", | ||
"grunt-shell": "^1.1.2", | ||
"jasmine-core": "2.4.1", | ||
"jasmine-core": "^2.4.1", | ||
"karma-chai": "^0.1.0", | ||
"karma-chrome-launcher": "*", | ||
@@ -64,6 +67,8 @@ "karma-detect-browsers": "^2.0.2", | ||
"karma-jasmine": "^0.3.7", | ||
"karma-mocha": "^0.2.2", | ||
"karma-opera-launcher": "*", | ||
"karma-phantomjs-launcher": "1.0.0", | ||
"karma-safari-launcher": "*", | ||
"phantomjs-prebuilt": "2.1.4" | ||
"mocha": "^2.4.5", | ||
"phantomjs-prebuilt": "^2.1.4" | ||
}, | ||
@@ -70,0 +75,0 @@ "dependencies": { |
@@ -122,2 +122,42 @@ # karma-mocha-reporter | ||
### showDiff | ||
**Type:** String | Boolean | ||
Shows a diff output. Is disabled by default. All credits to the contributors of [mocha](https://github.com/mochajs/mocha), since the diff logic is used from there and customized for this module. | ||
![screenshot](demo/diff.png) | ||
Currently only works with karma-mocha >= v0.2.2 Not supported for karma-jasmine since the additional properties needed to render the diff are not supported in jasmine yet. | ||
**Possible Values:** | ||
Value | Description | ||
------ | ----------- | ||
`true` | prints each diff in its own line, same as `'unified'` | ||
`'unified'` | prints each diff in its own line | ||
`'inline'` | prints diffs inline | ||
```js | ||
// karma.conf.js | ||
module.exports = function(config) { | ||
config.set({ | ||
frameworks: ['mocha', 'chai'], | ||
// reporters configuration | ||
reporters: ['mocha'], | ||
// reporter options | ||
mochaReporter: { | ||
showDiff: true | ||
}, | ||
plugins: [ | ||
'karma-chai', | ||
'karma-mocha', | ||
'karma-mocha-reporter' | ||
] | ||
}); | ||
}; | ||
``` | ||
### divider | ||
@@ -168,5 +208,8 @@ **Type:** String | ||
$ grunt demo --force | ||
$ npm run demo | ||
## Release History | ||
### v1.2.0 | ||
* Add support for diff output for failed tests | ||
### v1.1.6 | ||
@@ -173,0 +216,0 @@ * Fix error that reporter output was truncated when running multiple browsers |
Sorry, the diff of this file is not supported yet
30616
487
315
19