UNPKG

2.63 kBMarkdownView Raw
1mocha-multi
2===========
3
4A bit of a hack to get multiple reporters working with mocha
5
6[![Build Status](https://travis-ci.org/glenjamin/mocha-multi.png?branch=master)](https://travis-ci.org/glenjamin/mocha-multi)
7[![NPM version](https://badge.fury.io/js/mocha-multi.png)](http://badge.fury.io/js/mocha-multi)
8
9Usage
10-----
11
12 npm install mocha-multi --save-dev
13 mocha --reporter mocha-multi
14
15Choosing Reporters
16------------------
17
18Set an environment variable called `multi` to specify the desired reporters.
19Reporters are listed as whitespace separated type=destination pairs.
20
21```bash
22multi='dot=- xunit=file.xml doc=docs.html' mocha -R mocha-multi
23```
24
25The special value of `-` (hyphen) for destination uses normal stdout/stderr.
26
27Using mocha-multi programmatically
28----------------------------------
29
30Specify the desired reporters (and their options) by passing reporterOptions to the Mocha contructor
31
32for example: this is the equivalent of multi='spec=- Progress=/tmp/mocha-multi.Progress.out', with the addition of passing the verbose:true option to the Progress reporter
33```sh
34var reporterOptions={
35 Progress:{
36 stdout:"/tmp/mocha-multi.Progress.out",
37 options: {
38 verbose: true
39 }
40 },
41 spec: {
42 stdout:"-"
43 }
44};
45
46var mocha = new Mocha({
47 ui: 'bdd',
48 reporter: "mocha-multi",
49 reporterOptions:reporterOptions
50});
51mocha.addFile("test/dummy-spec.js");
52mocha.run(function onRun(failures){
53 console.log(failures);
54});
55```
56The options will be passed as the second argument to the reporter constructor
57
58How it works
59------------
60
61A big hack that keeps changing the value of process.stdout and process.stderr whenever a reporter is doing its thing.
62
63Seriously?
64----------
65
66Yeah, Sorry!
67
68All the hacks
69-------------
70
71This is very hacky, specifically:
72
73 * The `process` and `console` objects get their internal state messed with
74 * `process.exit` is hacked to wait for streams to finish writing
75 * Only works if reporters queue writes synchronously in event handlers
76
77Could this be a bit less hacky?
78-------------------------------
79
80 * Now that visionmedia/mocha#1059 is released the process.exit hack could be tidier
81
82 * If visionmedia/mocha#1061 is accepted upstream, I only need to hijack stdout, and can leave stderr alone
83
84 * Having each reporter run in a child process would make it eaiser to capture their streams, but might lead to other issues
85
86TODO
87----
88
89* Update hack now that visionmedia/mocha#1059 is merged
90* Add tests for coverage reports
91* Add tests which produce multiple reports at once
92* Add test for help text
93* Add test that uses --no-exit
94* Add test that doesn't use _mocha (maybe not?)