From 72273cb329504145e0e22438766efa31829ae1c6 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Mon, 24 Apr 2023 20:44:27 +0200 Subject: [PATCH 1/3] test(cli): add tests borrowed from #4407 --- .../config/mocharc-extended/base.json | 6 +++++ .../config/mocharc-extended/extends.json | 3 +++ .../config/mocharc-extended/modifiers.json | 5 ++++ test/integration/options.spec.js | 23 +++++++++++++++++++ 4 files changed, 37 insertions(+) create mode 100644 test/integration/fixtures/config/mocharc-extended/base.json create mode 100644 test/integration/fixtures/config/mocharc-extended/extends.json create mode 100644 test/integration/fixtures/config/mocharc-extended/modifiers.json create mode 100644 test/integration/options.spec.js diff --git a/test/integration/fixtures/config/mocharc-extended/base.json b/test/integration/fixtures/config/mocharc-extended/base.json new file mode 100644 index 0000000000..89c92825f9 --- /dev/null +++ b/test/integration/fixtures/config/mocharc-extended/base.json @@ -0,0 +1,6 @@ +{ + "require": ["foo", "bar"], + "bail": true, + "reporter": "dot", + "slow": 60 +} diff --git a/test/integration/fixtures/config/mocharc-extended/extends.json b/test/integration/fixtures/config/mocharc-extended/extends.json new file mode 100644 index 0000000000..715639efd7 --- /dev/null +++ b/test/integration/fixtures/config/mocharc-extended/extends.json @@ -0,0 +1,3 @@ +{ + "extends": "./modifiers.json" +} diff --git a/test/integration/fixtures/config/mocharc-extended/modifiers.json b/test/integration/fixtures/config/mocharc-extended/modifiers.json new file mode 100644 index 0000000000..8e6dcb1d3c --- /dev/null +++ b/test/integration/fixtures/config/mocharc-extended/modifiers.json @@ -0,0 +1,5 @@ +{ + "extends": "./base.json", + "reporter": "html", + "slow": 30 +} diff --git a/test/integration/options.spec.js b/test/integration/options.spec.js new file mode 100644 index 0000000000..b7ad906a7c --- /dev/null +++ b/test/integration/options.spec.js @@ -0,0 +1,23 @@ +'use strict'; + +var path = require('path'); +var loadOptions = require('../../lib/cli/options').loadOptions; + +describe('options', function () { + it('Should support extended options', function () { + var configDir = path.join( + __dirname, + 'fixtures', + 'config', + 'mocharc-extended' + ); + var extended = loadOptions([ + '--config', + path.join(configDir, 'extends.json') + ]); + expect(extended.require, 'to equal', ['foo', 'bar']); + expect(extended.bail, 'to equal', true); + expect(extended.reporter, 'to equal', 'html'); + expect(extended.slow, 'to equal', 30); + }); +}); From e76a0df7235fe0b330a2d7a28feaeb03e4c6d92b Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Mon, 24 Apr 2023 20:45:36 +0200 Subject: [PATCH 2/3] fix(cli): handle `extends` setting properly --- lib/cli/config.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/cli/config.js b/lib/cli/config.js index ac719833cf..dcd0defd35 100644 --- a/lib/cli/config.js +++ b/lib/cli/config.js @@ -11,6 +11,7 @@ const fs = require('fs'); const path = require('path'); const debug = require('debug')('mocha:cli:config'); const findUp = require('find-up'); +const yargs = require('yargs/yargs'); const {createUnparsableFileError} = require('../errors'); const utils = require('../utils'); @@ -76,6 +77,10 @@ exports.loadConfig = filepath => { } else { config = parsers.json(filepath); } + + config = yargs(undefined, path.dirname(filepath)) + .parserConfiguration(require('./options').YARGS_PARSER_CONFIG) + .config(config); } catch (err) { throw createUnparsableFileError( `Unable to read/parse ${filepath}: ${err}`, From f131e9a6d05c425459d717c6ffc009bb5cf15770 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Mon, 24 Apr 2023 21:15:08 +0200 Subject: [PATCH 3/3] test(cli): test the functionality for rc files --- .../config/mocharc-extended/.mocharc.json | 3 ++ test/integration/options.spec.js | 35 ++++++++++++++----- 2 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 test/integration/fixtures/config/mocharc-extended/.mocharc.json diff --git a/test/integration/fixtures/config/mocharc-extended/.mocharc.json b/test/integration/fixtures/config/mocharc-extended/.mocharc.json new file mode 100644 index 0000000000..98aae677ca --- /dev/null +++ b/test/integration/fixtures/config/mocharc-extended/.mocharc.json @@ -0,0 +1,3 @@ +{ + "extends": "./extends.json" +} diff --git a/test/integration/options.spec.js b/test/integration/options.spec.js index b7ad906a7c..77841ccdd9 100644 --- a/test/integration/options.spec.js +++ b/test/integration/options.spec.js @@ -4,20 +4,39 @@ var path = require('path'); var loadOptions = require('../../lib/cli/options').loadOptions; describe('options', function () { - it('Should support extended options', function () { - var configDir = path.join( - __dirname, - 'fixtures', - 'config', - 'mocharc-extended' - ); + var workingDirectory; + const workspaceDir = path.join( + __dirname, + 'fixtures', + 'config', + 'mocharc-extended' + ); + + beforeEach(function () { + workingDirectory = process.cwd(); + process.chdir(workspaceDir); + }); + + afterEach(function () { + process.chdir(workingDirectory); + }); + + it('Should support extended options using --config parameter', function () { var extended = loadOptions([ '--config', - path.join(configDir, 'extends.json') + path.join(workspaceDir, 'extends.json') ]); expect(extended.require, 'to equal', ['foo', 'bar']); expect(extended.bail, 'to equal', true); expect(extended.reporter, 'to equal', 'html'); expect(extended.slow, 'to equal', 30); }); + + it('Should support extended options using rc file', function () { + var extended = loadOptions([]); + expect(extended.require, 'to equal', ['foo', 'bar']); + expect(extended.bail, 'to equal', true); + expect(extended.reporter, 'to equal', 'html'); + expect(extended.slow, 'to equal', 30); + }); });