Skip to content

Commit

Permalink
Fix cli unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
javierbrea committed Sep 2, 2018
1 parent 514da9a commit 2d28204
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 21 deletions.
6 changes: 3 additions & 3 deletions cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ const domapic = require('domapic-base')
const options = require('../lib/options')
const user = require('./commands/user')

domapic.cli({
module.exports = domapic.cli({
script: path.resolve(__dirname, '..', 'server.js'),
customConfig: options,
customCommands: {
user
}
}).catch((err) => {
}).catch(err => {
process.exitCode = 1
if (!err.isDomapic) {
console.log(err)
console.error(err)
}
})
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"coveralls": "3.0.2",
"narval": "2.2.1",
"request": "2.87.0",
"request-promise": "4.2.2"
"request-promise": "4.2.2",
"mockery": "2.1.0"
},
"author": {
"name": "Javier Brea",
Expand Down
2 changes: 1 addition & 1 deletion test/functional/specs/db-config.specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test.describe('server', function () {
test.it('should have printed a log with the mongodb uri', () => {
return testUtils.logs.combined('controller')
.then((log) => {
return test.expect(log).to.contain(`Connecting with database "${DB_URI}"`)
return test.expect(log).to.contain(`Connected to database "${DB_URI}"`)
})
})

Expand Down
2 changes: 1 addition & 1 deletion test/unit/Base.mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const Mock = function () {

const stubs = {
service: serviceStubs,
cli: sandbox.stub(domapicBase, 'cli'),
cli: sandbox.stub(domapicBase, 'cli').usingPromise().resolves(),
Service: sandbox.stub(domapicBase, 'Service').usingPromise().resolves(serviceStubs)
}

Expand Down
99 changes: 85 additions & 14 deletions test/unit/cli/index.specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,101 @@
const path = require('path')

const test = require('narval')
const mockery = require('mockery')

const mocks = require('../mocks')

const options = require('../../../lib/options')

test.describe('cli index', () => {
test.describe('cli index', function () {
this.timeout(10000)
let sandbox
let baseMock
let consoleStub
let previousExitCode
let mocks
let options
let userCommand

test.before(() => {
baseMock = new mocks.Base()
require('../../../cli/index')
mockery.enable({
useCleanCache: true,
warnOnReplace: false,
warnOnUnregistered: false
})
})

test.after(() => {
baseMock.restore()
mockery.deregisterAll()
mockery.disable()
})

test.it('should called to create a cli from domapic base', () => {
test.expect(baseMock.stubs.cli).to.have.been.called()
test.describe('when cli execution finish ok', () => {
test.beforeEach(() => {
mockery.resetCache()
mocks = require('../mocks')
options = require('../../../lib/options')
userCommand = require('../../../cli/commands/user')
previousExitCode = process.exitCode
sandbox = test.sinon.createSandbox()
baseMock = new mocks.Base()
return require('../../../cli/index')
})

test.afterEach(() => {
process.exitCode = previousExitCode
baseMock.restore()
sandbox.restore()
})

test.it('should called to create a cli from domapic base', () => {
test.expect(baseMock.stubs.cli).to.have.been.called()
})

test.it('should have passed the server script path when created the cli', () => {
test.expect(baseMock.stubs.cli).to.have.been.calledWith({
script: path.resolve(__dirname, '..', '..', '..', 'server.js'),
customConfig: options,
customCommands: {
user: userCommand
}
})
})
})

test.it('should have passed the server script path when created the cli', () => {
test.expect(baseMock.stubs.cli).to.have.been.calledWith({
script: path.resolve(__dirname, '..', '..', '..', 'server.js'),
customConfig: options
test.describe('when cli execution fails', () => {
test.afterEach(() => {
process.exitCode = previousExitCode
baseMock.restore()
sandbox.restore()
})

test.it('should trace not controlled errors if cli fails', () => {
const error = new Error('foo')

mockery.resetCache()
mocks = require('../mocks')
previousExitCode = process.exitCode
sandbox = test.sinon.createSandbox()
baseMock = new mocks.Base()
baseMock.stubs.cli.rejects(error)
consoleStub = sandbox.stub(console, 'error')
return require('../../../cli/index').then(() => {
return test.expect(consoleStub).to.have.been.calledWith(error)
})
})

test.it('should not trace controlled errors if cli fails', () => {
const error = new Error()
error.isDomapic = true

mockery.resetCache()
mocks = require('../mocks')
previousExitCode = process.exitCode
sandbox = test.sinon.createSandbox()
baseMock = new mocks.Base()
baseMock.stubs.cli.rejects(error)
consoleStub = sandbox.stub(console, 'error')

return require('../../../cli/index').then(() => {
return test.expect(consoleStub).to.not.have.been.called()
})
})
})
})
3 changes: 2 additions & 1 deletion test/unit/lib/Commands.specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ test.describe('Commands', () => {
'getAll',
'getById',
'get',
'init'
'init',
'remove'
)
})

Expand Down

0 comments on commit 2d28204

Please sign in to comment.