diff --git a/packages/server/__snapshots__/args_spec.js b/packages/server/__snapshots__/args_spec.js index ab3180f1aac6..99434367f504 100644 --- a/packages/server/__snapshots__/args_spec.js +++ b/packages/server/__snapshots__/args_spec.js @@ -21,3 +21,11 @@ You passed: xyz The error was: Cannot read property 'split' of undefined ` + +exports['invalid spec error'] = ` +Cypress encountered an error while parsing the argument spec + +You passed: [object Object] + +The error was: spec must be a string or comma-separated list +` diff --git a/packages/server/lib/util/args.js b/packages/server/lib/util/args.js index 1dd9d19ee3dd..45e2637ac4eb 100644 --- a/packages/server/lib/util/args.js +++ b/packages/server/lib/util/args.js @@ -247,22 +247,29 @@ module.exports = { } if (spec) { - const resolvePath = (p) => { - return path.resolve(options.cwd, p) - } + try { + const resolvePath = (p) => { + return path.resolve(options.cwd, p) + } - // https://github.com/cypress-io/cypress/issues/8818 - // Sometimes spec is parsed to array. Because of that, we need check. - if (typeof spec === 'string') { - // clean up single quotes wrapping the spec for Windows users - // https://github.com/cypress-io/cypress/issues/2298 - if (spec[0] === '\'' && spec[spec.length - 1] === '\'') { - spec = spec.substring(1, spec.length - 1) + // https://github.com/cypress-io/cypress/issues/8818 + // Sometimes spec is parsed to array. Because of that, we need check. + if (typeof spec === 'string') { + // clean up single quotes wrapping the spec for Windows users + // https://github.com/cypress-io/cypress/issues/2298 + if (spec[0] === '\'' && spec[spec.length - 1] === '\'') { + spec = spec.substring(1, spec.length - 1) + } + + options.spec = strToArray(spec).map(resolvePath) + } else { + options.spec = spec.map(resolvePath) } + } catch (err) { + debug('could not pass config spec value %s', spec) + debug('error %o', err) - options.spec = strToArray(spec).map(resolvePath) - } else { - options.spec = spec.map(resolvePath) + return errors.throw('COULD_NOT_PARSE_ARGUMENTS', 'spec', spec, 'spec must be a string or comma-separated list') } } diff --git a/packages/server/test/unit/args_spec.js b/packages/server/test/unit/args_spec.js index 74fee4211971..6b92bdddefb1 100644 --- a/packages/server/test/unit/args_spec.js +++ b/packages/server/test/unit/args_spec.js @@ -152,6 +152,18 @@ describe('lib/util/args', () => { expect(options.spec[0]).to.eq(`${cwd}/cypress/integration/foo_spec.js`) }) + + it('throws if argument cannot be parsed', function () { + expect(() => { + return this.setup('--run-project', 'foo', '--spec', {}) + }).to.throw + + try { + return this.setup('--run-project', 'foo', '--spec', {}) + } catch (err) { + return snapshot('invalid spec error', stripAnsi(err.message)) + } + }) }) context('--tag', () => {