Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: extends not working for some settings #4980

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions lib/cli/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) : {};
}
};

Expand Down Expand Up @@ -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);
}
Expand All @@ -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:
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./extends.json"
}
6 changes: 6 additions & 0 deletions test/integration/fixtures/config/mocharc-extended/base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"require": ["foo", "bar"],
"bail": true,
"reporter": "dot",
"slow": 60
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./modifiers.json",
"_": ["**/*.spec.js"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "./base.json",
"reporter": "html",
"slow": 30
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions test/integration/options.spec.js
manuth marked this conversation as resolved.
Show resolved Hide resolved
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict';

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

describe('options', function () {
var workingDirectory;
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
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');
});
});
Loading