setup.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. 'overleaf-editor-core': require('overleaf-editor-core'), // does not play nice with sandbox
  31. },
  32. globals: { Buffer, JSON, Math, console, process, URL },
  33. sourceTransformers: {
  34. removeNodePrefix: function (source) {
  35. return source.replace(/require\(['"]node:/g, "require('")
  36. },
  37. },
  38. })
  39. // Mocha hooks
  40. exports.mochaHooks = {
  41. beforeEach() {
  42. this.logger = stubs.logger
  43. },
  44. afterEach() {
  45. sandbox.reset()
  46. },
  47. }