bootstrap.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. /*
  19. * Global stubs
  20. */
  21. const globalStubsSandbox = sinon.createSandbox()
  22. const globalStubs = {
  23. logger: {
  24. debug: globalStubsSandbox.stub(),
  25. info: globalStubsSandbox.stub(),
  26. log: globalStubsSandbox.stub(),
  27. warn: globalStubsSandbox.stub(),
  28. err: globalStubsSandbox.stub(),
  29. error: globalStubsSandbox.stub(),
  30. fatal: globalStubsSandbox.stub(),
  31. },
  32. }
  33. /*
  34. * Sandboxed module configuration
  35. */
  36. const SandboxedModule = require('sandboxed-module')
  37. SandboxedModule.configure({
  38. ignoreMissing: true,
  39. requires: getSandboxedModuleRequires(),
  40. globals: {
  41. AbortSignal,
  42. Buffer,
  43. Promise,
  44. console,
  45. process,
  46. URL,
  47. TextEncoder,
  48. TextDecoder,
  49. },
  50. })
  51. function getSandboxedModuleRequires() {
  52. const requires = {
  53. '@overleaf/logger': globalStubs.logger,
  54. }
  55. const internalModules = [
  56. '../../app/src/util/promises',
  57. '../../app/src/Features/Errors/Errors',
  58. '../../app/src/Features/Helpers/Mongo',
  59. ]
  60. const externalLibs = [
  61. 'async',
  62. 'bull',
  63. 'json2csv',
  64. 'lodash',
  65. 'marked',
  66. 'moment',
  67. '@overleaf/o-error',
  68. 'sanitize-html',
  69. 'sshpk',
  70. 'underscore',
  71. 'xml2js',
  72. ]
  73. for (const modulePath of internalModules) {
  74. requires[Path.resolve(__dirname, modulePath)] = require(modulePath)
  75. }
  76. for (const lib of externalLibs) {
  77. requires[lib] = require(lib)
  78. }
  79. return requires
  80. }
  81. /*
  82. * Mocha hooks
  83. */
  84. // sandboxed-module somehow registers every fake module it creates in this
  85. // module's children array, which uses quite a big amount of memory. We'll take
  86. // a copy of the module children array and restore it after each test so that
  87. // the garbage collector has a chance to reclaim the fake modules.
  88. let initialModuleChildren
  89. exports.mochaHooks = {
  90. beforeAll() {
  91. // Record initial module children
  92. initialModuleChildren = module.children.slice()
  93. },
  94. beforeEach() {
  95. // Install logger stub
  96. this.logger = globalStubs.logger
  97. },
  98. afterEach() {
  99. // Delete leaking sandboxed modules
  100. module.children = initialModuleChildren.slice()
  101. // Reset global stubs
  102. globalStubsSandbox.reset()
  103. // Restore other stubs
  104. sinon.restore()
  105. },
  106. }