Files
cypress/cli/test/exec/spawn_spec.js
T
Gleb Bahmutov 4520c2b6dd feat(run): handle failed tests returned by the cypress run (#180)
* feat(run): handle failed tests returned by the cypress run

* cli: rework errors thrown from cypress vs xvfb

* small tweak

* cli: test xvfb start error handling
2017-06-22 12:00:17 -04:00

91 lines
2.8 KiB
JavaScript

require('../spec_helper')
const cp = require('child_process')
const downloadUtils = require('../../lib/download/utils')
const xvfb = require('../../lib/exec/xvfb')
const spawn = require('../../lib/exec/spawn')
describe('exec spawn', function () {
beforeEach(function () {
this.sandbox.stub(process, 'exit')
this.spawnedProcess = this.sandbox.stub({
on: () => {},
unref: () => {},
})
this.sandbox.stub(cp, 'spawn').returns(this.spawnedProcess)
this.sandbox.stub(xvfb, 'start').resolves()
this.sandbox.stub(xvfb, 'stop').resolves()
this.sandbox.stub(xvfb, 'isNeeded').returns(true)
this.sandbox.stub(downloadUtils, 'getPathToExecutable').returns('/path/to/cypress')
})
context('#spawn', function () {
it('passes args + options to spawn', function () {
this.spawnedProcess.on.withArgs('exit').yieldsAsync(0)
return spawn.start('--foo', { foo: 'bar' }).then(() => {
expect(cp.spawn).to.be.calledWithMatch('/path/to/cypress', ['--foo'], { foo: 'bar' })
})
})
it('starts xvfb when needed', function () {
this.spawnedProcess.on.withArgs('exit').yieldsAsync(0)
return spawn.start('--foo').then(() => {
expect(xvfb.start).to.be.calledOnce
})
})
it('does not start xvfb when its not needed', function () {
xvfb.isNeeded.returns(false)
this.spawnedProcess.on.withArgs('exit').yieldsAsync(0)
return spawn.start('--foo').then(() => {
expect(xvfb.start).not.to.be.called
})
})
it('stops xvfb when spawn closes', function () {
this.spawnedProcess.on.withArgs('exit').yieldsAsync(0)
this.spawnedProcess.on.withArgs('close').yields()
return spawn.start('--foo').then(() => {
expect(xvfb.stop).to.be.calledOnce
})
})
it('resolves with spawned exit code in the message', function () {
this.spawnedProcess.on.withArgs('exit').yieldsAsync(10)
return spawn.start('--foo')
.then((code) => {
expect(code).to.equal(10)
})
})
it('rejects with error from spawn', function () {
const msg = 'the error message'
this.spawnedProcess.on.withArgs('error').yieldsAsync(new Error(msg))
return spawn.start('--foo')
.then(() => {
throw new Error('should have hit error handler but did not')
}, (e) => {
expect(e.message).to.include(msg)
})
})
it('unrefs if options.detached is true', function () {
this.spawnedProcess.on.withArgs('exit').yieldsAsync(0)
return spawn.start(null, { detached: true }).then(() => {
expect(this.spawnedProcess.unref).to.be.calledOnce
})
})
it('does not unref by default', function () {
this.spawnedProcess.on.withArgs('exit').yieldsAsync(0)
return spawn.start().then(() => {
expect(this.spawnedProcess.unref).not.to.be.called
})
})
})
})