Skip to content

Commit a735d7a

Browse files
committed
fix(cli): add proper extends support for package.json files
1 parent 1a85763 commit a735d7a

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

Diff for: 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}`,

Diff for: 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 {types, aliases} = require('./run-option-metadata');
1416
const {ONE_AND_DONE_ARGS} = require('./one-and-dones');
@@ -159,7 +161,7 @@ const parse = (args = [], defaultValues = {}, ...configObjects) => {
159161
const loadRc = (args = {}) => {
160162
if (args.config !== false) {
161163
const config = args.config || findConfig();
162-
return config ? loadConfig(config) : {};
164+
return config ? loadRcFile(loadConfig(config), config) : {};
163165
}
164166
};
165167

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

205207
module.exports.loadPkgRc = loadPkgRc;
206208

209+
/**
210+
* Loads the inherited configuration of the rc file located at the specified {@linkcode filePath}.
211+
*
212+
* @param {Object} config - The parsed configuration of the rc file.
213+
* @param {string} filePath - The name of the file containing the parsed configuration.
214+
*/
215+
const loadRcFile = (config, filePath) => {
216+
const {$0, ...options} = yargs(config._, path.dirname(filePath))
217+
.parserConfiguration(configuration)
218+
.config(config).argv;
219+
220+
return options;
221+
};
222+
207223
/**
208224
* Priority list:
209225
*

Diff for: 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.

Diff for: 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)