From 62a7fcb16df475416d70495d416549e6574c2ec6 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Sun, 19 Jun 2022 20:21:40 -0700 Subject: [PATCH] fix(tests): Got bin/server.js unit test coverage to 100% --- bin/server.js | 15 ++++++++------- test/unit/misc/server-unit.js | 26 +++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/bin/server.js b/bin/server.js index 66daf57..0d2871a 100644 --- a/bin/server.js +++ b/bin/server.js @@ -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) } } + + sleep (ms) { + return new Promise((resolve) => setTimeout(resolve, ms)) + } } -function sleep (ms) { - return new Promise((resolve) => setTimeout(resolve, ms)) -} - module.exports = Server diff --git a/test/unit/misc/server-unit.js b/test/unit/misc/server-unit.js index 1710fc7..a5b1f95 100644 --- a/test/unit/misc/server-unit.js +++ b/test/unit/misc/server-unit.js @@ -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) }) }) })