diff --git a/lib/cli/options.js b/lib/cli/options.js index 09351957b4..bd4b115a7f 100644 --- a/lib/cli/options.js +++ b/lib/cli/options.js @@ -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, @@ -212,7 +214,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) : {}; } }; @@ -254,7 +256,7 @@ const loadPkgRc = (args = {}) => { const pkg = JSON.parse(configData); 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); } @@ -271,6 +273,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: * diff --git a/test/integration/fixtures/config/mocharc-extended/.mocharc.json b/test/integration/fixtures/config/mocharc-extended/.mocharc.json new file mode 100644 index 0000000000..98aae677ca --- /dev/null +++ b/test/integration/fixtures/config/mocharc-extended/.mocharc.json @@ -0,0 +1,3 @@ +{ + "extends": "./extends.json" +} 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..54cb7122c5 --- /dev/null +++ b/test/integration/fixtures/config/mocharc-extended/extends.json @@ -0,0 +1,4 @@ +{ + "extends": "./modifiers.json", + "_": ["**/*.spec.js"] +} 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/fixtures/config/mocharc-extended/package-lock.json b/test/integration/fixtures/config/mocharc-extended/package-lock.json new file mode 100644 index 0000000000..12dd16138f --- /dev/null +++ b/test/integration/fixtures/config/mocharc-extended/package-lock.json @@ -0,0 +1,6 @@ +{ + "mocha": { + "extends": "./extends.json", + "grep": "package" + } +} diff --git a/test/integration/options.spec.js b/test/integration/options.spec.js new file mode 100644 index 0000000000..6f03fca663 --- /dev/null +++ b/test/integration/options.spec.js @@ -0,0 +1,58 @@ +'use strict'; + +var path = require('path'); +var loadOptions = require('../../lib/cli/options').loadOptions; + +describe('options', function () { + var workingDirectory; + const workspaceDir = path.join( + __dirname, + 'fixtures', + 'config', + 'mocharc-extended' + ); + const configFile = path.join(workspaceDir, 'extends.json'); + + beforeEach(function () { + workingDirectory = process.cwd(); + process.chdir(workspaceDir); + }); + + afterEach(function () { + process.chdir(workingDirectory); + }); + + it('Should load `_`-option properly', function () { + var extended = loadOptions(['--config', configFile]); + expect(extended._, 'to equal', ['**/*.spec.js']); + }); + + it('Should support extended options using --config parameter', function () { + var extended = loadOptions(['--config', configFile]); + 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); + }); + + it('Should support extended options using rc file', function () { + var extended = loadOptions([]); + 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); + }); + + it('Should support extended options using package.json', function () { + var extended = loadOptions([ + '--no-config', + '--package', + path.join(workspaceDir, 'package-lock.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); + expect(extended.grep, 'to equal', 'package'); + }); +});