What is karma-mocha?
The karma-mocha npm package is a plugin that allows you to use the Mocha testing framework with the Karma test runner. It enables you to run Mocha tests in various browsers and provides a seamless integration between Karma and Mocha.
What are karma-mocha's main functionalities?
Running Mocha Tests with Karma
This configuration file sets up Karma to use the Mocha framework. It specifies the test files to include and the browser to run the tests in. The 'singleRun' option ensures that Karma runs the tests once and then exits.
module.exports = function(config) {
config.set({
frameworks: ['mocha'],
files: [
'test/**/*.js'
],
browsers: ['Chrome'],
singleRun: true
});
};
Using Mocha Hooks
This code demonstrates the use of Mocha hooks (before, after, beforeEach, afterEach) within a test suite. These hooks allow you to set up preconditions and clean up after tests.
describe('Array', function() {
before(function() {
// runs before all tests in this block
});
after(function() {
// runs after all tests in this block
});
beforeEach(function() {
// runs before each test in this block
});
afterEach(function() {
// runs after each test in this block
});
it('should start empty', function() {
var arr = [];
assert.equal(arr.length, 0);
});
});
Asynchronous Testing
This code demonstrates how to write asynchronous tests in Mocha. The 'done' callback is used to signal that the test is complete. If an error occurs, it is passed to the 'done' callback.
describe('User', function() {
it('should save without error', function(done) {
var user = new User('Luna');
user.save(function(err) {
if (err) done(err);
else done();
});
});
});
Other packages similar to karma-mocha
karma-jasmine
The karma-jasmine package is a plugin that allows you to use the Jasmine testing framework with the Karma test runner. Like karma-mocha, it enables you to run tests in various browsers. Jasmine is known for its behavior-driven development (BDD) style and comes with a rich set of matchers and utilities.
karma-qunit
The karma-qunit package is a plugin that integrates the QUnit testing framework with the Karma test runner. QUnit is a powerful, easy-to-use JavaScript unit testing framework. It is used by the jQuery project and is known for its simplicity and ease of use.
karma-chai
The karma-chai package allows you to use the Chai assertion library with the Karma test runner. Chai can be used with different testing frameworks like Mocha, Jasmine, and QUnit. It provides a variety of assertion styles, including BDD and TDD, making it a versatile choice for testing.
karma-mocha
Adapter for the Mocha testing framework.
Installation
Install karma-mocha
and mocha
into to your project via npm
:
$ npm install karma-mocha mocha --save-dev
karma-mocha
should work with any version of mocha
.
Since karma-mocha
is an adapter for Karma, you likely have it installed already, but in case you don't:
$ npm install karma --save-dev
If you're having trouble, Karma provides detailed instructions on installation.
Configuration
Following code shows the default configuration...
module.exports = function(config) {
config.set({
frameworks: ['mocha'],
files: [
'*.js'
]
});
};
If you want to pass configuration options directly to mocha you can
do this in the following way
module.exports = function(config) {
config.set({
frameworks: ['mocha'],
files: [
'*.js'
],
client: {
mocha: {
reporter: 'html',
require: [require.resolve('bdd-lazy-var/bdd_lazy_var_global')],
ui: 'bdd-lazy-var/global',
}
}
});
};
If you want run only some tests matching a given pattern you can
do this in the following way
karma start &
karma run -- --grep=<pattern>
or
module.exports = function(config) {
config.set({
...
client: {
mocha: {
grep: '<pattern>',
...
}
...
}
});
};
If you want to expose test properties specific to mocha
, you can use the expose
option:
module.exports = function(config) {
config.set({
...
client: {
mocha: {
expose: ['body']
...
}
...
}
});
};
If you already have a configuration for Mocha in an opts file, you can use the opts
option:
module.exports = function(config) {
config.set({
...
client: {
mocha: {
opts: 'test/mocha.opts'
...
}
...
}
});
};
Internals
On the end of each test karma-mocha
passes to karma
result object with fields:
description
Test title.suite
List of titles of test suites.success
True if test is succeed, false otherwise.skipped
True if test is skipped.time
Test duration.log
List of errors.startTime
Milliseconds since epoch that the test startedendTime
Milliseconds since epoch that the test endedassertionErrors
List of additional error info:
name
Error name.message
Error message.actual
Actual data in assertion, serialized to string.expected
Expected data in assertion, serialized to string.showDiff
True if it is configured by assertion to show diff.
mocha
An optional object listed if you use the expose
option
This object will be passed to test reporter.
NB. the start and end times are added by the adapter whereas the duration is calculated by Mocha - as such they probably will not match arithmetically. Ie. endTime - startTime !== duration
. These fields have been added so that timestamped reports can be matched up with other timestamped reports from the target device (eg. memory profiling data collected outside the browser)
For more information on Karma see the homepage.