Skip to content

Commit d5da485

Browse files
committed
Initial commit
1 parent 8df677d commit d5da485

File tree

9 files changed

+212
-5
lines changed

9 files changed

+212
-5
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
node_modules/
1+
node_modules/
2+
test/ouput/

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test/
2+
.travis.yml

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
language: node_js
2+
node_js:
3+
- "iojs"
4+
- "0.12"
5+
- "0.11"
6+
- "0.10"
7+
matrix:
8+
fast_finish: true
9+
sudo: false

README.md

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,47 @@
1-
# mocha-circleci-reporter
2-
A mocha reporter that supports Circle CI, via a combined jUnit and Spec reporter output.
1+
# mocha-circleci-reporter [![Build Status](https://travis-ci.org/sandcastle/mocha-circleci-reporter.svg)](https://travis-ci.org/sandcastle/mocha-circleci-reporter)
2+
3+
> A Mocha reporter specifically for [Circle CI](https://circleci.com/).
4+
5+
6+
## Getting Started
7+
8+
Install the reporter as a development dependency:
9+
10+
```sh
11+
npm install mocha --save-dev
12+
npm install mocha-circleci-reporter --save-dev
13+
```
14+
15+
Update your `package.json` to use the reporter when running Mocha:
16+
17+
```json
18+
{
19+
"name": "my-package",
20+
"version": "0.0.1",
21+
"scripts": {
22+
"test": "node_modules/.bin/mocha --reporter mocha-circleci-reporter test/*.js"
23+
}
24+
}
25+
```
26+
27+
28+
## Background
29+
30+
### Why another reporter?
31+
32+
As of Mocha 2.x, its not possible to use multple reporters out of the box.
33+
This complicates things when dealing with CI systems like Circle CI that
34+
require a format such as [jUnit XML](https://windyroad.com.au/dl/Open%20Source/JUnit.xsd)
35+
as a lot of the goodness that is written to console when running the default
36+
reporter (`Spec`).
37+
38+
To overcome this, the `mocha-circleci-reporter` report combines both the
39+
builtin `Spec` and `mocha-junit-report` reporters.
40+
41+
### Mocha 3.x
42+
43+
There is work underway in Mocha 3.x to move to a plugin architecture that would
44+
make multiple reporters dead simple, until then I hope this simplifies things.
45+
46+
https://github.com/mochajs/mocha/issues/1457
47+

index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use-strict';
2+
3+
var Spec = require('mocha').reporters.Spec;
4+
var JUnit = require('mocha-junit-reporter');
5+
6+
7+
function MochaCircleCIReporter(runner, options) {
8+
new Spec(runner, options);
9+
new JUnit(runner, options);
10+
}
11+
12+
module.exports = MochaCircleCIReporter;

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "A mocha reporter that supports Cicle CI, via a combined jUnit and Spec reporter output.",
55
"main": "index.js",
66
"scripts": {
7-
"test": "mocha --harmony test/**/*.js"
7+
"test": "node_modules/.bin/mocha --harmony test/index.js"
88
},
99
"repository": {
1010
"type": "git",
@@ -28,6 +28,10 @@
2828
"homepage": "https://github.com/sandcastle/mocha-circleci-reporter#readme",
2929
"devDependencies": {
3030
"assert": "^1.3.0",
31-
"mocha": "^2.2.5"
31+
"test-console": "^1.0.0"
32+
},
33+
"dependencies": {
34+
"mocha": "^2.2.5",
35+
"mocha-junit-reporter": "^1.6.1"
3236
}
3337
}

test/helpers/mock-runner.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
var EventEmitter = require('events').EventEmitter;
4+
var util = require('util');
5+
6+
// mock test runner
7+
function Runner() {
8+
Runner.super_.call(this);
9+
}
10+
11+
util.inherits(Runner, EventEmitter);
12+
13+
Runner.prototype.start = function() {
14+
this.emit('start');
15+
};
16+
17+
Runner.prototype.end = function() {
18+
this.emit('end');
19+
};
20+
21+
Runner.prototype.startSuite = function(suite) {
22+
this.emit('suite', suite);
23+
};
24+
25+
Runner.prototype.pass = function(test) {
26+
this.emit('pass', test);
27+
this.endTest();
28+
};
29+
30+
Runner.prototype.fail = function(test, reason) {
31+
this.emit('fail', test, reason);
32+
this.endTest();
33+
};
34+
35+
Runner.prototype.endTest = function() {
36+
this.emit('end test');
37+
};
38+
39+
module.exports = Runner;

test/helpers/mock-test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
function Test(fullTitle, title, duration) {
4+
5+
this.title = title;
6+
this.duration = duration;
7+
8+
this.fullTitle = function() {
9+
return fullTitle;
10+
};
11+
12+
this.slow = function() { };
13+
}
14+
15+
module.exports = Test

test/index.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
'use-strict';
2+
/* global describe, it, beforeEach */
3+
4+
var Reporter = require('../index');
5+
var Runner = require('./helpers/mock-runner');
6+
var Test = require('./helpers/mock-test');
7+
var testConsole = require('test-console');
8+
var assert = require('assert');
9+
var fs = require('fs');
10+
11+
12+
describe('mocha-circleci-reporter', function() {
13+
14+
var runner;
15+
16+
beforeEach(function() {
17+
runner = new Runner();
18+
});
19+
20+
it('should output spec to stdout', function() {
21+
22+
new Reporter(runner, {
23+
reporterOptions: { mochaFile: 'test/output/console.xml' }
24+
});
25+
26+
var stdout = testConsole.stdout.inspect();
27+
try{
28+
executeTestRunner();
29+
}
30+
finally{
31+
stdout.restore();
32+
}
33+
34+
assert(stdout.output[1], '\u001b[0mFoo Bar module\u001b[0m\n');
35+
});
36+
37+
it('should output junit file', function() {
38+
39+
new Reporter(runner, {
40+
reporterOptions: { mochaFile: 'test/output/console.xml' }
41+
});
42+
43+
var stdout = testConsole.stdout.inspect();
44+
try{
45+
executeTestRunner();
46+
47+
var file = './test/output/console.xml';
48+
assert(fs.existsSync(file));
49+
fs.unlinkSync(file);
50+
}
51+
finally{
52+
stdout.restore();
53+
}
54+
});
55+
56+
function executeTestRunner(char){
57+
58+
char = char || '';
59+
runner.start();
60+
61+
runner.startSuite({
62+
title: 'Foo Bar module',
63+
tests: [1, 2]
64+
});
65+
66+
runner.pass(new Test('Foo can weez the juice', 'can weez the juice', 1));
67+
runner.fail(new Test('Bar can narfle the garthog', 'can narfle the garthog', 1), {
68+
message: char + 'expected garthog to be dead' + char
69+
});
70+
71+
runner.startSuite({
72+
title: 'Another suite!',
73+
tests: [1]
74+
});
75+
runner.pass(new Test('Another suite', 'works', 4));
76+
77+
runner.end();
78+
}
79+
80+
});

0 commit comments

Comments
 (0)