Files
shields/lib/in-process-server-test-helpers.js
Paul Melnikow c3ef232bf7 Place tests alongside their code (#969)
Reorg of the tests: move them just alongside their code. The principle relates to grouping by coupling, not by function and is established in best-practice documents (e.g. https://github.com/focusaurus/express_code_structure#underlying-principles-and-motivations), despite its break from the tradition of a separate `test/` tree. All of today's tools can handle tests spread through the repository.

There are some good, if subtle consequences of this change:

- Since files are close at hand, friction is reduced at development time, which encourages that new tests are written to cover new behaviors.
- It's easier to find the tests that cover a particular piece of functionality.
- It's easier to see which code has tests and which doesn't.
2017-04-28 00:55:15 -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.requestCache.clear();
}
/**
* Stop the server.
*
* @param {Object} server instance
*/
function stop (server) {
if (server) {
server.camp.close();
}
}
module.exports = {
start,
reset,
stop
};