Skip to content

Commit

Permalink
fix(cli): add proper extends support for package.json files
Browse files Browse the repository at this point in the history
  • Loading branch information
manuth committed Jan 18, 2025
1 parent 1a85763 commit a735d7a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
7 changes: 0 additions & 7 deletions lib/cli/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const fs = require('fs');
const path = require('path');
const debug = require('debug')('mocha:cli:config');
const findUp = require('find-up');
const yargs = require('yargs/yargs');
const {createUnparsableFileError} = require('../errors');
const utils = require('../utils');

Expand Down Expand Up @@ -77,12 +76,6 @@ exports.loadConfig = filepath => {
} else {
config = parsers.json(filepath);
}

const {$0, ...options} = yargs(config._, path.dirname(filepath))
.parserConfiguration(require('./options').YARGS_PARSER_CONFIG)
.config(config).argv;

config = options;
} catch (err) {
throw createUnparsableFileError(
`Unable to read/parse ${filepath}: ${err}`,
Expand Down
20 changes: 18 additions & 2 deletions lib/cli/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
*/

const fs = require('fs');
const path = require('path');
const ansi = require('ansi-colors');
const yargs = require('yargs/yargs');
const yargsParser = require('yargs-parser');
const {types, aliases} = require('./run-option-metadata');
const {ONE_AND_DONE_ARGS} = require('./one-and-dones');
Expand Down Expand Up @@ -159,7 +161,7 @@ const parse = (args = [], defaultValues = {}, ...configObjects) => {
const loadRc = (args = {}) => {
if (args.config !== false) {
const config = args.config || findConfig();
return config ? loadConfig(config) : {};
return config ? loadRcFile(loadConfig(config), config) : {};
}
};

Expand All @@ -185,7 +187,7 @@ const loadPkgRc = (args = {}) => {
const pkg = JSON.parse(fs.readFileSync(filepath, 'utf8'));
if (pkg.mocha) {
debug('`mocha` prop of package.json parsed: %O', pkg.mocha);
result = pkg.mocha;
result = loadRcFile(pkg.mocha, filepath);
} else {
debug('no config found in %s', filepath);
}
Expand All @@ -204,6 +206,20 @@ const loadPkgRc = (args = {}) => {

module.exports.loadPkgRc = loadPkgRc;

/**
* Loads the inherited configuration of the rc file located at the specified {@linkcode filePath}.
*
* @param {Object} config - The parsed configuration of the rc file.
* @param {string} filePath - The name of the file containing the parsed configuration.
*/
const loadRcFile = (config, filePath) => {
const {$0, ...options} = yargs(config._, path.dirname(filePath))
.parserConfiguration(configuration)
.config(config).argv;

return options;
};

/**
* Priority list:
*
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/integration/options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe('options', function () {

it('Should support extended options using package.json', function () {
var extended = loadOptions([
'--no-config',
'--package',
path.join(workspaceDir, 'package-lock.json')
]);
Expand Down

0 comments on commit a735d7a

Please sign in to comment.