setup.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. sourceTransformers: {
  33. removeNodePrefix: function (source) {
  34. return source.replace(/require\(['"]node:/g, "require('")
  35. },
  36. },
  37. })
  38. // Mocha hooks
  39. exports.mochaHooks = {
  40. beforeEach() {
  41. this.logger = stubs.logger
  42. },
  43. afterEach() {
  44. sandbox.reset()
  45. },
  46. }