fix(tests): Got bin/server.js unit test coverage to 100%

This commit is contained in:
Chris Troutner
2022-06-19 20:21:40 -07:00
parent d8addd5b27
commit 62a7fcb16d
2 changed files with 33 additions and 8 deletions
+6 -5
View File
@@ -33,6 +33,7 @@ class Server {
this.controllers = new Controllers()
this.mongoose = mongoose
this.config = config
this.process = process
}
async startServer () {
@@ -88,7 +89,7 @@ class Server {
console.log(`Running server in environment: ${this.config.env}`)
wlogger.info(`Running server in environment: ${this.config.env}`)
await app.listen(this.config.port)
this.server = await app.listen(this.config.port)
console.log(`Server started on ${this.config.port}`)
// Create the system admin user.
@@ -114,14 +115,14 @@ class Server {
console.log(
'Exiting after 5 seconds. Depending on process manager to restart.'
)
await sleep(5000)
process.exit(1)
}
await this.sleep(5000)
this.process.exit(1)
}
}
function sleep (ms) {
sleep (ms) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
}
module.exports = Server
+25 -1
View File
@@ -27,13 +27,37 @@ describe('#server', () => {
sandbox.stub(uut.controllers, 'attachRESTControllers').resolves()
sandbox.stub(uut.adminLib, 'createSystemUser').resolves(true)
sandbox.stub(uut.controllers, 'attachControllers').resolves()
uut.config.env = 'dev'
const result = await uut.startServer()
// console.log('result: ', result)
assert.property(result, 'env')
result.close()
// Turn off the server.
uut.server.close()
// Restor config env
uut.config.env = 'test'
})
it('should exit on failure', async () => {
// Force an error
sandbox.stub(uut.mongoose, 'connect').rejects(new Error('test error'))
// Prevent default behavior of exiting the program.
sandbox.stub(uut, 'sleep').resolves()
sandbox.stub(uut.process, 'exit').returns()
await uut.startServer()
// Not throwing an error is a success
})
})
describe('#sleep', () => {
it('should execute', async () => {
await uut.sleep(1)
})
})
})