setup.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. const chai = require('chai')
  2. const sinon = require('sinon')
  3. const sinonChai = require('sinon-chai')
  4. const chaiAsPromised = require('chai-as-promised')
  5. const SandboxedModule = require('sandboxed-module')
  6. const timersPromises = require('timers/promises')
  7. // ensure every ObjectId has the id string as a property for correct comparisons
  8. require('mongodb-legacy').ObjectId.cacheHexString = true
  9. process.env.BACKEND = 'gcs'
  10. // Chai configuration
  11. chai.should()
  12. chai.use(sinonChai)
  13. chai.use(chaiAsPromised)
  14. // Global stubs
  15. const sandbox = sinon.createSandbox()
  16. const stubs = {
  17. logger: {
  18. debug: sandbox.stub(),
  19. log: sandbox.stub(),
  20. info: sandbox.stub(),
  21. warn: sandbox.stub(),
  22. err: sandbox.stub(),
  23. error: sandbox.stub(),
  24. fatal: sandbox.stub(),
  25. },
  26. }
  27. // SandboxedModule configuration
  28. SandboxedModule.configure({
  29. requires: {
  30. '@overleaf/logger': stubs.logger,
  31. 'timers/promises': timersPromises,
  32. 'mongodb-legacy': require('mongodb-legacy'),
  33. bson: require('bson'),
  34. },
  35. globals: { Buffer, JSON, Math, console, process },
  36. })
  37. exports.mochaHooks = {
  38. beforeEach() {
  39. this.logger = stubs.logger
  40. },
  41. afterEach() {
  42. sandbox.reset()
  43. },
  44. }