setup.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. const chai = require('chai')
  2. const chaiAsPromised = require('chai-as-promised')
  3. const config = require('config')
  4. const fetch = require('node-fetch')
  5. const { knex, redis } = require('../storage')
  6. const { exec } = require('node:child_process')
  7. const { promisify } = require('node:util')
  8. // ensure every ObjectId has the id string as a property for correct comparisons
  9. require('mongodb').ObjectId.cacheHexString = true
  10. chai.use(chaiAsPromised)
  11. chai.config.truncateThreshold = 0
  12. async function setupPostgresDatabase() {
  13. this.timeout(60_000)
  14. await knex.migrate.latest()
  15. }
  16. async function setupMongoDatabase() {
  17. this.timeout(60_000)
  18. await promisify(exec)(
  19. `cd ../../tools/migrations && npm run migrations -- migrate -t server-ce`
  20. )
  21. }
  22. async function createGcsBuckets() {
  23. this.timeout(60_000)
  24. for (const bucket of [
  25. config.get('blobStore.globalBucket'),
  26. config.get('blobStore.projectBucket'),
  27. config.get('chunkStore.bucket'),
  28. config.get('zipStore.bucket'),
  29. 'fake-user-files-gcs',
  30. ]) {
  31. await fetch('http://gcs:9090/storage/v1/b', {
  32. method: 'POST',
  33. body: JSON.stringify({ name: bucket }),
  34. headers: { 'Content-Type': 'application/json' },
  35. })
  36. }
  37. }
  38. // Tear down the connection pool after all the tests have run, so the process
  39. // can exit.
  40. async function tearDownConnectionPool() {
  41. await knex.destroy()
  42. await redis.disconnect()
  43. }
  44. module.exports = {
  45. setupPostgresDatabase,
  46. createGcsBuckets,
  47. tearDownConnectionPool,
  48. mochaHooks: {
  49. beforeAll: [setupPostgresDatabase, setupMongoDatabase, createGcsBuckets],
  50. afterAll: [tearDownConnectionPool],
  51. },
  52. }