MongoHelper.mjs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { execFile } from 'child_process'
  2. import {
  3. connectionPromise,
  4. db,
  5. dropTestDatabase,
  6. } from '../../../../app/src/infrastructure/mongodb.js'
  7. import Settings from '@overleaf/settings'
  8. const DEFAULT_ENV = 'saas'
  9. export default {
  10. initialize() {
  11. before('wait for db', async function () {
  12. await connectionPromise
  13. })
  14. if (process.env.CLEANUP_MONGO === 'true') {
  15. before('drop test database', dropTestDatabase)
  16. }
  17. before('run migrations', function (done) {
  18. const args = [
  19. 'run',
  20. 'migrations',
  21. '--',
  22. 'migrate',
  23. '-t',
  24. Settings.env || DEFAULT_ENV,
  25. ]
  26. execFile('npm', args, (error, stdout, stderr) => {
  27. if (error) {
  28. throw error
  29. }
  30. done()
  31. })
  32. })
  33. afterEach('purge mongo data', async function () {
  34. return Promise.all(
  35. Object.values(db).map(async collection => {
  36. if (collection === db.migrations) {
  37. // Do not clear the collection for tracking migrations.
  38. return
  39. }
  40. return collection.deleteMany({})
  41. })
  42. )
  43. })
  44. },
  45. }