mocha-multi
A bit of a hack to get multiple reporters working with mocha
Last updated 10 months ago by glenjamin .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install mocha-multi 
SYNC missed versions from official npm registry.

mocha-multi

A bit of a hack to get multiple reporters working with mocha

Build Status NPM version

Usage

npm install mocha-multi --save-dev
mocha --reporter mocha-multi

Choosing Reporters

For both methods below, the special value of - (hyphen) for destination uses normal stdout/stderr.

With the multi Environment Variable

Set the environment variable multi to whitespace-separated type=destination pairs.

multi='dot=- xunit=file.xml doc=docs.html' mocha -R mocha-multi

With --reporter-options

Pass --reporter-options with comma-separated type=destination pairs.

mocha -R mocha-multi --reporter-options dot=-,xunit=file.xml,doc=docs.html

From a file

Using either of the above methods, include a type=destination pair where the type is mocha-multi and the destination is a filename, e.g. mocha-multi=mocha-multi-reporters.json

More reporters will be loaded from the named file, which must be valid JSON in the same data format described below for passing reporterOptions to Mocha programmatically.

Using mocha-multi programmatically

You may specify the desired reporters (and their options) by passing reporterOptions to the Mocha contructor.

For example: the following config is the equivalent of setting multi='spec=- Progress=/tmp/mocha-multi.Progress.out', with the addition of passing the verbose: true option to the Progress reporter.

var reporterOptions = {
	Progress: {
		stdout: "/tmp/mocha-multi.Progress.out",
		options: {
			verbose: true
		}
	},
	spec: "-"
};

var mocha = new Mocha({
    ui: "bdd"
    reporter: "mocha-multi",
    reporterOptions: reporterOptions
});
mocha.addFile("test/dummy-spec.js");
mocha.run(function onRun(failures){
    console.log(failures);
});

The options will be passed as the second argument to the reporter constructor.

How it works

A big hack that keeps changing the value of process.stdout and process.stderr whenever a reporter is doing its thing.

Seriously?

Yeah, Sorry!

All the hacks

This is very hacky, specifically:

  • The process and console objects get their internal state messed with
  • process.exit is hacked to wait for streams to finish writing
  • Only works if reporters queue writes synchronously in event handlers

Could this be a bit less hacky?

  • Now that https://github.com/mochajs/mocha/pull/1059 is released the process.exit hack could maybe be tidier

  • Having each reporter run in a child process would make it eaiser to capture their streams, but might lead to other issues

TODO

  • Add tests for coverage reports
  • Add tests which produce multiple reports at once
  • Add test for help text
  • Add test that uses --no-exit

HISTORY

1.0.0 (unreleased)

The breaking changes are mostly around internals, and shouldn't affect most people.

  • BREAKING: MochaMulti.prototype.done removed, new MochaMulti(...).done now optional
  • BREAKING: new MochaMulti(...).options removed
  • BREAKING: Must run at least mocha@>=2.2.0
  • BREAKING: Must run at least node@>=6.0.0
  • Correctly set exit code when writing to files
  • Declare support for mocha@^4.0.0
  • Support running mocha without a run callback
  • Upgrade to ES2015+ via eslint-preset-airbnb-base (MochaMulti is an ES class)
  • Avoid patching stderr, now that mocha does not write to it

Current Tags

  • 1.1.3                                ...           latest (10 months ago)

22 Versions

  • 1.1.3                                ...           10 months ago
  • 1.1.2                                ...           10 months ago
  • 1.1.1                                ...           a year ago
  • 1.1.0                                ...           a year ago
  • 1.0.1                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
  • 0.11.1                                ...           3 years ago
  • 0.11.0                                ...           3 years ago
  • 0.10.0                                ...           3 years ago
  • 0.9.1                                ...           4 years ago
  • 0.9.0                                ...           4 years ago
  • 0.8.0                                ...           4 years ago
  • 0.7.2                                ...           5 years ago
  • 0.7.1                                ...           5 years ago
  • 0.6.0                                ...           6 years ago
  • 0.5.0                                ...           6 years ago
  • 0.4.1                                ...           6 years ago
  • 0.4.0                                ...           6 years ago
  • 0.3.0                                ...           7 years ago
  • 0.2.1                                ...           7 years ago
  • 0.2.0                                ...           7 years ago
  • 0.1.0                                ...           7 years ago

Copyright 2014 - 2016 © taobao.org |