Skip to content

Commit

Permalink
feat: Add support for diff output for failed tests
Browse files Browse the repository at this point in the history
* currently only works with karma-mocha >= v0.2.2

Closes #26
Closes #32
  • Loading branch information
Andreas Krummsdorf committed Feb 19, 2016
1 parent 87136f5 commit ee73dfa
Show file tree
Hide file tree
Showing 8 changed files with 331 additions and 9 deletions.
5 changes: 5 additions & 0 deletions Gruntfile.js
Expand Up @@ -26,6 +26,9 @@ module.exports = function (grunt) {
demo: { demo: {
configFile: 'demo/karma.conf.js' configFile: 'demo/karma.conf.js'
}, },
mocha: {
configFile: 'demo/karma.mocha.conf.js'
},
fast: { fast: {
configFile: 'demo/karma.conf.js', configFile: 'demo/karma.conf.js',
browsers: ['PhantomJS'], browsers: ['PhantomJS'],
Expand Down Expand Up @@ -196,6 +199,7 @@ module.exports = function (grunt) {
grunt.registerTask('colors', ['copy:demo', 'karma:colors']); grunt.registerTask('colors', ['copy:demo', 'karma:colors']);
grunt.registerTask('duplicate', ['copy:demo', 'karma:duplicate']); grunt.registerTask('duplicate', ['copy:demo', 'karma:duplicate']);
grunt.registerTask('reload', ['copy:demo', 'karma:reload']); grunt.registerTask('reload', ['copy:demo', 'karma:reload']);
grunt.registerTask('mocha', ['copy:demo', 'karma:mocha']);
grunt.registerTask('demo', [ grunt.registerTask('demo', [
'copy:demo', 'copy:demo',
'karma:singleBrowser', 'karma:singleBrowser',
Expand All @@ -207,6 +211,7 @@ module.exports = function (grunt) {
'karma:noColors', 'karma:noColors',
'karma:colors', 'karma:colors',
'karma:duplicate', 'karma:duplicate',
'karma:mocha',
'karma:reload' 'karma:reload'
]); ]);


Expand Down
2 changes: 1 addition & 1 deletion LICENSE
@@ -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. Licensed under the MIT license.


Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
42 changes: 41 additions & 1 deletion README.md
Expand Up @@ -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 ### divider
**Type:** String **Type:** String


Expand Down Expand Up @@ -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: You can preview your changes by running:


$ grunt demo --force $ npm run demo


## Release History ## Release History
### v1.1.6 ### v1.1.6
Expand Down
Binary file added demo/diff.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 73 additions & 0 deletions demo/karma.mocha.conf.js
@@ -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'
]
});
};
47 changes: 47 additions & 0 deletions demo/mocha.spec.js
@@ -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.