2019-05-28 07:04:44 -04:00
|
|
|
const chai = require('chai')
|
2019-11-18 09:37:05 -05:00
|
|
|
const sinon = require('sinon')
|
2019-05-28 07:04:44 -04:00
|
|
|
|
2020-05-22 06:34:57 -04:00
|
|
|
// add chai.should()
|
|
|
|
chai.should()
|
|
|
|
|
2019-05-28 07:04:44 -04:00
|
|
|
// Load sinon-chai assertions so expect(stubFn).to.have.been.calledWith('abc')
|
|
|
|
// has a nicer failure messages
|
|
|
|
chai.use(require('sinon-chai'))
|
2019-07-18 10:18:56 -04:00
|
|
|
|
2019-08-28 08:59:41 -04:00
|
|
|
// Load promise support for chai
|
|
|
|
chai.use(require('chai-as-promised'))
|
|
|
|
|
2019-10-07 04:30:51 -04:00
|
|
|
// Do not truncate assertion errors
|
|
|
|
chai.config.truncateThreshold = 0
|
|
|
|
|
2019-07-18 10:18:56 -04:00
|
|
|
// add support for mongoose in sinon
|
|
|
|
require('sinon-mongoose')
|
2019-11-07 05:28:27 -05:00
|
|
|
|
2019-11-18 09:37:05 -05:00
|
|
|
afterEach(function() {
|
|
|
|
sinon.restore()
|
|
|
|
})
|
2020-11-27 08:11:12 -05:00
|
|
|
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const PromisesUtils = require('../../app/src/util/promises')
|
|
|
|
const Errors = require('../../app/src/Features/Errors/Errors')
|
|
|
|
const GLOBAL_REQUIRE_CACHE_FOR_SANDBOXED_MODULES = {
|
|
|
|
// cache p-limit for all expressify/promisifyAll users
|
|
|
|
'../../util/promises': PromisesUtils,
|
|
|
|
'../../../../app/src/util/promises': PromisesUtils,
|
|
|
|
|
|
|
|
// Errors are widely used and instance checks need the exact same prototypes
|
|
|
|
'../Errors/Errors': Errors,
|
|
|
|
'../../../../app/src/Features/Errors/Errors': Errors,
|
|
|
|
'../../../../../app/src/Features/Errors/Errors': Errors
|
|
|
|
}
|
|
|
|
const LIBRARIES = [
|
|
|
|
'@overleaf/o-error',
|
|
|
|
'async',
|
|
|
|
'lodash',
|
|
|
|
'moment',
|
|
|
|
'underscore',
|
|
|
|
'xml2js',
|
|
|
|
'json2csv',
|
|
|
|
'sanitize-html',
|
|
|
|
'marked'
|
|
|
|
]
|
|
|
|
LIBRARIES.forEach(lib => {
|
|
|
|
GLOBAL_REQUIRE_CACHE_FOR_SANDBOXED_MODULES[lib] = require(lib)
|
|
|
|
})
|
|
|
|
|
|
|
|
SandboxedModule.configure({
|
|
|
|
requires: GLOBAL_REQUIRE_CACHE_FOR_SANDBOXED_MODULES
|
|
|
|
})
|
2021-02-03 07:36:39 -05:00
|
|
|
|
|
|
|
// sandboxed-module somehow registers every fake module it creates in this
|
|
|
|
// module's children array, which uses quite a big amount of memory. We'll take
|
|
|
|
// a copy of the module children array and restore it after each test so that
|
|
|
|
// the garbage collector has a chance to reclaim the fake modules.
|
|
|
|
let initialModuleChildren
|
|
|
|
before('record initial module children', function() {
|
|
|
|
initialModuleChildren = module.children.slice()
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach('restore module children', function() {
|
|
|
|
// Delete leaking sandboxed modules
|
|
|
|
module.children = initialModuleChildren.slice()
|
|
|
|
})
|