Files
Robot-Clash/server/test/helpers.js

24 lines
884 B
JavaScript
Raw Normal View History

/* Test helpers: start the real server on a random port with an in-memory database. */
'use strict';
process.env.NODE_ENV = 'test';
process.env.DB_FILE = ':memory:';
delete process.env.JWT_SECRET;
const { listen } = require('../app');
async function startServer() {
const srv = await listen(0);
const base = 'http://127.0.0.1:' + srv.address().port;
const json = async (method, path, body, token) => {
const res = await fetch(base + path, {
method,
headers: Object.assign({ 'Content-Type': 'application/json' }, token ? { Authorization: 'Bearer ' + token } : {}),
body: body === undefined ? undefined : JSON.stringify(body),
});
return { status: res.status, body: await res.json().catch(() => null) };
};
return { base, json, close: () => new Promise(r => { srv.closeAllConnections(); srv.close(r); }) };
}
module.exports = { startServer };