bootstrap.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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-legacy').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. AbortController,
  44. AbortSignal,
  45. Buffer,
  46. Promise,
  47. console,
  48. process,
  49. URL,
  50. TextEncoder,
  51. TextDecoder,
  52. },
  53. })
  54. function getSandboxedModuleRequires() {
  55. const requires = {
  56. '@overleaf/logger': globalStubs.logger,
  57. }
  58. const internalModules = [
  59. '../../app/src/Features/Errors/Errors',
  60. '../../app/src/Features/Helpers/Mongo',
  61. ]
  62. const externalLibs = [
  63. 'async',
  64. 'bull',
  65. 'json2csv',
  66. 'lodash',
  67. 'marked',
  68. 'moment',
  69. '@overleaf/o-error',
  70. 'sanitize-html',
  71. 'sshpk',
  72. 'xml2js',
  73. 'mongodb',
  74. ]
  75. for (const modulePath of internalModules) {
  76. requires[Path.resolve(__dirname, modulePath)] = require(modulePath)
  77. }
  78. for (const lib of externalLibs) {
  79. requires[lib] = require(lib)
  80. }
  81. return requires
  82. }
  83. /*
  84. * Mocha hooks
  85. */
  86. // sandboxed-module somehow registers every fake module it creates in this
  87. // module's children array, which uses quite a big amount of memory. We'll take
  88. // a copy of the module children array and restore it after each test so that
  89. // the garbage collector has a chance to reclaim the fake modules.
  90. let initialModuleChildren
  91. exports.mochaHooks = {
  92. beforeAll() {
  93. // Record initial module children
  94. initialModuleChildren = module.children.slice()
  95. },
  96. beforeEach() {
  97. // Install logger stub
  98. this.logger = globalStubs.logger
  99. },
  100. afterEach() {
  101. // Delete leaking sandboxed modules
  102. module.children = initialModuleChildren.slice()
  103. // Reset global stubs
  104. globalStubsSandbox.reset()
  105. // Restore other stubs
  106. sinon.restore()
  107. },
  108. }