-
-
Notifications
You must be signed in to change notification settings - Fork 535
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(tests): increase test coverage on util modules
- Loading branch information
1 parent
6d15c62
commit 6c63aaf
Showing
7 changed files
with
281 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { expect } from 'chai'; | ||
import proxyquire from 'proxyquire'; | ||
import sinon from 'sinon'; | ||
|
||
describe('GitHub', () => { | ||
let install; | ||
let spawnSpy; | ||
let hasYarnSpy; | ||
let exitHandler; | ||
|
||
beforeEach(() => { | ||
const outOn = (s, fn) => fn('a'); | ||
const errOn = outOn; | ||
spawnSpy = sinon.stub(); | ||
spawnSpy.returns({ | ||
stdout: { on: outOn }, | ||
stderr: { on: errOn }, | ||
on: (s, fn) => { exitHandler = fn; }, | ||
}); | ||
hasYarnSpy = sinon.stub(); | ||
install = proxyquire.noCallThru().load('../../src/util/install-dependencies', { | ||
'yarn-or-npm': { | ||
spawn: spawnSpy, | ||
hasYarn: hasYarnSpy, | ||
}, | ||
}).default; | ||
}); | ||
|
||
it('should immediately resolve if no deps are provided', async () => { | ||
await install('mydir', []); | ||
expect(spawnSpy.callCount).to.equal(0); | ||
}); | ||
|
||
it('should reject if reject the promise if exit code is not 0', (done) => { | ||
const p = install('void', ['electron']); | ||
p.then(() => done(new Error('expected install to be rejected'))) | ||
.catch(() => done()); | ||
exitHandler(1); | ||
}); | ||
|
||
it('should resolve if reject the promise if exit code is 0', (done) => { | ||
const p = install('void', ['electron']); | ||
p.then(() => done()) | ||
.catch(() => done(new Error('expected install to be resolved'))); | ||
exitHandler(0); | ||
}); | ||
|
||
describe('with yarn', () => { | ||
beforeEach(() => { | ||
hasYarnSpy.returns(true); | ||
}); | ||
|
||
it('should install prod deps', () => { | ||
install('mydir', ['react']); | ||
expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['add', 'react']); | ||
}); | ||
|
||
it('should install dev deps', () => { | ||
install('mydir', ['eslint'], true); | ||
expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['add', 'eslint', '--dev']); | ||
}); | ||
|
||
it('should install exact deps', () => { | ||
install('mydir', ['react-dom'], false, true); | ||
expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['add', 'react-dom', '--exact']); | ||
}); | ||
|
||
it('should install exact dev deps', () => { | ||
install('mydir', ['mocha'], true, true); | ||
expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['add', 'mocha', '--dev', '--exact']); | ||
}); | ||
}); | ||
|
||
describe('with npm', () => { | ||
beforeEach(() => { | ||
hasYarnSpy.returns(false); | ||
}); | ||
|
||
it('should install prod deps', () => { | ||
install('mydir', ['react']); | ||
expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['install', 'react', '--save']); | ||
}); | ||
|
||
it('should install dev deps', () => { | ||
install('mydir', ['eslint'], true); | ||
expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['install', 'eslint', '--save-dev']); | ||
}); | ||
|
||
it('should install exact deps', () => { | ||
install('mydir', ['react-dom'], false, true); | ||
expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['install', 'react-dom', '--save-exact', '--save']); | ||
}); | ||
|
||
it('should install exact dev deps', () => { | ||
install('mydir', ['mocha'], true, true); | ||
expect(spawnSpy.firstCall.args[0]).to.be.deep.equal(['install', 'mocha', '--save-exact', '--save-dev']); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { expect } from 'chai'; | ||
import proxyquire from 'proxyquire'; | ||
import sinon from 'sinon'; | ||
|
||
describe('asyncOra', () => { | ||
let asyncOra; | ||
let MockOra; | ||
let currentOra; | ||
|
||
beforeEach(() => { | ||
currentOra = undefined; | ||
MockOra = (text) => { | ||
currentOra = { | ||
start() { | ||
this.started = true; | ||
return currentOra; | ||
}, | ||
succeed() { | ||
this.succeeded = true; | ||
return currentOra; | ||
}, | ||
fail() { | ||
this.failed = true; | ||
return currentOra; | ||
}, | ||
get text() { | ||
return currentOra._text; | ||
}, | ||
set text(newText) { | ||
currentOra._text = newText; | ||
}, | ||
}; | ||
currentOra.succeeded = false; | ||
currentOra.failed = false; | ||
currentOra._text = text; | ||
return currentOra; | ||
}; | ||
asyncOra = proxyquire.noCallThru().load('../../src/util/ora-handler', { | ||
ora: { ora: MockOra }, | ||
}).default; | ||
}); | ||
|
||
it('should create an ora with an inital value', () => { | ||
asyncOra('say this first', async () => {}); | ||
expect(currentOra).to.not.equal(undefined); | ||
expect(currentOra.text).to.equal('say this first'); | ||
}); | ||
|
||
it('should not create an ora when in non-interactive mode', () => { | ||
asyncOra.interactive = false; | ||
asyncOra('say this again', async () => {}); | ||
expect(currentOra).to.equal(undefined); | ||
}); | ||
|
||
it('should call the provided async function', async () => { | ||
const spy = sinon.spy(); | ||
await asyncOra('random text', async () => { | ||
spy(); | ||
}); | ||
expect(spy.callCount).to.equal(1); | ||
}); | ||
|
||
it('should succeed the ora if the async fn passes', async () => { | ||
await asyncOra('random text', async () => { | ||
if (2 + 2 === 5) console.error('Big brother is at it again'); | ||
}); | ||
expect(currentOra.succeeded).to.equal(true); | ||
expect(currentOra.failed).to.equal(false); | ||
}); | ||
|
||
it('should fail the ora if the async fn throws', async () => { | ||
await asyncOra('this is gonna end badly', async () => { | ||
throw { message: 'Not an error', stack: 'No Stack - Not an error' }; // eslint-disable-line | ||
}, () => {}); | ||
expect(currentOra.succeeded).to.equal(false); | ||
expect(currentOra.failed).to.equal(true); | ||
}); | ||
|
||
it('should exit the process with status 1 if the async fn throws', async () => { | ||
const processExitSpy = sinon.spy(); | ||
await asyncOra('this is dodge', async () => { | ||
throw 'woops'; // eslint-disable-line | ||
}, processExitSpy); | ||
expect(processExitSpy.callCount).to.equal(1); | ||
expect(processExitSpy.firstCall.args).to.deep.equal([1]); | ||
}); | ||
|
||
it('should exit the process with status 1 if the async fn throws a number', async () => { | ||
const processExitSpy = sinon.spy(); | ||
await asyncOra('this is dodge', async () => { | ||
throw 42; // eslint-disable-line | ||
}, processExitSpy); | ||
expect(processExitSpy.callCount).to.equal(1); | ||
expect(processExitSpy.firstCall.args).to.deep.equal([1]); | ||
}); | ||
|
||
it('should just reject the promise in non-interactive mode if the fn throws', (done) => { | ||
asyncOra.interactive = false; | ||
asyncOra('doo-wop', async () => { | ||
throw new Error('uh oh'); | ||
}).then(() => done(new Error('expected asyncOra to be rejected'))) | ||
.catch(() => done()); | ||
}); | ||
|
||
it('should provide a fully functioning mock ora in non-interactive mode', async () => { | ||
asyncOra.interactive = false; | ||
await asyncOra('ora-magic', async (spinner) => { | ||
expect(spinner).to.have.property('start'); | ||
expect(spinner).to.have.property('stop'); | ||
expect(spinner).to.have.property('succeed'); | ||
expect(spinner).to.have.property('fail'); | ||
expect(spinner.start().stop().fail().succeed()).to.equal(spinner); | ||
}); | ||
}); | ||
}); |