require('../spec_helper') const cli = require(`${lib}/cli`) const util = require(`${lib}/util`) const logger = require(`${lib}/logger`) const run = require(`${lib}/exec/run`) const open = require(`${lib}/exec/open`) const info = require(`${lib}/tasks/info`) const verify = require(`${lib}/tasks/verify`) const install = require(`${lib}/tasks/install`) const snapshot = require('snap-shot-it') const execa = require('execa-wrap') const path = require('path') describe('cli', function () { beforeEach(function () { logger.reset() this.sandbox.stub(process, 'exit') this.sandbox.stub(util, 'exit') this.sandbox.stub(util, 'logErrorExit1') this.exec = (args) => cli.init(`node test ${args}`.split(' ')) }) context('help command', () => { it('shows help', () => execa('bin/cypress', ['help']).then(snapshot) ) it('shows help for -h', () => execa('bin/cypress', ['-h']).then(snapshot) ) it('shows help for --help', () => execa('bin/cypress', ['--help']).then(snapshot) ) }) context('unknown command', () => { it('shows usage and exits', () => execa('bin/cypress', ['foo']).then(snapshot) ) }) context('cypress version', function () { it('reports package version', function (done) { this.sandbox.stub(util, 'pkgVersion').returns('1.2.3') this.sandbox.stub(info, 'getInstalledVersion').resolves('X.Y.Z') this.exec('version') process.exit.callsFake(() => { snapshot('cli version and binary version', logger.print()) done() }) }) it('reports package and binary message', function (done) { this.sandbox.stub(util, 'pkgVersion').returns('1.2.3') this.sandbox.stub(info, 'getInstalledVersion').resolves('X.Y.Z') this.exec('version') process.exit.callsFake(() => { snapshot('cli version and binary version', logger.print()) done() }) }) it('handles non-existent binary version', function (done) { this.sandbox.stub(util, 'pkgVersion').returns('1.2.3') this.sandbox.stub(info, 'getInstalledVersion').resolves(null) this.exec('version') process.exit.callsFake(() => { snapshot('cli version no binary version', logger.print()) done() }) }) it('handles non-existent binary --version', function (done) { this.sandbox.stub(util, 'pkgVersion').returns('1.2.3') this.sandbox.stub(info, 'getInstalledVersion').resolves(null) this.exec('--version') process.exit.callsFake(() => { snapshot('cli --version no binary version', logger.print()) done() }) }) it('handles non-existent binary -v', function (done) { this.sandbox.stub(util, 'pkgVersion').returns('1.2.3') this.sandbox.stub(info, 'getInstalledVersion').resolves(null) this.exec('-v') process.exit.callsFake(() => { snapshot('cli -v no binary version', logger.print()) done() }) }) }) context('cypress run', function () { beforeEach(function () { this.sandbox.stub(run, 'start').resolves(0) }) it('calls run.start with options + exits with code', function (done) { run.start.resolves(10) this.exec('run') util.exit.callsFake((code) => { expect(code).to.eq(10) done() }) }) it('run.start with options + catches errors', function (done) { const err = new Error('foo') run.start.rejects(err) this.exec('run') util.logErrorExit1.callsFake((e) => { expect(e).to.eq(err) done() }) }) it('calls run without group flag', function () { this.exec('run') expect(run.start).to.be.calledWith({}) }) it('calls run with group flag', function () { this.exec('run --group') expect(run.start).to.be.calledWith({ group: true }) }) it('calls run with groupId', function () { this.exec('run --group-id foo') expect(run.start).to.be.calledWith({ groupId: 'foo' }) }) it('calls run with port', function () { this.exec('run --port 7878') expect(run.start).to.be.calledWith({ port: '7878' }) }) it('calls run with spec', function () { this.exec('run --spec cypress/integration/foo_spec.js') expect(run.start).to.be.calledWith({ spec: 'cypress/integration/foo_spec.js' }) }) it('calls run with port with -p arg', function () { this.exec('run -p 8989') expect(run.start).to.be.calledWith({ port: '8989' }) }) it('calls run with env variables', function () { this.exec('run --env foo=bar,host=http://localhost:8888') expect(run.start).to.be.calledWith({ env: 'foo=bar,host=http://localhost:8888' }) }) it('calls run with config', function () { this.exec('run --config watchForFileChanges=false,baseUrl=localhost') expect(run.start).to.be.calledWith({ config: 'watchForFileChanges=false,baseUrl=localhost' }) }) it('calls run with key', function () { this.exec('run --key asdf') expect(run.start).to.be.calledWith({ key: 'asdf' }) }) it('calls run with --record', function () { this.exec('run --record') expect(run.start).to.be.calledWith({ record: true }) }) it('calls run with --record false', function () { this.exec('run --record false') expect(run.start).to.be.calledWith({ record: false }) }) it('calls run with relative --project folder', function () { this.sandbox.stub(path, 'resolve') .withArgs('foo/bar').returns('/mock/absolute/foo/bar') this.exec('run --project foo/bar') expect(run.start).to.be.calledWith({ project: '/mock/absolute/foo/bar' }) }) it('calls run with absolute --project folder', function () { this.exec('run --project /tmp/foo/bar') expect(run.start).to.be.calledWith({ project: '/tmp/foo/bar' }) }) it('calls run with heded', function () { this.exec('run --headed') expect(run.start).to.be.calledWith({ headed: true }) }) }) context('cypress open', function () { beforeEach(function () { this.sandbox.stub(open, 'start').resolves(0) }) it('calls open.start with relative --project folder', function () { this.sandbox.stub(path, 'resolve') .withArgs('foo/bar').returns('/mock/absolute/foo/bar') this.exec('open --project foo/bar') expect(open.start).to.be.calledWith({ project: '/mock/absolute/foo/bar' }) }) it('calls open.start with absolute --project folder', function () { this.exec('open --project /tmp/foo/bar') expect(open.start).to.be.calledWith({ project: '/tmp/foo/bar' }) }) it('calls open.start with options', function () { // this.sandbox.stub(open, 'start').resolves() this.exec('open --port 7878') expect(open.start).to.be.calledWith({ port: '7878' }) }) it('calls open.start + catches errors', function (done) { const err = new Error('foo') open.start.rejects(err) this.exec('open --port 7878') util.logErrorExit1.callsFake((e) => { expect(e).to.eq(err) done() }) }) }) it('install calls install.start with force: true', function () { this.sandbox.stub(install, 'start').resolves() this.exec('install') expect(install.start).to.be.calledWith({ force: true }) }) it('install calls install.start + catches errors', function (done) { const err = new Error('foo') this.sandbox.stub(install, 'start').rejects(err) this.exec('install') util.logErrorExit1.callsFake((e) => { expect(e).to.eq(err) done() }) }) it('verify calls verify.start with force: true', function () { this.sandbox.stub(verify, 'start').resolves() this.exec('verify') expect(verify.start).to.be.calledWith({ force: true, welcomeMessage: false }) }) it('verify calls verify.start + catches errors', function (done) { const err = new Error('foo') this.sandbox.stub(verify, 'start').rejects(err) this.exec('verify') util.logErrorExit1.callsFake((e) => { expect(e).to.eq(err) done() }) }) })