setup.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. const testLogRecorder = require('@overleaf/logger/test-log-recorder')
  9. // ensure every ObjectId has the id string as a property for correct comparisons
  10. require('mongodb').ObjectId.cacheHexString = true
  11. chai.use(chaiAsPromised)
  12. chai.config.truncateThreshold = 0
  13. async function setupPostgresDatabase() {
  14. this.timeout(60_000)
  15. await knex.migrate.latest()
  16. }
  17. async function setupMongoDatabase() {
  18. this.timeout(60_000)
  19. await promisify(exec)(
  20. // Run saas migrations for backup indexes
  21. `cd ../../tools/migrations && npm run migrations -- migrate -t saas`
  22. )
  23. }
  24. async function createGcsBuckets() {
  25. this.timeout(60_000)
  26. for (const bucket of [
  27. config.get('blobStore.globalBucket'),
  28. config.get('blobStore.projectBucket'),
  29. config.get('chunkStore.bucket'),
  30. config.get('zipStore.bucket'),
  31. 'fake-user-files-gcs',
  32. ]) {
  33. await fetch('http://gcs:9090/storage/v1/b', {
  34. method: 'POST',
  35. body: JSON.stringify({ name: bucket }),
  36. headers: { 'Content-Type': 'application/json' },
  37. })
  38. }
  39. }
  40. // Tear down the connection pool after all the tests have run, so the process
  41. // can exit.
  42. async function tearDownConnectionPool() {
  43. await knex.destroy()
  44. await redis.disconnect()
  45. }
  46. module.exports = {
  47. setupPostgresDatabase,
  48. createGcsBuckets,
  49. tearDownConnectionPool,
  50. mochaHooks: {
  51. beforeAll: [setupPostgresDatabase, setupMongoDatabase, createGcsBuckets],
  52. afterAll: [tearDownConnectionPool],
  53. beforeEach: process.env.CI === 'true' ? [testLogRecorder] : [],
  54. },
  55. }