Skip to content

Commit

Permalink
fix: Make mocha-junit-reporter work with Mocha 6 (#80)
Browse files Browse the repository at this point in the history
This change is compatible with mocha 6+ and earlier versions
  • Loading branch information
YOU54F authored and clayreimann committed Jun 7, 2019
1 parent 74ca404 commit b28d669
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
7 changes: 4 additions & 3 deletions .travis.yml
@@ -1,8 +1,9 @@
language: node_js
node_js:
- '6'
- '8'
- '10'
- "6"
- "8"
- "10"
script: npm test
deploy:
provider: npm
email: $NPM_EMAIL
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -200,4 +200,4 @@ output line 2
[travis-build]: https://travis-ci.org/michaelleeallen/mocha-junit-reporter
[npm-badge]: https://img.shields.io/npm/v/mocha-junit-reporter.svg?maxAge=2592000
[npm-listing]: https://www.npmjs.com/package/mocha-junit-reporter
[ant-schema]: http://windyroad.org/dl/Open%20Source/JUnit.xsd
[ant-schema]: http://windyroad.org/dl/Open%20Source/JUnit.xsd
20 changes: 20 additions & 0 deletions index.js
Expand Up @@ -9,6 +9,23 @@ var mkdirp = require('mkdirp');
var md5 = require('md5');
var stripAnsi = require('strip-ansi');

var createStatsCollector;
var mocha6plus;

try {
var json = JSON.parse(
fs.readFileSync("./node_modules/mocha/package.json", "utf8")
);
version = json.version;
if (version >= "6") {
createStatsCollector = require("mocha/lib/stats-collector");
mocha6plus = true;
} else {
mocha6plus = false;
}
} catch (e) {
console.warn("Couldn't determine Mocha version");
}
module.exports = MochaJUnitReporter;

// A subset of invalid characters as defined in http://www.w3.org/TR/xml/#charsets that can occur in e.g. stacktraces
Expand Down Expand Up @@ -155,6 +172,9 @@ function getJenkinsClassname (test) {
* @param {Object} options - mocha options
*/
function MochaJUnitReporter(runner, options) {
if (mocha6plus) {
createStatsCollector(runner);
}
this._options = configureDefaults(options);
this._runner = runner;
this._generateSuiteTitle = this._options.useFullSuiteTitle ? fullSuiteTitle : defaultSuiteTitle;
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "mocha-junit-reporter",
"version": "1.22.0",
"version": "1.23.0",
"description": "A JUnit reporter for mocha.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit b28d669

Please sign in to comment.