Skip to content

Commit

Permalink
Add support for configuration files with ".jsonc" extension (#3760); c…
Browse files Browse the repository at this point in the history
…loses #3753
  • Loading branch information
sstephant authored and boneskull committed Feb 27, 2019
1 parent 9e95d36 commit 22831c5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
3 changes: 2 additions & 1 deletion docs/index.md
Expand Up @@ -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.
Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions 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"
}
1 change: 1 addition & 0 deletions lib/cli/config.js
Expand Up @@ -24,6 +24,7 @@ exports.CONFIG_FILES = [
'.mocharc.js',
'.mocharc.yaml',
'.mocharc.yml',
'.mocharc.jsonc',
'.mocharc.json'
];

Expand Down
19 changes: 15 additions & 4 deletions test/node-unit/cli/config.spec.js
Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand Down

0 comments on commit 22831c5

Please sign in to comment.