Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix improper warnings for invalid reporters #4275

Merged
merged 3 commits into from May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion .eslintrc.yml
Expand Up @@ -14,6 +14,11 @@ rules:
strict:
- error
- safe
# disallow Object.assign
no-restricted-properties:
- error
- object: 'Object'
property: 'assign'
overrides:
- files:
- scripts/**/*.js
Expand All @@ -31,6 +36,11 @@ overrides:
ecmaVersion: 2018
env:
browser: false
rules:
no-restricted-properties:
- off
- object: 'Object'
property: 'assign'
- files:
- esm-utils.js
parserOptions:
Expand Down Expand Up @@ -79,7 +89,6 @@ overrides:
# disallow property access of `global.<timer>.*`
- selector: '*[object.object.name=global][object.property.name=/(Date|(set|clear)(Timeout|Immediate|Interval))/]:expression'
message: *GH-237

- files:
- test/**/*.mjs
parserOptions:
Expand Down
3 changes: 2 additions & 1 deletion lib/cli/cli.js
Expand Up @@ -19,6 +19,7 @@ const {loadOptions, YARGS_PARSER_CONFIG} = require('./options');
const commands = require('./commands');
const ansi = require('ansi-colors');
const {repository, homepage, version, gitter} = require('../../package.json');
const {cwd} = require('../utils');

/**
* - Accepts an `Array` of arguments
Expand All @@ -31,7 +32,7 @@ exports.main = (argv = process.argv.slice(2)) => {
debug('entered main with raw args', argv);
// ensure we can require() from current working directory
if (typeof module.paths !== 'undefined') {
module.paths.push(process.cwd(), path.resolve('node_modules'));
module.paths.push(cwd(), path.resolve('node_modules'));
}

Error.stackTraceLimit = Infinity; // configurable via --stack-trace-limit?
Expand Down
3 changes: 2 additions & 1 deletion lib/cli/config.js
Expand Up @@ -11,6 +11,7 @@ const fs = require('fs');
const path = require('path');
const debug = require('debug')('mocha:cli:config');
const findUp = require('find-up');
const utils = require('../utils');

/**
* These are the valid config files, in order of precedence;
Expand Down Expand Up @@ -92,7 +93,7 @@ exports.loadConfig = filepath => {
* @param {string} [cwd] - Current working directory
* @returns {string|null} Filepath to config, if found
*/
exports.findConfig = (cwd = process.cwd()) => {
exports.findConfig = (cwd = utils.cwd()) => {
const filepath = findUp.sync(exports.CONFIG_FILES, {cwd});
if (filepath) {
debug('findConfig: found config file %s', filepath);
Expand Down
18 changes: 9 additions & 9 deletions lib/mocha.js
Expand Up @@ -58,7 +58,7 @@ var mochaStates = utils.defineConstants({
*/

if (!process.browser && typeof module.paths !== 'undefined') {
var cwd = process.cwd();
var cwd = utils.cwd();
module.paths.push(cwd, path.join(cwd, 'node_modules'));
}

Expand Down Expand Up @@ -231,24 +231,24 @@ Mocha.prototype.reporter = function(reporter, reporterOptions) {
_reporter = require(reporter);
} catch (err) {
if (
err.code !== 'MODULE_NOT_FOUND' ||
err.message.indexOf('Cannot find module') !== -1
err.code === 'MODULE_NOT_FOUND' ||
err.message.indexOf('Cannot find module') >= 0
) {
// Try to load reporters from a path (absolute or relative)
try {
_reporter = require(path.resolve(process.cwd(), reporter));
_reporter = require(path.resolve(utils.cwd(), reporter));
} catch (_err) {
_err.code !== 'MODULE_NOT_FOUND' ||
_err.message.indexOf('Cannot find module') !== -1
? console.warn(sQuote(reporter) + ' reporter not found')
: console.warn(
_err.code === 'MODULE_NOT_FOUND' ||
_err.message.indexOf('Cannot find module') >= 0
? utils.warn(sQuote(reporter) + ' reporter not found')
boneskull marked this conversation as resolved.
Show resolved Hide resolved
: utils.warn(
sQuote(reporter) +
' reporter blew up with error:\n' +
err.stack
);
}
} else {
console.warn(
utils.warn(
sQuote(reporter) + ' reporter blew up with error:\n' + err.stack
);
}
Expand Down
12 changes: 11 additions & 1 deletion lib/utils.js
Expand Up @@ -637,7 +637,7 @@ exports.stackTraceFilter = function() {
var slash = path.sep;
var cwd;
if (is.node) {
cwd = process.cwd() + slash;
cwd = exports.cwd() + slash;
} else {
cwd = (typeof location === 'undefined'
? window.location
Expand Down Expand Up @@ -821,3 +821,13 @@ exports.supportsEsModules = function() {
}
}
};

/**
* Returns current working directory
*
* Wrapper around `process.cwd()` for isolation
* @private
*/
exports.cwd = function cwd() {
return process.cwd();
};