Skip to content

Commit 0bda388

Browse files
committed
fix(cli): add proper extends support for package.json files
1 parent 4254c8b commit 0bda388

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

lib/cli/config.js

-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const fs = require('fs');
1111
const path = require('path');
1212
const debug = require('debug')('mocha:cli:config');
1313
const findUp = require('find-up');
14-
const yargs = require('yargs/yargs');
1514
const {createUnparsableFileError} = require('../errors');
1615
const utils = require('../utils');
1716

@@ -77,12 +76,6 @@ exports.loadConfig = filepath => {
7776
} else {
7877
config = parsers.json(filepath);
7978
}
80-
81-
const {$0, ...options} = yargs(config._, path.dirname(filepath))
82-
.parserConfiguration(require('./options').YARGS_PARSER_CONFIG)
83-
.config(config).argv;
84-
85-
config = options;
8679
} catch (err) {
8780
throw createUnparsableFileError(
8881
`Unable to read/parse ${filepath}: ${err}`,

lib/cli/options.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
*/
99

1010
const fs = require('fs');
11+
const path = require('path');
1112
const ansi = require('ansi-colors');
13+
const yargs = require('yargs/yargs');
1214
const yargsParser = require('yargs-parser');
1315
const {
1416
types,
@@ -212,7 +214,7 @@ const parse = (args = [], defaultValues = {}, ...configObjects) => {
212214
const loadRc = (args = {}) => {
213215
if (args.config !== false) {
214216
const config = args.config || findConfig();
215-
return config ? loadConfig(config) : {};
217+
return config ? loadRcFile(loadConfig(config), config) : {};
216218
}
217219
};
218220

@@ -254,7 +256,7 @@ const loadPkgRc = (args = {}) => {
254256
const pkg = JSON.parse(configData);
255257
if (pkg.mocha) {
256258
debug('`mocha` prop of package.json parsed: %O', pkg.mocha);
257-
result = pkg.mocha;
259+
result = loadRcFile(pkg.mocha, filepath);
258260
} else {
259261
debug('no config found in %s', filepath);
260262
}
@@ -271,6 +273,20 @@ const loadPkgRc = (args = {}) => {
271273

272274
module.exports.loadPkgRc = loadPkgRc;
273275

276+
/**
277+
* Loads the inherited configuration of the rc file located at the specified {@linkcode filePath}.
278+
*
279+
* @param {Object} config - The parsed configuration of the rc file.
280+
* @param {string} filePath - The name of the file containing the parsed configuration.
281+
*/
282+
const loadRcFile = (config, filePath) => {
283+
const {$0, ...options} = yargs(config._, path.dirname(filePath))
284+
.parserConfiguration(configuration)
285+
.config(config).argv;
286+
287+
return options;
288+
};
289+
274290
/**
275291
* Priority list:
276292
*

test/integration/fixtures/config/mocharc-extended/package-lock.json

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/integration/options.spec.js

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ describe('options', function () {
4545

4646
it('Should support extended options using package.json', function () {
4747
var extended = loadOptions([
48+
'--no-config',
4849
'--package',
4950
path.join(workspaceDir, 'package-lock.json')
5051
]);

0 commit comments

Comments
 (0)