From 444ed5b49ed8aebf1ca3c284fa2df503d9000521 Mon Sep 17 00:00:00 2001 From: Sylvain STEPHANT Date: Thu, 21 Feb 2019 14:23:40 +0100 Subject: [PATCH] config: default support of .jsonc extension (#3753) --- .gitignore | 1 + example/config/.mocharc.jsonc | 14 +++++++++++ lib/cli/config.js | 5 ++++ package-lock.json | 15 ++++++++++++ package.json | 2 ++ test/integration/config.spec.js | 2 ++ .../integration/fixtures/config/mocharc.jsonc | 7 ++++++ test/node-unit/cli/config.spec.js | 23 +++++++++++++++++++ 8 files changed, 69 insertions(+) create mode 100644 example/config/.mocharc.jsonc create mode 100644 test/integration/fixtures/config/mocharc.jsonc diff --git a/.gitignore b/.gitignore index 02634697bf..3ab9de9814 100644 --- a/.gitignore +++ b/.gitignore @@ -79,6 +79,7 @@ Session.vim out/ .idea_modules/ atlassian-ide-plugin.xml +mocha.iml # SourceTree *_BACKUP_* diff --git a/example/config/.mocharc.jsonc b/example/config/.mocharc.jsonc new file mode 100644 index 0000000000..0a4b3ab621 --- /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..46d194ecca 100644 --- a/lib/cli/config.js +++ b/lib/cli/config.js @@ -24,6 +24,8 @@ exports.CONFIG_FILES = [ '.mocharc.js', '.mocharc.yaml', '.mocharc.yml', + '.mocharc.jsonc', + '.mocharc.json5', '.mocharc.json' ]; @@ -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')) @@ -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); } diff --git a/package-lock.json b/package-lock.json index 38ba70edee..5e6696a46b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10360,6 +10360,21 @@ "integrity": "sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus=", "dev": true }, + "json5": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/json5/-/json5-2.1.0.tgz", + "integrity": "sha512-8Mh9h6xViijj36g7Dxi+Y4S6hNGV96vcJZr/SrlHh1LR/pEn/8j/+qIBbs44YKl69Lrfctp4QD+AdWLTMqEZAQ==", + "requires": { + "minimist": "^1.2.0" + }, + "dependencies": { + "minimist": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", + "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=" + } + } + }, "jsonfile": { "version": "2.4.0", "resolved": "http://registry.npmjs.org/jsonfile/-/jsonfile-2.4.0.tgz", diff --git a/package.json b/package.json index 3e2f61131f..542cfb8f47 100644 --- a/package.json +++ b/package.json @@ -411,6 +411,7 @@ "Sulabh Bista ", "Sune Simonsen ", "Svetlana <39729453+Lana-Light@users.noreply.github.com>", + "Sylvain Stéphant ", "Tapiwa Kelvin ", "Ted Yavuzkurt ", "Teddy Zeenny ", @@ -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", diff --git a/test/integration/config.spec.js b/test/integration/config.spec.js index 76a4a718d4..636bcec363 100644 --- a/test/integration/config.spec.js +++ b/test/integration/config.spec.js @@ -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); }); }); diff --git a/test/integration/fixtures/config/mocharc.jsonc b/test/integration/fixtures/config/mocharc.jsonc new file mode 100644 index 0000000000..60d8316ed3 --- /dev/null +++ b/test/integration/fixtures/config/mocharc.jsonc @@ -0,0 +1,7 @@ +// Comment +{ + require: ["foo", "bar"], + "bail": true, + "reporter": /* 📋 */ "dot", + "slow": +60, +} diff --git a/test/node-unit/cli/config.spec.js b/test/node-unit/cli/config.spec.js index 78f1dd5d00..691cb4392f 100644 --- a/test/node-unit/cli/config.spec.js +++ b/test/node-unit/cli/config.spec.js @@ -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); }); @@ -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';