Skip to content
Permalink
Browse files

feat: Add support for diff output for failed tests

* currently only works with karma-mocha >= v0.2.2

Closes #26
Closes #32
  • Loading branch information...
4kochi committed Feb 19, 2016
1 parent 87136f5 commit ee73dfabd0e98b2c86a99e1e520cb25e2d626080
Showing with 331 additions and 9 deletions.
  1. +5 −0 Gruntfile.js
  2. +1 −1 LICENSE
  3. +41 −1 README.md
  4. BIN demo/diff.png
  5. +73 −0 demo/karma.mocha.conf.js
  6. +47 −0 demo/mocha.spec.js
  7. +156 −4 index.js
  8. +8 −3 package.json
@@ -26,6 +26,9 @@ module.exports = function (grunt) {
demo: {
configFile: 'demo/karma.conf.js'
},
mocha: {
configFile: 'demo/karma.mocha.conf.js'
},
fast: {
configFile: 'demo/karma.conf.js',
browsers: ['PhantomJS'],
@@ -196,6 +199,7 @@ module.exports = function (grunt) {
grunt.registerTask('colors', ['copy:demo', 'karma:colors']);
grunt.registerTask('duplicate', ['copy:demo', 'karma:duplicate']);
grunt.registerTask('reload', ['copy:demo', 'karma:reload']);
grunt.registerTask('mocha', ['copy:demo', 'karma:mocha']);
grunt.registerTask('demo', [
'copy:demo',
'karma:singleBrowser',
@@ -207,6 +211,7 @@ module.exports = function (grunt) {
'karma:noColors',
'karma:colors',
'karma:duplicate',
'karma:mocha',
'karma:reload'
]);

@@ -1,4 +1,4 @@
Copyright (C) 2013-2015 Litixsoft GmbH <info@litixsoft.de>
Copyright (C) 2013-2016 Litixsoft GmbH <info@litixsoft.de>
Licensed under the MIT license.

Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -119,6 +119,46 @@ module.exports = function(config) {
};
```

### 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
**Type:** String

@@ -164,7 +204,7 @@ In lieu of a formal styleguide take care to maintain the existing coding style.

You can preview your changes by running:

$ grunt demo --force
$ npm run demo

## Release History
### v1.1.6
BIN +255 KB demo/diff.png
Binary file not shown.
@@ -0,0 +1,73 @@
module.exports = function (config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: '../',

frameworks: ['mocha', 'chai'],

// list of files / patterns to load in the browser
files: [
'demo/mocha.spec.js'
],

mochaReporter: {
showDiff: true
},

// use dots reporter, as travis terminal does not support escaping sequences
// possible values: 'dots', 'progress'
// CLI --reporters progress
reporters: ['mocha'],

// web server port
// CLI --port 9876
port: 9876,

// enable / disable colors in the output (reporters and logs)
// CLI --colors --no-colors
colors: true,

// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
// CLI --log-level debug
logLevel: config.LOG_INFO,

// enable / disable watching file and executing tests whenever any file changes
// CLI --auto-watch --no-auto-watch
autoWatch: false,

// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
// CLI --browsers Chrome,Firefox,Safari
browsers: [process.env.TRAVIS ? 'Firefox' : 'Chrome'],

// If browser does not capture in given timeout [ms], kill it
// CLI --capture-timeout 5000
captureTimeout: 20000,

// Auto run tests on start (when browsers are captured) and exit
// CLI --single-run --no-single-run
singleRun: true,

// report which specs are slower than 500ms
// CLI --report-slower-than 500
reportSlowerThan: 500,

plugins: [
'karma-mocha',
'karma-mocha-reporter',
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-ie-launcher',
'karma-safari-launcher',
'karma-phantomjs-launcher',
'karma-chai'
]
});
};
@@ -0,0 +1,47 @@
'use strict';

describe('A mocha test suite', function () {

it('should work', function () {
var expected = 'foo';
var actual = 'foo';
expect(actual).to.equal(expected);
});

it('should show a string diff', function () {
var expected = 'foo';
var actual = 'foo bar';
expect(actual).to.equal(expected);
});

it('should show an array diff', function () {
var expected = [
'foo',
'bar'
];
var actual = [
'bar',
'baz'
];
expect(actual).to.deep.equal(expected);
});

it('should show an object diff', function () {
var expected = {
foo: 42,
bar: 1764,
baz: {
qux: 'bleep',
norf: 'bloop'
}
};
var actual = {
bar: 1764,
baz: {
norf: 'bloop'
},
random: true
};
expect(actual).to.deep.equal(expected);
});
});

0 comments on commit ee73dfa

Please sign in to comment.
You can’t perform that action at this time.