Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

Add support for configuration files with ".jsonc" extension (#3760); closes #3753

Sylvain22831c5bdbcd
diff --git a/docs/index.md b/docs/index.md
index d746a1bd..34dd8973 100644
--- a/docs/index.md
+++ b/docs/index.md
@@ -1627,7 +1627,7 @@ In addition to supporting the legacy [`mocha.opts`](#mochaopts) run-control form
- **JavaScript**: Create a `.mocharc.js` in your project's root directory, and export an object (`module.exports = {/* ... */}`) containing your configuration.
- **YAML**: Create a `.mocharc.yaml` (or `.mocharc.yml`) in your project's root directory.
-- **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.
+- **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.
- **`package.json`**: Create a `mocha` property in your project's `package.json`.
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
1. `.mocharc.js`
1. `.mocharc.yaml`
1. `.mocharc.yml`
+1. `.mocharc.jsonc`
1. `.mocharc.json`
### Merging
diff --git a/example/config/.mocharc.jsonc b/example/config/.mocharc.jsonc
new file mode 100644
index 00000000..d86055f8
--- /dev/null
+++ b/example/config/.mocharc.jsonc
@@ -0,0 +1,14 @@
+// This config file contains Mocha's defaults.
+// As you can see, comments are allowed.
+// This same configuration could be provided in the `mocha` property of your
+// project's `package.json`.
+{
+ "diff": true,
+ "extension": ["js"],
+ "opts": "./test/mocha.opts",
+ "package": /* 📦 */ "./package.json",
+ "reporter": /* 📋 */ "spec",
+ "slow": 75,
+ "timeout": 2000,
+ "ui": "bdd"
+}
diff --git a/lib/cli/config.js b/lib/cli/config.js
index 458840e8..dd8f987c 100644
--- a/lib/cli/config.js
+++ b/lib/cli/config.js
@@ -24,6 +24,7 @@ exports.CONFIG_FILES = [
'.mocharc.js',
'.mocharc.yaml',
'.mocharc.yml',
+ '.mocharc.jsonc',
'.mocharc.json'
];
diff --git a/test/node-unit/cli/config.spec.js b/test/node-unit/cli/config.spec.js
index 78f1dd5d..d5d64ee5 100644
--- a/test/node-unit/cli/config.spec.js
+++ b/test/node-unit/cli/config.spec.js
@@ -24,7 +24,7 @@ describe('cli/config', function() {
sandbox.stub(parsers, 'js').returns(config);
});
- describe('when supplied a filepath with .yaml extension', function() {
+ describe('when supplied a filepath with ".yaml" extension', function() {
const filepath = 'foo.yaml';
it('should use the YAML parser', function() {
@@ -35,7 +35,7 @@ describe('cli/config', function() {
});
});
- describe('when supplied a filepath with .yml extension', function() {
+ describe('when supplied a filepath with ".yml" extension', function() {
const filepath = 'foo.yml';
it('should use the YAML parser', function() {
@@ -46,7 +46,7 @@ describe('cli/config', function() {
});
});
- describe('when supplied a filepath with .js extension', function() {
+ describe('when supplied a filepath with ".js" extension', function() {
const filepath = 'foo.js';
it('should use the JS parser', function() {
@@ -57,7 +57,18 @@ describe('cli/config', function() {
});
});
- describe('when supplied a filepath with .json extension', function() {
+ describe('when supplied a filepath with ".jsonc" extension', function() {
+ const filepath = 'foo.jsonc';
+
+ it('should use the JSON parser', function() {
+ loadConfig('foo.jsonc');
+ expect(parsers.json, 'to have calls satisfying', [
+ {args: [filepath], returned: config}
+ ]).and('was called times', 1);
+ });
+ });
+
+ describe('when supplied a filepath with ".json" extension', function() {
const filepath = 'foo.json';
it('should use the JSON parser', function() {