|
@@ -2,9 +2,13 @@
|
|
|
|
|
|
import path from 'path'
|
|
|
import express from 'express'
|
|
|
+import WebSocket from 'ws'
|
|
|
import serveStatic from 'serve-static'
|
|
|
import routes from '../../server/routes'
|
|
|
import { apiPrefix, imageServedUrl, imagesPath } from '../../config'
|
|
|
+import connectDb from '../../server/database'
|
|
|
+import { errorHandler as wsErrorHandler } from '../../server/webSocket'
|
|
|
+import wsMessageHandler from '../../server/webSocket/messageHandler'
|
|
|
|
|
|
// Path to `test` directory
|
|
|
export const testDir = path.resolve(__dirname, '..')
|
|
@@ -13,15 +17,25 @@ export const testDir = path.resolve(__dirname, '..')
|
|
|
export const json = obj => 'JSON DATA : ' + (JSON.stringify(obj, null, 2) || obj)
|
|
|
|
|
|
/**
|
|
|
- * Uses supertest to open an Express server on an ephemeral port.
|
|
|
+ * @typedef PluginConfig
|
|
|
+ * @property {boolean} [webSocket=false] should the server start with a WebSocket server
|
|
|
+ * @property {boolean} [database=false] should the server start with a WebSocket server
|
|
|
+ */
|
|
|
+/**
|
|
|
+ * Open an Express server not listening to any port.
|
|
|
* The server serves images in `test/images`, all api routes and
|
|
|
* uses a custom error handler (no logging to stdout).
|
|
|
*
|
|
|
* Using `request` (supertest) on this object will start the server
|
|
|
- *
|
|
|
+ * on an ephemeral port.
|
|
|
+ * @param {PluginConfig} plugins plugins that should be loaded with the server
|
|
|
* @returns {object} an Express server
|
|
|
*/
|
|
|
-export const serve = () => {
|
|
|
+const serve = async (plugins = { webSocket: false, database: false }) => {
|
|
|
+ // Connect to db
|
|
|
+ if (plugins && plugins.database) await connectDb()
|
|
|
+
|
|
|
+ // Open a HTTP server
|
|
|
const app = express()
|
|
|
app.use(imageServedUrl, serveStatic(imagesPath))
|
|
|
app.use(apiPrefix, routes)
|
|
@@ -31,10 +45,22 @@ export const serve = () => {
|
|
|
data: err.data || undefined
|
|
|
})
|
|
|
})
|
|
|
+
|
|
|
+ // Open a WebSocket server
|
|
|
+ if (plugins && plugins.webSocket) {
|
|
|
+ const wss = new WebSocket.Server({ server: app })
|
|
|
+ wss.on('error', err => {
|
|
|
+ throw err
|
|
|
+ })
|
|
|
+ wss.on('connection', ws => {
|
|
|
+ ws.on('message', data => wsMessageHandler(ws)(data).catch(wsErrorHandler(ws)))
|
|
|
+ ws.on('error', wsErrorHandler(ws))
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
return app
|
|
|
}
|
|
|
|
|
|
-// Before each tests, start a server
|
|
|
-export const beforeEachTests = async t => {
|
|
|
- t.context.server = serve()
|
|
|
-}
|
|
|
+// Pass a server to test context
|
|
|
+export const getTestServer = async (t, plugins) => (t.context.server = await serve(plugins))
|
|
|
+
|