From 22831c5bdbcd751d08804097fa04910c96ed6903 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Wed, 27 Feb 2019 18:51:53 +0100 Subject: [PATCH] Add support for configuration files with ".jsonc" extension (#3760); closes #3753 --- docs/index.md | 3 ++- example/config/.mocharc.jsonc | 14 ++++++++++++++ lib/cli/config.js | 1 + test/node-unit/cli/config.spec.js | 19 +++++++++++++++---- 4 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 example/config/.mocharc.jsonc diff --git a/docs/index.md b/docs/index.md index d746a1bdb2..34dd8973a0 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 0000000000..d86055f870 --- /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 458840e8d2..dd8f987ca1 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 78f1dd5d00..d5d64ee593 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() {