feat: include warning when running with invalid config (#9088)

Co-authored-by: Jennifer Shehane <jennifer@cypress.io>
This commit is contained in:
Mo
2020-11-16 10:39:34 +02:00
committed by GitHub
parent d5eadc23ae
commit 470c69ec5a
4 changed files with 73 additions and 2 deletions
@@ -294,3 +294,10 @@ You passed: nonono
The error was: Cannot read property 'split' of undefined
`
exports['INVALID_CONFIG_OPTION'] = `
\`test\` is not a valid configuration option,\`foo\` is not a valid configuration option
https://on.cypress.io/configuration
`
+18 -2
View File
@@ -14,8 +14,8 @@ const Promise = require('bluebird')
const debug = require('debug')('cypress:server:cypress')
const argsUtils = require('./util/args')
const warning = (code) => {
return require('./errors').warning(code)
const warning = (code, args) => {
return require('./errors').warning(code, args)
}
const exit = (code = 0) => {
@@ -27,6 +27,20 @@ const exit = (code = 0) => {
return process.exit(code)
}
const showWarningForInvalidConfig = (options) => {
const invalidConfigOptions = require('lodash').keys(options.config).reduce((invalid, option) => {
if (!require('./config').getConfigKeys().find((configKey) => configKey === option)) {
invalid.push(option)
}
return invalid
}, [])
if (invalidConfigOptions.length && options.invokedFromCli) {
return warning('INVALID_CONFIG_OPTION', invalidConfigOptions)
}
}
const exit0 = () => {
return exit(0)
}
@@ -115,6 +129,8 @@ module.exports = {
try {
options = argsUtils.toObject(argv)
showWarningForInvalidConfig(options)
} catch (argumentsError) {
debug('could not parse CLI arguments: %o', argv)
+6
View File
@@ -945,6 +945,12 @@ const getMsgByType = function (type, arg1 = {}, arg2, arg3) {
https://on.cypress.io/test-retries
`
case 'INVALID_CONFIG_OPTION':
return stripIndent`\
${arg1.map((arg) => `\`${arg}\` is not a valid configuration option`)}
https://on.cypress.io/configuration
`
default:
}
}
@@ -230,6 +230,48 @@ describe('lib/cypress', () => {
})
})
context('invalid config', function () {
beforeEach(function () {
this.win = {
on: sinon.stub(),
webContents: {
on: sinon.stub(),
},
}
sinon.stub(electron.app, 'on').withArgs('ready').yieldsAsync()
sinon.stub(Windows, 'open').resolves(this.win)
})
it('shows warning if config is not valid', function () {
return cypress.start(['--config=test=false', '--cwd=/foo/bar'])
.then(() => {
expect(errors.warning).to.be.calledWith('INVALID_CONFIG_OPTION')
expect(console.log).to.be.calledWithMatch('`test` is not a valid configuration option')
expect(console.log).to.be.calledWithMatch('https://on.cypress.io/configuration')
})
})
it('shows warning when multiple config are not valid', function () {
return cypress.start(['--config=test=false,foo=bar', '--cwd=/foo/bar'])
.then(() => {
expect(errors.warning).to.be.calledWith('INVALID_CONFIG_OPTION')
expect(console.log).to.be.calledWithMatch('`test` is not a valid configuration option')
expect(console.log).to.be.calledWithMatch('`foo` is not a valid configuration option')
expect(console.log).to.be.calledWithMatch('https://on.cypress.io/configuration')
snapshotConsoleLogs('INVALID_CONFIG_OPTION')
})
})
it('does not show warning if config is valid', function () {
return cypress.start(['--config=trashAssetsBeforeRuns=false'])
.then(() => {
expect(errors.warning).to.not.be.calledWith('INVALID_CONFIG_OPTION')
})
})
})
context('--get-key', () => {
it('writes out key and exits on success', function () {
return Promise.all([