MongoHelper.js 1.0 KB

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