bootstrap.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. const chai = require('chai')
  2. const sinon = require('sinon')
  3. // add chai.should()
  4. chai.should()
  5. // Load sinon-chai assertions so expect(stubFn).to.have.been.calledWith('abc')
  6. // has a nicer failure messages
  7. chai.use(require('sinon-chai'))
  8. // Load promise support for chai
  9. chai.use(require('chai-as-promised'))
  10. // Do not truncate assertion errors
  11. chai.config.truncateThreshold = 0
  12. // add support for mongoose in sinon
  13. require('sinon-mongoose')
  14. afterEach(function() {
  15. sinon.restore()
  16. })
  17. const SandboxedModule = require('sandboxed-module')
  18. const PromisesUtils = require('../../app/src/util/promises')
  19. const Errors = require('../../app/src/Features/Errors/Errors')
  20. const GLOBAL_REQUIRE_CACHE_FOR_SANDBOXED_MODULES = {
  21. // cache p-limit for all expressify/promisifyAll users
  22. '../../util/promises': PromisesUtils,
  23. '../../../../app/src/util/promises': PromisesUtils,
  24. // Errors are widely used and instance checks need the exact same prototypes
  25. '../Errors/Errors': Errors,
  26. '../../../../app/src/Features/Errors/Errors': Errors,
  27. '../../../../../app/src/Features/Errors/Errors': Errors
  28. }
  29. const LIBRARIES = [
  30. '@overleaf/o-error',
  31. 'async',
  32. 'lodash',
  33. 'moment',
  34. 'underscore',
  35. 'xml2js',
  36. 'json2csv',
  37. 'sanitize-html',
  38. 'marked'
  39. ]
  40. LIBRARIES.forEach(lib => {
  41. GLOBAL_REQUIRE_CACHE_FOR_SANDBOXED_MODULES[lib] = require(lib)
  42. })
  43. SandboxedModule.configure({
  44. requires: GLOBAL_REQUIRE_CACHE_FOR_SANDBOXED_MODULES
  45. })
  46. // sandboxed-module somehow registers every fake module it creates in this
  47. // module's children array, which uses quite a big amount of memory. We'll take
  48. // a copy of the module children array and restore it after each test so that
  49. // the garbage collector has a chance to reclaim the fake modules.
  50. let initialModuleChildren
  51. before('record initial module children', function() {
  52. initialModuleChildren = module.children.slice()
  53. })
  54. afterEach('restore module children', function() {
  55. // Delete leaking sandboxed modules
  56. module.children = initialModuleChildren.slice()
  57. })