Skip to content

Commit

Permalink
Fixed a bug where configurations did not actually support extends
Browse files Browse the repository at this point in the history
  • Loading branch information
mf-pherlihy committed Jan 16, 2021
1 parent 30d5b66 commit 105d1aa
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
24 changes: 22 additions & 2 deletions lib/cli/options.js
Expand Up @@ -8,7 +8,10 @@
*/

const fs = require('fs');
const path = require('path');
const ansi = require('ansi-colors');
const {cwd} = require('../utils');
const applyExtends = require('yargs/lib/apply-extends');
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 @@ -123,7 +126,19 @@ const parse = (args = [], defaultValues = {}, ...configObjects) => {

const result = yargsParser.detailed(args, {
configuration,
configObjects,
configObjects: configObjects.map(configObject => {
// '_configPath' gets set by config.loadConfig()
const config = configObject || {};
const configPath =
typeof config._configPath === 'string'
? path.dirname(config._configPath)
: cwd();

// Remove the internal property
delete config._configPath;

return applyExtends(config, configPath, true);
}),
default: defaultValues,
coerce: coerceOpts,
narg: nargOpts,
Expand Down Expand Up @@ -157,7 +172,12 @@ const parse = (args = [], defaultValues = {}, ...configObjects) => {
const loadRc = (args = {}) => {
if (args.config !== false) {
const config = args.config || findConfig();
return config ? loadConfig(config) : {};
const configObject = config ? loadConfig(config) : {};

// Set _configPath for use by parse()
configObject._configPath = config;

return configObject;
}
};

Expand Down
6 changes: 6 additions & 0 deletions test/integration/fixtures/config/mocharc-extended/base.json
@@ -0,0 +1,6 @@
{
"require": ["foo", "bar"],
"bail": true,
"reporter": "dot",
"slow": 60
}
@@ -0,0 +1,3 @@
{
"extends": "./modifiers.json"
}
@@ -0,0 +1,5 @@
{
"extends": "./base.json",
"reporter": "html",
"slow": 30
}
23 changes: 23 additions & 0 deletions test/integration/options.spec.js
@@ -0,0 +1,23 @@
'use strict';

var path = require('path');
var loadOptions = require('../../lib/cli/options').loadOptions;

describe('options', function() {
it('Should support extended options', function() {
var configDir = path.join(
__dirname,
'fixtures',
'config',
'mocharc-extended'
);
var extended = loadOptions([
'--config',
path.join(configDir, 'extends.json')
]);
expect(extended.require, 'to equal', ['foo', 'bar']);
expect(extended.bail, 'to equal', true);
expect(extended.reporter, 'to equal', 'html');
expect(extended.slow, 'to equal', 30);
});
});

0 comments on commit 105d1aa

Please sign in to comment.