From 48ba0ede630f90d86cb0d4414e8beb88758c74c6 Mon Sep 17 00:00:00 2001 From: Sylvain STEPHANT Date: Thu, 21 Feb 2019 14:23:40 +0100 Subject: [PATCH 1/5] 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 | 1 + test/integration/config.spec.js | 2 ++ .../integration/fixtures/config/mocharc.jsonc | 7 ++++++ test/node-unit/cli/config.spec.js | 23 +++++++++++++++++++ 8 files changed, 68 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 700ad24086..368074be9f 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 0f430d8cc5..499e357700 100644 --- a/package.json +++ b/package.json @@ -502,6 +502,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'; From cfa94e70107786d4fa46166d66d7b740ed55ece9 Mon Sep 17 00:00:00 2001 From: Sylvain STEPHANT Date: Thu, 21 Feb 2019 16:57:49 +0100 Subject: [PATCH 2/5] Documentation update for #3753 --- docs/index.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/index.md b/docs/index.md index d746a1bdb2..517c3a95b9 100644 --- a/docs/index.md +++ b/docs/index.md @@ -1628,6 +1628,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. +- **[JSON5](https://json5.org/)**: Create a `.mocharc.jsonc` (or `.mocharc.json5`) in your project's root directory. - **`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 +1650,8 @@ 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.json5` 1. `.mocharc.json` ### Merging From e847424b961c664ba6e71780f3839691ff56f657 Mon Sep 17 00:00:00 2001 From: Sylvain STEPHANT Date: Fri, 22 Feb 2019 16:46:16 +0100 Subject: [PATCH 3/5] Drop JSON5 support --- .gitignore | 1 - docs/index.md | 4 +--- lib/cli/config.js | 4 ---- package-lock.json | 15 --------------- package.json | 1 - test/integration/config.spec.js | 2 -- test/integration/fixtures/config/mocharc.jsonc | 7 ------- test/node-unit/cli/config.spec.js | 16 ++-------------- 8 files changed, 3 insertions(+), 47 deletions(-) delete mode 100644 test/integration/fixtures/config/mocharc.jsonc diff --git a/.gitignore b/.gitignore index 3ab9de9814..02634697bf 100644 --- a/.gitignore +++ b/.gitignore @@ -79,7 +79,6 @@ Session.vim out/ .idea_modules/ atlassian-ide-plugin.xml -mocha.iml # SourceTree *_BACKUP_* diff --git a/docs/index.md b/docs/index.md index 517c3a95b9..34dd8973a0 100644 --- a/docs/index.md +++ b/docs/index.md @@ -1627,8 +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. -- **[JSON5](https://json5.org/)**: Create a `.mocharc.jsonc` (or `.mocharc.json5`) in your project's root directory. +- **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. @@ -1651,7 +1650,6 @@ If no custom path was given, and if there are multiple configuration files in th 1. `.mocharc.yaml` 1. `.mocharc.yml` 1. `.mocharc.jsonc` -1. `.mocharc.json5` 1. `.mocharc.json` ### Merging diff --git a/lib/cli/config.js b/lib/cli/config.js index 46d194ecca..dd8f987ca1 100644 --- a/lib/cli/config.js +++ b/lib/cli/config.js @@ -25,7 +25,6 @@ exports.CONFIG_FILES = [ '.mocharc.yaml', '.mocharc.yml', '.mocharc.jsonc', - '.mocharc.json5', '.mocharc.json' ]; @@ -37,7 +36,6 @@ 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')) @@ -59,8 +57,6 @@ 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 368074be9f..700ad24086 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10360,21 +10360,6 @@ "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 499e357700..0f430d8cc5 100644 --- a/package.json +++ b/package.json @@ -502,7 +502,6 @@ "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 636bcec363..76a4a718d4 100644 --- a/test/integration/config.spec.js +++ b/test/integration/config.spec.js @@ -11,10 +11,8 @@ 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 deleted file mode 100644 index 60d8316ed3..0000000000 --- a/test/integration/fixtures/config/mocharc.jsonc +++ /dev/null @@ -1,7 +0,0 @@ -// 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 691cb4392f..295cd845dc 100644 --- a/test/node-unit/cli/config.spec.js +++ b/test/node-unit/cli/config.spec.js @@ -21,7 +21,6 @@ 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); }); @@ -61,20 +60,9 @@ describe('cli/config', function() { describe('when supplied a filepath with .jsonc extension', function() { const filepath = 'foo.jsonc'; - it('should use the JSON5 parser', function() { + it('should use the JSON 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', [ + expect(parsers.json, 'to have calls satisfying', [ {args: [filepath], returned: config} ]).and('was called times', 1); }); From cdd35388a79dd966cf62c0c94b61f57b21a9f7f7 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Sun, 24 Feb 2019 22:34:56 +0100 Subject: [PATCH 4/5] Fixes JSONC example (no more JSON5) --- example/config/.mocharc.jsonc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/example/config/.mocharc.jsonc b/example/config/.mocharc.jsonc index 0a4b3ab621..d86055f870 100644 --- a/example/config/.mocharc.jsonc +++ b/example/config/.mocharc.jsonc @@ -3,12 +3,12 @@ // This same configuration could be provided in the `mocha` property of your // project's `package.json`. { - diff: true, + "diff": true, "extension": ["js"], "opts": "./test/mocha.opts", "package": /* 📦 */ "./package.json", "reporter": /* 📋 */ "spec", - "slow": 75., - "timeout": /* ⏱ */ +2000, - "ui": "bdd", + "slow": 75, + "timeout": 2000, + "ui": "bdd" } From fdf7375b9e478246c4a4fcbb3eddadc1b491dbb6 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Sun, 24 Feb 2019 22:43:06 +0100 Subject: [PATCH 5/5] Adds emphasize on the config file extensions Uses double quotes around file extensions --- test/node-unit/cli/config.spec.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/node-unit/cli/config.spec.js b/test/node-unit/cli/config.spec.js index 295cd845dc..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,7 @@ describe('cli/config', function() { }); }); - describe('when supplied a filepath with .jsonc extension', function() { + describe('when supplied a filepath with ".jsonc" extension', function() { const filepath = 'foo.jsonc'; it('should use the JSON parser', function() { @@ -68,7 +68,7 @@ describe('cli/config', function() { }); }); - describe('when supplied a filepath with .json extension', function() { + describe('when supplied a filepath with ".json" extension', function() { const filepath = 'foo.json'; it('should use the JSON parser', function() {