setup.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. // Run saas migrations for backup indexes
  20. `cd ../../tools/migrations && npm run migrations -- migrate -t saas`
  21. )
  22. }
  23. async function createGcsBuckets() {
  24. this.timeout(60_000)
  25. for (const bucket of [
  26. config.get('blobStore.globalBucket'),
  27. config.get('blobStore.projectBucket'),
  28. config.get('chunkStore.bucket'),
  29. config.get('zipStore.bucket'),
  30. 'fake-user-files-gcs',
  31. ]) {
  32. await fetch('http://gcs:9090/storage/v1/b', {
  33. method: 'POST',
  34. body: JSON.stringify({ name: bucket }),
  35. headers: { 'Content-Type': 'application/json' },
  36. })
  37. }
  38. }
  39. // Tear down the connection pool after all the tests have run, so the process
  40. // can exit.
  41. async function tearDownConnectionPool() {
  42. await knex.destroy()
  43. await redis.disconnect()
  44. }
  45. module.exports = {
  46. setupPostgresDatabase,
  47. createGcsBuckets,
  48. tearDownConnectionPool,
  49. mochaHooks: {
  50. beforeAll: [setupPostgresDatabase, setupMongoDatabase, createGcsBuckets],
  51. afterAll: [tearDownConnectionPool],
  52. },
  53. }