Skip to content

Commit

Permalink
Eslint (YOU54F#63)
Browse files Browse the repository at this point in the history
* - Testing: Get to 100% coverage

* - Apply some common rules; simplify

* - Docs: Use `const` in README
  • Loading branch information
brettz9 authored May 11, 2020
1 parent 184db91 commit e754c50
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 72 deletions.
14 changes: 13 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,23 @@ module.exports = {
env: {
node: true
},
parserOptions: {
ecmaVersion: 2015
},
rules: {
'brace-style': [2, 'stroustrup', {allowSingleLine: true}],
'no-console': 0,
strict: [2],
indent: [2, 4]
indent: [2, 4],

semi: ['error'],
'prefer-const': ['error'],
'no-var': ['error'],
'prefer-destructuring': ['error'],
'object-shorthand': ['error'],
quotes: ['error', 'single'],
'quote-props': ['error', 'as-needed'],
'prefer-template': ['error']
},
overrides: [
{
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ $ cat xunit-custom.xml
Note that when Mocha is called programmatically, it is passed an options object when created. This object is usually derived from a config file that your mocha test runner reads prior to instantiation. This is the object that must contain a key `reporter` with a value of `cypress-multi-reporters` for this plugin to be used. You can also pass the key `reporterOptions` with a value of any of the above listed config files (including the `reporterEnabled` subkey and any other plugin configuration information.) This removes the requirement to have an intermediate configuration file specifically for the multireporter configuration.

```js
var mocha = new Mocha({
const mocha = new Mocha({
reporter: "cypress-multi-reporters",
timeout: config.testTimeout || 60000,
slow: config.slow || 10000,
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';

var MultiReporters = require('./lib/MultiReporters');
const MultiReporters = require('./lib/MultiReporters');

module.exports = MultiReporters;
74 changes: 37 additions & 37 deletions lib/MultiReporters.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
'use strict';

var _ = {
const _ = {
get: require('lodash/get'),
camelCase: require('lodash/camelCase'),
size: require('lodash/size'),
after: require('lodash/after'),
merge: require('lodash/merge')
};
var debug = require('debug')('mocha:reporters:MultiReporters');
var fs = require('fs');
var util = require('util')
var mocha = require('mocha');
var Base = mocha.reporters.Base;
var path = require('path');
const debug = require('debug')('mocha:reporters:MultiReporters');
const fs = require('fs');
const util = require('util');
const mocha = require('mocha');
const {Base} = mocha.reporters;
const path = require('path');

var createStatsCollector;
var mocha6plus;
let createStatsCollector;
let mocha6plus;

try {
var json = JSON.parse(
fs.readFileSync(path.dirname(require.resolve('mocha')) + "/package.json", "utf8")
const json = JSON.parse(
fs.readFileSync(`${path.dirname(require.resolve('mocha')) }/package.json`, 'utf8')
);
var version = json.version;
const {version} = json;
// istanbul ignore else
if (version >= "6") {
createStatsCollector = require("mocha/lib/stats-collector");
if (version >= '6') {
createStatsCollector = require('mocha/lib/stats-collector');
mocha6plus = true;
}
else {
Expand All @@ -33,7 +33,7 @@ try {
}
catch (e) {
// istanbul ignore next
console.warn("Couldn't determine Mocha version");
console.warn('Couldn\'t determine Mocha version');
}

function MultiReporters(runner, options) {
Expand All @@ -44,7 +44,6 @@ function MultiReporters(runner, options) {
Base.call(this, runner);

if (_.get(options, 'execute', true)) {

options = this.getOptions(options);

this._reporters = _.get(options, 'reporterEnabled', 'tap').split(',').map(
Expand All @@ -53,13 +52,13 @@ function MultiReporters(runner, options) {

name = name.trim();

var reporterOptions = this.getReporterOptions(options, name);
const reporterOptions = this.getReporterOptions(options, name);

//
// Mocha Reporters
// https://github.com/mochajs/mocha/blob/master/lib/reporters/index.js
//
var Reporter = _.get(mocha, ['reporters', name], null);
let Reporter = _.get(mocha, ['reporters', name], null);

//
// External Reporters.
Expand All @@ -76,27 +75,28 @@ function MultiReporters(runner, options) {
Reporter = require(path.resolve(process.cwd(), name));
}
catch (_err) {
_err.message.indexOf('Cannot find module') !== -1 ? console.warn('"' + name + '" reporter not found')
: console.warn('"' + name + '" reporter blew up with error:\n' + _err.stack);
_err.message.indexOf('Cannot find module') !== -1 ? console.warn(`"${ name }" reporter not found`)
: console.warn(`"${ name }" reporter blew up with error:\n${ _err.stack}`);
}
}
else {
console.warn('"' + name + '" reporter blew up with error:\n' + err.stack);
console.warn(`"${ name }" reporter blew up with error:\n${ err.stack}`);
}
}
}

if (Reporter !== null) {
return new Reporter(
runner, {
reporterOptions: reporterOptions
reporterOptions
}
);
}
else {
console.error('Reporter not found!', name);
}
}.bind(this)
},
this
);
}
}
Expand All @@ -106,24 +106,24 @@ util.inherits(MultiReporters, Base);
MultiReporters.CONFIG_FILE = '../config.json';

MultiReporters.prototype.done = function (failures, fn) {
var numberOfReporters = _.size(this._reporters);
const numberOfReporters = _.size(this._reporters);

if (numberOfReporters === 0) {
console.error('Unable to invoke fn(failures) - no reporters were registered');
return;
}

var reportersWithDoneHandler = this._reporters.filter(function (reporter) {
const reportersWithDoneHandler = this._reporters.filter(function (reporter) {
return reporter && (typeof reporter.done === 'function');
});

var numberOfReportersWithDoneHandler = _.size(reportersWithDoneHandler);
const numberOfReportersWithDoneHandler = _.size(reportersWithDoneHandler);

if (numberOfReportersWithDoneHandler === 0) {
return fn(failures);
}

var done = _.after(numberOfReportersWithDoneHandler, function() {
const done = _.after(numberOfReportersWithDoneHandler, function() {
fn(failures);
});

Expand All @@ -134,16 +134,16 @@ MultiReporters.prototype.done = function (failures, fn) {

MultiReporters.prototype.getOptions = function (options) {
debug('options', options);
var resultantOptions = _.merge({}, this.getDefaultOptions(), this.getCustomOptions(options));
const resultantOptions = _.merge({}, this.getDefaultOptions(), this.getCustomOptions(options));
debug('options file (resultant)', resultantOptions);
return resultantOptions;
};

MultiReporters.prototype.getCustomOptions = function (options) {
var customOptionsFile = _.get(options, 'reporterOptions.configFile');
let customOptionsFile = _.get(options, 'reporterOptions.configFile');
debug('options file (custom)', customOptionsFile);

var customOptions;
let customOptions;
if (customOptionsFile) {
customOptionsFile = path.resolve(customOptionsFile);
debug('options file (custom)', customOptionsFile);
Expand All @@ -164,17 +164,17 @@ MultiReporters.prototype.getCustomOptions = function (options) {
}
}
else {
customOptions = _.get(options, "reporterOptions");
customOptions = _.get(options, 'reporterOptions');
}

return customOptions;
};

MultiReporters.prototype.getDefaultOptions = function () {
var defaultOptionsFile = require.resolve(MultiReporters.CONFIG_FILE);
const defaultOptionsFile = require.resolve(MultiReporters.CONFIG_FILE);
debug('options file (default)', defaultOptionsFile);

var defaultOptions = fs.readFileSync(defaultOptionsFile).toString();
let defaultOptions = fs.readFileSync(defaultOptionsFile).toString();
debug('options (default)', defaultOptions);

try {
Expand All @@ -189,15 +189,15 @@ MultiReporters.prototype.getDefaultOptions = function () {
};

MultiReporters.prototype.getReporterOptions = function (options, name) {
var _name = name;
var commonReporterOptions = _.get(options, 'reporterOptions', {});
const _name = name;
const commonReporterOptions = _.get(options, 'reporterOptions', {});
debug('reporter options (common)', _name, commonReporterOptions);

name = [_.camelCase(name), 'ReporterOptions'].join('');
var customReporterOptions = _.get(options, [name], {});
const customReporterOptions = _.get(options, [name], {});
debug('reporter options (custom)', _name, customReporterOptions);

var resultantReporterOptions = _.merge({}, commonReporterOptions, customReporterOptions);
const resultantReporterOptions = _.merge({}, commonReporterOptions, customReporterOptions);
debug('reporter options (resultant)', _name, resultantReporterOptions);

return resultantReporterOptions;
Expand Down
7 changes: 7 additions & 0 deletions tests/custom-bad-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"reporterEnabled": "badname",

"badnameReporterOptions": {
"output": "artifacts/test/custom-xunit.xml"
}
}
7 changes: 7 additions & 0 deletions tests/custom-erring-external-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"reporterEnabled": "dot, mocha/README.md",

"xunitReporterOptions": {
"output": "artifacts/test/custom-xunit.xml"
}
}
7 changes: 7 additions & 0 deletions tests/custom-erring-internal-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"reporterEnabled": "dot, ./tests/custom-erring-internal-reporter.js",

"xunitReporterOptions": {
"output": "artifacts/test/custom-xunit.xml"
}
}
16 changes: 16 additions & 0 deletions tests/custom-erring-internal-reporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

const mocha = require('mocha');
const {Base} = mocha.reporters;

/**
* This is a project-local custom reporter which is used as a stub in tests
* to verify if loading reporters from a path (absolute or relative) is successful
*/
function CustomReporterStub(runner) {
Base.call(this, runner);
}

module.exports = CustomReporterStub;

throw new Error('Oops');
10 changes: 5 additions & 5 deletions tests/custom-external-config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

module.exports = {
"reporterEnabled": "mocha-junit-reporter",
reporterEnabled: 'mocha-junit-reporter',

"mochaJunitReporterReporterOptions": {
"id": "mocha-junit-reporter",
"mochaFile": "junit.xml"
mochaJunitReporterReporterOptions: {
id: 'mocha-junit-reporter',
mochaFile: 'junit.xml'
}
}
};
4 changes: 2 additions & 2 deletions tests/custom-internal-reporter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var mocha = require('mocha');
var Base = mocha.reporters.Base;
const mocha = require('mocha');
const {Base} = mocha.reporters;

/**
* This is a project-local custom reporter which is used as a stub in tests
Expand Down
Loading

0 comments on commit e754c50

Please sign in to comment.