Skip to content

Commit

Permalink
config: default support of .jsonc extension (#3753)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sylvain STEPHANT committed Feb 21, 2019
1 parent b7cfceb commit 444ed5b
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -79,6 +79,7 @@ Session.vim
out/
.idea_modules/
atlassian-ide-plugin.xml
mocha.iml

# SourceTree
*_BACKUP_*
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",
}
5 changes: 5 additions & 0 deletions lib/cli/config.js
Expand Up @@ -24,6 +24,8 @@ exports.CONFIG_FILES = [
'.mocharc.js',
'.mocharc.yaml',
'.mocharc.yml',
'.mocharc.jsonc',
'.mocharc.json5',
'.mocharc.json'
];

Expand All @@ -35,6 +37,7 @@ const parsers = (exports.parsers = {
yaml: filepath =>
require('js-yaml').safeLoad(fs.readFileSync(filepath, 'utf8')),
js: filepath => require(filepath),
json5: filepath => require('json5').parse(fs.readFileSync(filepath, 'utf8')),
json: filepath =>
JSON.parse(
require('strip-json-comments')(fs.readFileSync(filepath, 'utf8'))
Expand All @@ -56,6 +59,8 @@ exports.loadConfig = filepath => {
config = parsers.yaml(filepath);
} else if (ext === '.js') {
config = parsers.js(filepath);
} else if (/\.json[5c]/.test(ext)) {
config = parsers.json5(filepath);
} else {
config = parsers.json(filepath);
}
Expand Down
15 changes: 15 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -411,6 +411,7 @@
"Sulabh Bista <sul4bh@gmail.com>",
"Sune Simonsen <sune@we-knowhow.dk>",
"Svetlana <39729453+Lana-Light@users.noreply.github.com>",
"Sylvain Stéphant <sstephant+github@gmail.com>",
"Tapiwa Kelvin <tapiwa@munzwa.tk>",
"Ted Yavuzkurt <hello@TedY.io>",
"Teddy Zeenny <teddyzeenny@gmail.com>",
Expand Down Expand Up @@ -500,6 +501,7 @@
"growl": "1.10.5",
"he": "1.2.0",
"js-yaml": "3.12.0",
"json5": "2.1.0",
"log-symbols": "2.2.0",
"minimatch": "3.0.4",
"mkdirp": "0.5.1",
Expand Down
2 changes: 2 additions & 0 deletions test/integration/config.spec.js
Expand Up @@ -11,8 +11,10 @@ describe('config', function() {
var configDir = path.join(__dirname, 'fixtures', 'config');
var js = loadConfig(path.join(configDir, 'mocharc.js'));
var json = loadConfig(path.join(configDir, 'mocharc.json'));
var jsonc = loadConfig(path.join(configDir, 'mocharc.jsonc'));
var yaml = loadConfig(path.join(configDir, 'mocharc.yaml'));
expect(js, 'to equal', json);
expect(json, 'to equal', yaml);
expect(yaml, 'to equal', jsonc);
});
});
7 changes: 7 additions & 0 deletions test/integration/fixtures/config/mocharc.jsonc
@@ -0,0 +1,7 @@
// Comment
{
require: ["foo", "bar"],
"bail": true,
"reporter": /* 📋 */ "dot",
"slow": +60,
}
23 changes: 23 additions & 0 deletions test/node-unit/cli/config.spec.js
Expand Up @@ -21,6 +21,7 @@ describe('cli/config', function() {
beforeEach(function() {
sandbox.stub(parsers, 'yaml').returns(config);
sandbox.stub(parsers, 'json').returns(config);
sandbox.stub(parsers, 'json5').returns(config);
sandbox.stub(parsers, 'js').returns(config);
});

Expand Down Expand Up @@ -57,6 +58,28 @@ describe('cli/config', function() {
});
});

describe('when supplied a filepath with .jsonc extension', function() {
const filepath = 'foo.jsonc';

it('should use the JSON5 parser', function() {
loadConfig('foo.jsonc');
expect(parsers.json5, 'to have calls satisfying', [
{args: [filepath], returned: config}
]).and('was called times', 1);
});
});

describe('when supplied a filepath with .json5 extension', function() {
const filepath = 'foo.json5';

it('should use the JSON5 parser', function() {
loadConfig('foo.json5');
expect(parsers.json5, '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';

Expand Down

0 comments on commit 444ed5b

Please sign in to comment.