overleaf/services/web/test/unit/bootstrap.js

128 lines
2.7 KiB
JavaScript
Raw Normal View History

const Path = require('path')
const chai = require('chai')
const sinon = require('sinon')
/*
* Chai configuration
*/
// add chai.should()
chai.should()
// Load sinon-chai assertions so expect(stubFn).to.have.been.calledWith('abc')
// has a nicer failure messages
chai.use(require('sinon-chai'))
// Load promise support for chai
chai.use(require('chai-as-promised'))
// Do not truncate assertion errors
chai.config.truncateThreshold = 0
// add support for mongoose in sinon
require('sinon-mongoose')
// ensure every ObjectId has the id string as a property for correct comparisons
require('mongodb').ObjectId.cacheHexString = true
/*
* Global stubs
*/
const globalStubsSandbox = sinon.createSandbox()
const globalStubs = {
logger: {
debug: globalStubsSandbox.stub(),
info: globalStubsSandbox.stub(),
log: globalStubsSandbox.stub(),
warn: globalStubsSandbox.stub(),
err: globalStubsSandbox.stub(),
error: globalStubsSandbox.stub(),
fatal: globalStubsSandbox.stub(),
},
}
/*
* Sandboxed module configuration
*/
const SandboxedModule = require('sandboxed-module')
SandboxedModule.configure({
ignoreMissing: true,
requires: getSandboxedModuleRequires(),
globals: {
[web] Promisify ProjectController (#18477) * Create `promiseAuto` util to replace `async.auto` * Promisify `BrandVariationsHandler.getBrandVariationById` * Promisify `updateProjectSettings` * Promisify `updateProjectAdminSettings` * Promisify `newProject` * Promisify `deleteProject` * Promisify `loadEditor` * Fix brandVariation loading in promise auto * Promisify `_refreshFeatures` * Promisify `_injectProjectUsers` * Fix `no-inner-declarations` * Promisify `cloneProject` * Promisify `userProjectsJson` * Promisify `projectEntitiesJson` * Promisify `restoreProject` * Promisify `renameProject` * Additional warning fix * Update unit tests * Fixup `updateProjectSettings`: call jobs inside the Promise.all * Use `expressify(...)` instead of manually call `next(err)` https://github.com/overleaf/internal/pull/18477#discussion_r1613611987 https://github.com/overleaf/internal/pull/18477#discussion_r1613621146 https://github.com/overleaf/internal/pull/18477#discussion_r1613634000 ... * Replace Promise.all by sequencial awaits https://github.com/overleaf/internal/pull/18477#discussion_r1613852746 https://github.com/overleaf/internal/pull/18477#discussion_r1613611987 * Remove manual throws of 500. Let the generic error handler catch them. https://github.com/overleaf/internal/pull/18477#discussion_r1613623446 https://github.com/overleaf/internal/pull/18477#discussion_r1613628955 * Promisify `untrashProject` https://github.com/overleaf/internal/pull/18477#discussion_r1613627783 * Promisify `expireDeletedProjectsAfterDuration` * Promisify `archiveProject` * Promisify `unarchiveProject` * Promisify `trashProject` * Promisify `expireDeletedProject` * Use async `setTimeout` from `timers/promise` https://github.com/overleaf/internal/pull/18477#discussion_r1613843085 * Remove unused `_injectProjectUsers` https://github.com/overleaf/internal/pull/18477#discussion_r1613855766 * Add missing exec in queries (?) Not sure if that makes a real difference but it's more consistent with the rest of the code * Catch floating promises https://github.com/overleaf/internal/pull/18477#discussion_r1613868876 * Replace custom `promiseAuto` by `p-props` from NPM https://github.com/overleaf/internal/pull/18477#discussion_r1613393294 * Downgrade `p-props` to v4. Later versions require ESM * Simplify code around `splitTestAssignments` GitOrigin-RevId: 84d37f7aa9227b5b9acf9eeb5db1b78afc01b6ee
2024-05-29 12:19:10 +00:00
AbortController,
AbortSignal,
Buffer,
Promise,
console,
process,
URL,
TextEncoder,
TextDecoder,
},
})
function getSandboxedModuleRequires() {
const requires = {
'@overleaf/logger': globalStubs.logger,
}
const internalModules = [
'../../app/src/Features/Errors/Errors',
'../../app/src/Features/Helpers/Mongo',
]
const externalLibs = [
'async',
'bull',
'json2csv',
'lodash',
'marked',
'moment',
'@overleaf/o-error',
'sanitize-html',
'sshpk',
'xml2js',
'mongodb',
]
for (const modulePath of internalModules) {
requires[Path.resolve(__dirname, modulePath)] = require(modulePath)
}
for (const lib of externalLibs) {
requires[lib] = require(lib)
}
return requires
}
/*
* Mocha hooks
*/
// 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
exports.mochaHooks = {
beforeAll() {
// Record initial module children
initialModuleChildren = module.children.slice()
},
beforeEach() {
// Install logger stub
this.logger = globalStubs.logger
},
afterEach() {
// Delete leaking sandboxed modules
module.children = initialModuleChildren.slice()
// Reset global stubs
globalStubsSandbox.reset()
// Restore other stubs
sinon.restore()
},
}