_test_functions.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict'
  2. import path from 'path'
  3. import express from 'express'
  4. import bodyParser from 'body-parser'
  5. import serveStatic from 'serve-static'
  6. import routes from '../../server/routes'
  7. import { apiPrefix, imageServedUrl, imagesPath } from '../../config'
  8. import connectDb from '../../server/database'
  9. import { errorHandler } from '../../server/functions'
  10. // Path to `test` directory
  11. export const testDir = path.resolve(__dirname, '..')
  12. // Pretty-print a JSON object
  13. export const json = obj => 'JSON DATA : ' + (JSON.stringify(obj, null, 2) || obj)
  14. /**
  15. * Open an Express server not listening to any port.
  16. * The server serves images in `test/images`, all api routes and
  17. * uses a custom error handler (no logging to stdout).
  18. *
  19. * Using `request` (supertest) on this object will start the server
  20. * on an ephemeral port.
  21. *
  22. * @param {PluginConfig} plugins plugins that should be loaded with the server
  23. * @returns {object} an Express server
  24. */
  25. export const getHttpServer = () => {
  26. const app = express()
  27. app.use(bodyParser.json())
  28. app.use(imageServedUrl, serveStatic(imagesPath))
  29. app.use(apiPrefix, routes)
  30. app.use(errorHandler)
  31. return app
  32. }
  33. /** Connect to the database */
  34. export { connectDb }