From 0bda388c840646d29eab25ba67b992e76b832fb2 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Sat, 18 Jan 2025 01:26:39 +0100 Subject: [PATCH] fix(cli): add proper `extends` support for `package.json` files --- lib/cli/config.js | 7 ------- lib/cli/options.js | 20 +++++++++++++++++-- .../config/mocharc-extended/package-lock.json | 6 ++++++ test/integration/options.spec.js | 1 + 4 files changed, 25 insertions(+), 9 deletions(-) create mode 100644 test/integration/fixtures/config/mocharc-extended/package-lock.json diff --git a/lib/cli/config.js b/lib/cli/config.js index a18443296b..ac719833cf 100644 --- a/lib/cli/config.js +++ b/lib/cli/config.js @@ -11,7 +11,6 @@ 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'); @@ -77,12 +76,6 @@ exports.loadConfig = filepath => { } else { config = parsers.json(filepath); } - - const {$0, ...options} = yargs(config._, path.dirname(filepath)) - .parserConfiguration(require('./options').YARGS_PARSER_CONFIG) - .config(config).argv; - - config = options; } catch (err) { throw createUnparsableFileError( `Unable to read/parse ${filepath}: ${err}`, 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/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 index b9a2b659f0..6f03fca663 100644 --- a/test/integration/options.spec.js +++ b/test/integration/options.spec.js @@ -45,6 +45,7 @@ describe('options', function () { it('Should support extended options using package.json', function () { var extended = loadOptions([ + '--no-config', '--package', path.join(workspaceDir, 'package-lock.json') ]);