bootstrap.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. const Path = require('path')
  2. const sinon = require('sinon')
  3. require('./common_bootstrap')
  4. const chai = require('chai')
  5. /*
  6. * Chai configuration
  7. */
  8. // add chai.should()
  9. chai.should()
  10. // Load sinon-chai assertions so expect(stubFn).to.have.been.calledWith('abc')
  11. // has a nicer failure messages
  12. chai.use(require('sinon-chai'))
  13. // Load promise support for chai
  14. chai.use(require('chai-as-promised'))
  15. // Do not truncate assertion errors
  16. chai.config.truncateThreshold = 0
  17. /*
  18. * Global stubs
  19. */
  20. const globalStubsSandbox = sinon.createSandbox()
  21. const globalStubs = {
  22. logger: {
  23. debug: globalStubsSandbox.stub(),
  24. info: globalStubsSandbox.stub(),
  25. log: globalStubsSandbox.stub(),
  26. warn: globalStubsSandbox.stub(),
  27. err: globalStubsSandbox.stub(),
  28. error: globalStubsSandbox.stub(),
  29. fatal: globalStubsSandbox.stub(),
  30. },
  31. }
  32. /*
  33. * Sandboxed module configuration
  34. */
  35. const SandboxedModule = require('sandboxed-module')
  36. SandboxedModule.configure({
  37. ignoreMissing: true,
  38. requires: getSandboxedModuleRequires(),
  39. globals: {
  40. AbortController,
  41. AbortSignal,
  42. Buffer,
  43. Promise,
  44. console,
  45. process,
  46. URL,
  47. TextEncoder,
  48. TextDecoder,
  49. },
  50. sourceTransformers: {
  51. removeNodePrefix: function (source) {
  52. return source.replace(/require\(['"]node:/g, "require('")
  53. },
  54. },
  55. })
  56. function getSandboxedModuleRequires() {
  57. const requires = {
  58. '@overleaf/logger': globalStubs.logger,
  59. }
  60. const internalModules = ['../../app/src/Features/Errors/Errors']
  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. 'mongodb-legacy',
  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. }