bootstrap.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. const Path = require('path')
  2. const chai = require('chai')
  3. const sinon = require('sinon')
  4. /*
  5. * Chai configuration
  6. */
  7. // add chai.should()
  8. chai.should()
  9. // Load sinon-chai assertions so expect(stubFn).to.have.been.calledWith('abc')
  10. // has a nicer failure messages
  11. chai.use(require('sinon-chai'))
  12. // Load promise support for chai
  13. chai.use(require('chai-as-promised'))
  14. // Do not truncate assertion errors
  15. chai.config.truncateThreshold = 0
  16. // add support for mongoose in sinon
  17. require('sinon-mongoose')
  18. // ensure every ObjectId has the id string as a property for correct comparisons
  19. require('mongodb').ObjectId.cacheHexString = true
  20. /*
  21. * Global stubs
  22. */
  23. const globalStubsSandbox = sinon.createSandbox()
  24. const globalStubs = {
  25. logger: {
  26. debug: globalStubsSandbox.stub(),
  27. info: globalStubsSandbox.stub(),
  28. log: globalStubsSandbox.stub(),
  29. warn: globalStubsSandbox.stub(),
  30. err: globalStubsSandbox.stub(),
  31. error: globalStubsSandbox.stub(),
  32. fatal: globalStubsSandbox.stub(),
  33. },
  34. }
  35. /*
  36. * Sandboxed module configuration
  37. */
  38. const SandboxedModule = require('sandboxed-module')
  39. SandboxedModule.configure({
  40. ignoreMissing: true,
  41. requires: getSandboxedModuleRequires(),
  42. globals: {
  43. AbortSignal,
  44. Buffer,
  45. Promise,
  46. console,
  47. process,
  48. URL,
  49. TextEncoder,
  50. TextDecoder,
  51. },
  52. })
  53. function getSandboxedModuleRequires() {
  54. const requires = {
  55. '@overleaf/logger': globalStubs.logger,
  56. }
  57. const internalModules = [
  58. '../../app/src/Features/Errors/Errors',
  59. '../../app/src/Features/Helpers/Mongo',
  60. ]
  61. const externalLibs = [
  62. 'async',
  63. 'bull',
  64. 'json2csv',
  65. 'lodash',
  66. 'marked',
  67. 'moment',
  68. '@overleaf/o-error',
  69. 'sanitize-html',
  70. 'sshpk',
  71. 'xml2js',
  72. 'mongodb',
  73. ]
  74. for (const modulePath of internalModules) {
  75. requires[Path.resolve(__dirname, modulePath)] = require(modulePath)
  76. }
  77. for (const lib of externalLibs) {
  78. requires[lib] = require(lib)
  79. }
  80. return requires
  81. }
  82. /*
  83. * Mocha hooks
  84. */
  85. // sandboxed-module somehow registers every fake module it creates in this
  86. // module's children array, which uses quite a big amount of memory. We'll take
  87. // a copy of the module children array and restore it after each test so that
  88. // the garbage collector has a chance to reclaim the fake modules.
  89. let initialModuleChildren
  90. exports.mochaHooks = {
  91. beforeAll() {
  92. // Record initial module children
  93. initialModuleChildren = module.children.slice()
  94. },
  95. beforeEach() {
  96. // Install logger stub
  97. this.logger = globalStubs.logger
  98. },
  99. afterEach() {
  100. // Delete leaking sandboxed modules
  101. module.children = initialModuleChildren.slice()
  102. // Reset global stubs
  103. globalStubsSandbox.reset()
  104. // Restore other stubs
  105. sinon.restore()
  106. },
  107. }