diff --git a/lib/cli/options.js b/lib/cli/options.js index 95428c4f96..473d17a91d 100644 --- a/lib/cli/options.js +++ b/lib/cli/options.js @@ -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/helpers'); const yargsParser = require('yargs-parser'); const {types, aliases} = require('./run-option-metadata'); const {ONE_AND_DONE_ARGS} = require('./one-and-dones'); @@ -124,7 +127,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, @@ -158,7 +173,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; } }; diff --git a/test/integration/fixtures/config/mocharc-extended/base.json b/test/integration/fixtures/config/mocharc-extended/base.json new file mode 100644 index 0000000000..89c92825f9 --- /dev/null +++ b/test/integration/fixtures/config/mocharc-extended/base.json @@ -0,0 +1,6 @@ +{ + "require": ["foo", "bar"], + "bail": true, + "reporter": "dot", + "slow": 60 +} diff --git a/test/integration/fixtures/config/mocharc-extended/extends.json b/test/integration/fixtures/config/mocharc-extended/extends.json new file mode 100644 index 0000000000..715639efd7 --- /dev/null +++ b/test/integration/fixtures/config/mocharc-extended/extends.json @@ -0,0 +1,3 @@ +{ + "extends": "./modifiers.json" +} diff --git a/test/integration/fixtures/config/mocharc-extended/modifiers.json b/test/integration/fixtures/config/mocharc-extended/modifiers.json new file mode 100644 index 0000000000..8e6dcb1d3c --- /dev/null +++ b/test/integration/fixtures/config/mocharc-extended/modifiers.json @@ -0,0 +1,5 @@ +{ + "extends": "./base.json", + "reporter": "html", + "slow": 30 +} diff --git a/test/integration/options.spec.js b/test/integration/options.spec.js new file mode 100644 index 0000000000..f4674b7543 --- /dev/null +++ b/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); + }); +});