setup.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. const chai = require('chai')
  2. const chaiAsPromised = require('chai-as-promised')
  3. const sinonChai = require('sinon-chai')
  4. const SandboxedModule = require('sandboxed-module')
  5. const sinon = require('sinon')
  6. // ensure every ObjectId has the id string as a property for correct comparisons
  7. require('mongodb-legacy').ObjectId.cacheHexString = true
  8. // Chai configuration
  9. chai.should()
  10. chai.use(chaiAsPromised)
  11. // Load sinon-chai assertions so expect(stubFn).to.have.been.calledWith('abc')
  12. // has a nicer failure messages
  13. chai.use(sinonChai)
  14. // Global stubs
  15. const sandbox = sinon.createSandbox()
  16. const stubs = {
  17. logger: {
  18. debug: sandbox.stub(),
  19. log: sandbox.stub(),
  20. warn: sandbox.stub(),
  21. err: sandbox.stub(),
  22. error: sandbox.stub(),
  23. },
  24. }
  25. // SandboxedModule configuration
  26. SandboxedModule.configure({
  27. requires: {
  28. '@overleaf/logger': stubs.logger,
  29. 'mongodb-legacy': require('mongodb-legacy'), // for ObjectId comparisons
  30. },
  31. globals: { Buffer, JSON, Math, console, process },
  32. })
  33. // Mocha hooks
  34. exports.mochaHooks = {
  35. beforeEach() {
  36. this.logger = stubs.logger
  37. },
  38. afterEach() {
  39. sandbox.reset()
  40. },
  41. }