Skip to content

Commit 22831c5

Browse files
sstephantboneskull
authored andcommitted
Add support for configuration files with ".jsonc" extension (#3760); closes #3753
1 parent 9e95d36 commit 22831c5

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

docs/index.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1627,7 +1627,7 @@ In addition to supporting the legacy [`mocha.opts`](#mochaopts) run-control form
16271627

16281628
- **JavaScript**: Create a `.mocharc.js` in your project's root directory, and export an object (`module.exports = {/* ... */}`) containing your configuration.
16291629
- **YAML**: Create a `.mocharc.yaml` (or `.mocharc.yml`) in your project's root directory.
1630-
- **JSON**: Create a `.mocharc.json` in your project's root directory. Comments — while not valid JSON — are allowed in this file, and will be ignored by Mocha.
1630+
- **JSON**: Create a `.mocharc.json` (or `.mocharc.jsonc`) in your project's root directory. Comments — while not valid JSON — are allowed in this file, and will be ignored by Mocha.
16311631
- **`package.json`**: Create a `mocha` property in your project's `package.json`.
16321632

16331633
Mocha suggests using one of the above strategies for configuration instead of the legacy `mocha.opts` format.
@@ -1649,6 +1649,7 @@ If no custom path was given, and if there are multiple configuration files in th
16491649
1. `.mocharc.js`
16501650
1. `.mocharc.yaml`
16511651
1. `.mocharc.yml`
1652+
1. `.mocharc.jsonc`
16521653
1. `.mocharc.json`
16531654

16541655
### Merging

example/config/.mocharc.jsonc

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// This config file contains Mocha's defaults.
2+
// As you can see, comments are allowed.
3+
// This same configuration could be provided in the `mocha` property of your
4+
// project's `package.json`.
5+
{
6+
"diff": true,
7+
"extension": ["js"],
8+
"opts": "./test/mocha.opts",
9+
"package": /* 📦 */ "./package.json",
10+
"reporter": /* 📋 */ "spec",
11+
"slow": 75,
12+
"timeout": 2000,
13+
"ui": "bdd"
14+
}

lib/cli/config.js

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ exports.CONFIG_FILES = [
2424
'.mocharc.js',
2525
'.mocharc.yaml',
2626
'.mocharc.yml',
27+
'.mocharc.jsonc',
2728
'.mocharc.json'
2829
];
2930

test/node-unit/cli/config.spec.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('cli/config', function() {
2424
sandbox.stub(parsers, 'js').returns(config);
2525
});
2626

27-
describe('when supplied a filepath with .yaml extension', function() {
27+
describe('when supplied a filepath with ".yaml" extension', function() {
2828
const filepath = 'foo.yaml';
2929

3030
it('should use the YAML parser', function() {
@@ -35,7 +35,7 @@ describe('cli/config', function() {
3535
});
3636
});
3737

38-
describe('when supplied a filepath with .yml extension', function() {
38+
describe('when supplied a filepath with ".yml" extension', function() {
3939
const filepath = 'foo.yml';
4040

4141
it('should use the YAML parser', function() {
@@ -46,7 +46,7 @@ describe('cli/config', function() {
4646
});
4747
});
4848

49-
describe('when supplied a filepath with .js extension', function() {
49+
describe('when supplied a filepath with ".js" extension', function() {
5050
const filepath = 'foo.js';
5151

5252
it('should use the JS parser', function() {
@@ -57,7 +57,18 @@ describe('cli/config', function() {
5757
});
5858
});
5959

60-
describe('when supplied a filepath with .json extension', function() {
60+
describe('when supplied a filepath with ".jsonc" extension', function() {
61+
const filepath = 'foo.jsonc';
62+
63+
it('should use the JSON parser', function() {
64+
loadConfig('foo.jsonc');
65+
expect(parsers.json, 'to have calls satisfying', [
66+
{args: [filepath], returned: config}
67+
]).and('was called times', 1);
68+
});
69+
});
70+
71+
describe('when supplied a filepath with ".json" extension', function() {
6172
const filepath = 'foo.json';
6273

6374
it('should use the JSON parser', function() {

0 commit comments

Comments
 (0)