Files
shields/lib/in-process-server-test-helpers.js
Paul Melnikow dc44ba7725 Clean up request-handler, github-auth, and analytics; upgrade to Mocha 4 (#1142)
- Add tests to request-handler to prepare for some tweaks to caching for #820
- Clean up code in request-handler: renames, DRY, arrows, imports
- Allow for clean shutdown of `setInterval` code. This requires the ability to cancel autosaving.
- Upgrade to Mocha 4, and clean up so the process exits on its own (see mochajs/mocha#3044)
- Better encapsulate analytics
2017-10-17 22:01:46 -04:00

67 lines
1.3 KiB
JavaScript

/**
* Helpers to run a Shields server in process.
*
* Usage:
* let server;
* before('Start running the server', function () {
* this.timeout(5000);
* server = serverHelpers.start();
* });
* after('Shut down the server', function () { serverHelpers.stop(server); });
*/
'use strict';
const config = require('./test-config');
let startCalled = false;
/**
* Start the server.
*
* @param {Number} port number (optional)
* @return {Object} The scoutcamp instance
*/
function start () {
if (startCalled) {
throw Error('Because of the way Shields works, you can only use this ' +
'once per node process. Once you call stop(), the game is over.');
}
startCalled = true;
const originalArgv = process.argv;
// Modifying argv during import is a bit dirty, but it works, and avoids
// making bigger changes to server.js.
process.argv = ['', '', config.port, 'localhost'];
const server = require('../server');
process.argv = originalArgv;
return server;
}
/**
* Reset the server, to avoid or reduce side effects between tests.
*
* @param {Object} server instance
*/
function reset (server) {
server.reset();
}
/**
* Stop the server.
*
* @param {Object} server instance
*/
function stop (server) {
if (server) {
server.stop();
}
}
module.exports = {
start,
reset,
stop
};