diff --git a/lib/cli/config.js b/lib/cli/config.js index ac719833cf..dcd0defd35 100644 --- a/lib/cli/config.js +++ b/lib/cli/config.js @@ -11,6 +11,7 @@ const fs = require('fs'); const path = require('path'); const debug = require('debug')('mocha:cli:config'); const findUp = require('find-up'); +const yargs = require('yargs/yargs'); const {createUnparsableFileError} = require('../errors'); const utils = require('../utils'); @@ -76,6 +77,10 @@ exports.loadConfig = filepath => { } else { config = parsers.json(filepath); } + + config = yargs(undefined, path.dirname(filepath)) + .parserConfiguration(require('./options').YARGS_PARSER_CONFIG) + .config(config); } catch (err) { throw createUnparsableFileError( `Unable to read/parse ${filepath}: ${err}`, 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..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..77841ccdd9 --- /dev/null +++ b/test/integration/options.spec.js @@ -0,0 +1,42 @@ +'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' + ); + + beforeEach(function () { + workingDirectory = process.cwd(); + process.chdir(workspaceDir); + }); + + afterEach(function () { + process.chdir(workingDirectory); + }); + + it('Should support extended options using --config parameter', function () { + var extended = loadOptions([ + '--config', + path.join(workspaceDir, '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); + }); + + 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); + }); +});