Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

karma-mocha

karma-runner960.9kMIT2.0.1TypeScript support: definitely-typed

A Karma plugin. Adapter for Mocha testing framework.

karma-plugin, karma-adapter, mocha

readme

karma-mocha

js-standard-style npm version npm downloads

Build Status Dependency Status devDependency Status

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...

// karma.conf.js
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

// karma.conf.js
module.exports = function(config) {
  config.set({
    frameworks: ['mocha'],

    files: [
      '*.js'
    ],

    client: {
      mocha: {
        // change Karma's debug.html to the mocha web reporter
        reporter: 'html',

        // require specific files after Mocha is initialized
        require: [require.resolve('bdd-lazy-var/bdd_lazy_var_global')],

        // custom ui, defined in required file above
        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>', // passed directly to mocha
        ...
      }
      ...
    }
  });
};

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'] // This will be exposed in a reporter as `result.mocha.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' // You can set opts to equal true then plugin will load opts from default location '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 started
  • endTime Milliseconds since epoch that the test ended
  • assertionErrors 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.

changelog

2.0.1 (2020-04-29)

Bug Fixes

  • deps: Report fails without emit 'test end' event (#223) (1a8226c)

2.0.0 (2020-04-14)

Features

  • Update Node.js versions (fd64f5b)

BREAKING CHANGES

  • drop support for node 8

1.3.0 (2016-11-09)

Bug Fixes

  • polyfill Date.now to restore IE compat (246f25a)
  • deps: remove peer dependency on mocha (0bbf932), closes #114

Features

1.2.0 (2016-09-26)

Bug Fixes

  • "remove mocha stack entries" was too greedy (c893d0a)

Features

  • reporter: add timestamps to results (7b41f52)
  • reporter: expose mocha test properties (e4eb5fd)

1.1.1 (2016-06-26)

Bug Fixes

  • handle non existing mochaConfig (dcb74a5)

1.1.0 (2016-06-26)

Features

  • allow requiring files via karma.conf.js (f00d6b3), closes #84
  • deps: update dependencies (fe04ebb)

1.0.1 (2016-05-02)

Bug Fixes

  • Do not lose stack trace at debug.html (0b5a392)

0.2.2 (2016-02-15)

0.2.1 (2015-11-13)

Bug Fixes

  • Tests that call this.skip() are marked as failed instead of skipped fix #77 (73e554f)

0.2.0 (2015-06-22)

Bug Fixes

  • config: grep is interpreted as regexp (01d11e6)
  • npm: Make .npmignore more sensible to dot files (4ebf361), closes #59
  • Drop karma as a peerDependency. (5c11809)

0.1.10 (2014-12-04)

0.1.9 (2014-08-23)

Bug Fixes

  • properly format err messages with new lines (1144690), closes #40

0.1.8 (2014-08-18)

Bug Fixes

0.1.7 (2014-08-07)

0.1.6 (2014-07-17)

Bug Fixes

  • correctly prepare the new version (v0.1.5) (ab2a23f)

0.1.5 (2014-06-16)

0.1.4 (2014-06-15)

Features

  • Allow define html reporter on debug page. resolve #20 (7f8d4cc)

0.1.3 (2014-03-13)

Bug Fixes

0.1.2 (2014-02-18)

Bug Fixes

  • restores the ability to use grep (2787886)

0.1.1 (2013-11-26)

Features

  • Pass client configuration to `mocha.setup method. (4df9ba6), closes #13

0.1.0 (2013-08-06)

0.0.4 (2013-07-19)

Features

  • remove dump, as karma implements it (e02af06)

0.0.3 (2013-07-07)

Bug Fixes

  • config does not have to be defined (edfa534)

0.0.2 (2013-07-05)

Bug Fixes

  • report time for skipped tests so that netTime is computed correctly (4f5470d), closes #141