Add global test setup

Configure chai and SandboxedModule in a central place. Configure
SandboxedModule globals that are required in Node 12.
This commit is contained in:
Eric Mc Sween 2021-03-23 15:08:32 -04:00
parent ccb8a022a9
commit a793ecd41b
24 changed files with 47 additions and 102 deletions

View file

@ -0,0 +1,3 @@
{
"require": "test/setup.js"
}

View file

@ -11,9 +11,7 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const sinon = require('sinon') const sinon = require('sinon')
const chai = require('chai') const { expect } = require('chai')
chai.should()
const { expect } = chai
const { ObjectId } = require('../../../app/js/mongodb') const { ObjectId } = require('../../../app/js/mongodb')
const Settings = require('settings-sharelatex') const Settings = require('settings-sharelatex')
const request = require('request') const request = require('request')

View file

@ -15,9 +15,7 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const sinon = require('sinon') const sinon = require('sinon')
const chai = require('chai') const { expect } = require('chai')
chai.should()
const { expect } = chai
const { db, ObjectId } = require('../../../app/js/mongodb') const { db, ObjectId } = require('../../../app/js/mongodb')
const Settings = require('settings-sharelatex') const Settings = require('settings-sharelatex')
const request = require('request') const request = require('request')

View file

@ -11,9 +11,7 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const sinon = require('sinon') const sinon = require('sinon')
const chai = require('chai') const { expect } = require('chai')
chai.should()
const { expect } = chai
const { ObjectId } = require('../../../app/js/mongodb') const { ObjectId } = require('../../../app/js/mongodb')
const Settings = require('settings-sharelatex') const Settings = require('settings-sharelatex')
const request = require('request') const request = require('request')

View file

@ -10,9 +10,7 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const sinon = require('sinon') const sinon = require('sinon')
const chai = require('chai') const { expect } = require('chai')
chai.should()
const { expect } = chai
const { ObjectId } = require('../../../app/js/mongodb') const { ObjectId } = require('../../../app/js/mongodb')
const Settings = require('settings-sharelatex') const Settings = require('settings-sharelatex')

View file

@ -11,9 +11,7 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const sinon = require('sinon') const sinon = require('sinon')
const chai = require('chai') const { expect } = require('chai')
chai.should()
const { expect } = chai
const { ObjectId } = require('../../../app/js/mongodb') const { ObjectId } = require('../../../app/js/mongodb')
const Settings = require('settings-sharelatex') const Settings = require('settings-sharelatex')

View file

@ -10,9 +10,7 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const sinon = require('sinon') const sinon = require('sinon')
const chai = require('chai') const { expect } = require('chai')
chai.should()
const { expect } = chai
const Settings = require('settings-sharelatex') const Settings = require('settings-sharelatex')
const LockManager = require('../../../app/js/LockManager') const LockManager = require('../../../app/js/LockManager')
const rclient = require('redis').createClient(Settings.redis.history) // Only works locally for now const rclient = require('redis').createClient(Settings.redis.history) // Only works locally for now

View file

@ -10,9 +10,7 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const sinon = require('sinon') const sinon = require('sinon')
const chai = require('chai') const { expect } = require('chai')
chai.should()
const { expect } = chai
const { ObjectId } = require('../../../app/js/mongodb') const { ObjectId } = require('../../../app/js/mongodb')
const Settings = require('settings-sharelatex') const Settings = require('settings-sharelatex')

View file

@ -14,7 +14,6 @@
*/ */
const app = require('../../../../app') const app = require('../../../../app')
const { waitForDb } = require('../../../../app/js/mongodb') const { waitForDb } = require('../../../../app/js/mongodb')
require('logger-sharelatex')
const logger = require('logger-sharelatex') const logger = require('logger-sharelatex')
const Settings = require('settings-sharelatex') const Settings = require('settings-sharelatex')

View file

@ -0,0 +1,21 @@
const chai = require('chai')
const SandboxedModule = require('sandboxed-module')
// Chai configuration
chai.should()
// SandboxedModule configuration
SandboxedModule.configure({
requires: {
'logger-sharelatex': {
debug() {},
log() {},
info() {},
warn() {},
err() {},
error() {},
fatal() {}
}
},
globals: { Buffer, JSON, console, process }
})

View file

@ -11,19 +11,13 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const sinon = require('sinon') const sinon = require('sinon')
const chai = require('chai') const { expect } = require('chai')
const should = chai.should()
const { expect } = chai
const modulePath = '../../../../app/js/DiffGenerator.js' const modulePath = '../../../../app/js/DiffGenerator.js'
const SandboxedModule = require('sandboxed-module') const SandboxedModule = require('sandboxed-module')
describe('DiffGenerator', function () { describe('DiffGenerator', function () {
beforeEach(function () { beforeEach(function () {
this.DiffGenerator = SandboxedModule.require(modulePath, { this.DiffGenerator = SandboxedModule.require(modulePath, {})
requires: {
'logger-sharelatex': { warn: sinon.stub() }
}
})
this.ts = Date.now() this.ts = Date.now()
this.user_id = 'mock-user-id' this.user_id = 'mock-user-id'
this.user_id_2 = 'mock-user-id-2' this.user_id_2 = 'mock-user-id-2'

View file

@ -11,9 +11,7 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const sinon = require('sinon') const sinon = require('sinon')
const chai = require('chai') const { expect } = require('chai')
const should = chai.should()
const { expect } = chai
const modulePath = '../../../../app/js/DiffManager.js' const modulePath = '../../../../app/js/DiffManager.js'
const SandboxedModule = require('sandboxed-module') const SandboxedModule = require('sandboxed-module')
@ -21,11 +19,6 @@ describe('DiffManager', function () {
beforeEach(function () { beforeEach(function () {
this.DiffManager = SandboxedModule.require(modulePath, { this.DiffManager = SandboxedModule.require(modulePath, {
requires: { requires: {
'logger-sharelatex': (this.logger = {
log: sinon.stub(),
error: sinon.stub(),
warn: sinon.stub()
}),
'./UpdatesManager': (this.UpdatesManager = {}), './UpdatesManager': (this.UpdatesManager = {}),
'./DocumentUpdaterManager': (this.DocumentUpdaterManager = {}), './DocumentUpdaterManager': (this.DocumentUpdaterManager = {}),
'./DiffGenerator': (this.DiffGenerator = {}) './DiffGenerator': (this.DiffGenerator = {})

View file

@ -9,8 +9,6 @@
* DS102: Remove unnecessary code created because of implicit returns * DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const chai = require('chai')
chai.should()
const sinon = require('sinon') const sinon = require('sinon')
const modulePath = '../../../../app/js/MongoAWS.js' const modulePath = '../../../../app/js/MongoAWS.js'
const SandboxedModule = require('sandboxed-module') const SandboxedModule = require('sandboxed-module')
@ -36,11 +34,6 @@ describe('MongoAWS', function () {
}), }),
child_process: (this.child_process = {}), child_process: (this.child_process = {}),
'mongo-uri': (this.mongouri = {}), 'mongo-uri': (this.mongouri = {}),
'logger-sharelatex': (this.logger = {
log: sinon.stub(),
error: sinon.stub(),
err() {}
}),
'aws-sdk': (this.awssdk = {}), 'aws-sdk': (this.awssdk = {}),
fs: (this.fs = {}), fs: (this.fs = {}),
's3-streams': (this.S3S = {}), 's3-streams': (this.S3S = {}),

View file

@ -10,9 +10,7 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const sinon = require('sinon') const sinon = require('sinon')
const chai = require('chai') const { expect } = require('chai')
const should = chai.should()
const { expect } = chai
const modulePath = '../../../../app/js/DocumentUpdaterManager.js' const modulePath = '../../../../app/js/DocumentUpdaterManager.js'
const SandboxedModule = require('sandboxed-module') const SandboxedModule = require('sandboxed-module')
@ -21,10 +19,6 @@ describe('DocumentUpdaterManager', function () {
this.DocumentUpdaterManager = SandboxedModule.require(modulePath, { this.DocumentUpdaterManager = SandboxedModule.require(modulePath, {
requires: { requires: {
request: (this.request = {}), request: (this.request = {}),
'logger-sharelatex': (this.logger = {
log: sinon.stub(),
error: sinon.stub()
}),
'settings-sharelatex': (this.settings = { 'settings-sharelatex': (this.settings = {
apis: { documentupdater: { url: 'http://example.com' } } apis: { documentupdater: { url: 'http://example.com' } }
}) })

View file

@ -10,9 +10,7 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const sinon = require('sinon') const sinon = require('sinon')
const chai = require('chai') const { expect } = require('chai')
const should = chai.should()
const { expect } = chai
const modulePath = '../../../../app/js/HttpController.js' const modulePath = '../../../../app/js/HttpController.js'
const SandboxedModule = require('sandboxed-module') const SandboxedModule = require('sandboxed-module')
@ -21,7 +19,6 @@ describe('HttpController', function () {
this.HttpController = SandboxedModule.require(modulePath, { this.HttpController = SandboxedModule.require(modulePath, {
singleOnly: true, singleOnly: true,
requires: { requires: {
'logger-sharelatex': { log: sinon.stub() },
'./UpdatesManager': (this.UpdatesManager = {}), './UpdatesManager': (this.UpdatesManager = {}),
'./DiffManager': (this.DiffManager = {}), './DiffManager': (this.DiffManager = {}),
'./RestoreManager': (this.RestoreManager = {}), './RestoreManager': (this.RestoreManager = {}),

View file

@ -16,9 +16,7 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const sinon = require('sinon') const sinon = require('sinon')
const chai = require('chai') const { expect } = require('chai')
const should = chai.should()
const { expect } = chai
const modulePath = '../../../../app/js/LockManager.js' const modulePath = '../../../../app/js/LockManager.js'
const SandboxedModule = require('sandboxed-module') const SandboxedModule = require('sandboxed-module')
@ -36,8 +34,7 @@ describe('LockManager', function () {
return (this.rclient = { auth: sinon.stub() }) return (this.rclient = { auth: sinon.stub() })
} }
}, },
'settings-sharelatex': this.Settings, 'settings-sharelatex': this.Settings
'logger-sharelatex': { error() {} }
} }
}) })

View file

@ -10,9 +10,7 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const sinon = require('sinon') const sinon = require('sinon')
const chai = require('chai') const { expect } = require('chai')
const should = chai.should()
const { expect } = chai
const modulePath = '../../../../app/js/MongoManager.js' const modulePath = '../../../../app/js/MongoManager.js'
const packModulePath = '../../../../app/js/PackManager.js' const packModulePath = '../../../../app/js/PackManager.js'
const SandboxedModule = require('sandboxed-module') const SandboxedModule = require('sandboxed-module')
@ -26,8 +24,7 @@ describe('MongoManager', function () {
requires: { requires: {
'./mongodb': { db: (this.db = {}), ObjectId }, './mongodb': { db: (this.db = {}), ObjectId },
'./PackManager': (this.PackManager = {}), './PackManager': (this.PackManager = {}),
'@overleaf/metrics': { timeAsyncMethod() {} }, '@overleaf/metrics': { timeAsyncMethod() {} }
'logger-sharelatex': { log() {} }
} }
}) })
this.callback = sinon.stub() this.callback = sinon.stub()

View file

@ -11,10 +11,7 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const sinon = require('sinon') const sinon = require('sinon')
const chai = require('chai') const { assert, expect } = require('chai')
const { assert } = require('chai')
const should = chai.should()
const { expect } = chai
const modulePath = '../../../../app/js/PackManager.js' const modulePath = '../../../../app/js/PackManager.js'
const SandboxedModule = require('sandboxed-module') const SandboxedModule = require('sandboxed-module')
const { ObjectId } = require('mongodb') const { ObjectId } = require('mongodb')
@ -31,7 +28,6 @@ describe('PackManager', function () {
'./mongodb': { db: (this.db = {}), ObjectId }, './mongodb': { db: (this.db = {}), ObjectId },
'./LockManager': {}, './LockManager': {},
'./MongoAWS': {}, './MongoAWS': {},
'logger-sharelatex': { log: sinon.stub(), error: sinon.stub() },
'@overleaf/metrics': { inc() {} }, '@overleaf/metrics': { inc() {} },
'./ProjectIterator': require('../../../../app/js/ProjectIterator.js'), // Cache for speed './ProjectIterator': require('../../../../app/js/ProjectIterator.js'), // Cache for speed
'settings-sharelatex': { 'settings-sharelatex': {

View file

@ -12,9 +12,7 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const sinon = require('sinon') const sinon = require('sinon')
const chai = require('chai') const { expect } = require('chai')
const should = chai.should()
const { expect } = chai
const modulePath = '../../../../app/js/RedisManager.js' const modulePath = '../../../../app/js/RedisManager.js'
const SandboxedModule = require('sandboxed-module') const SandboxedModule = require('sandboxed-module')

View file

@ -10,9 +10,7 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const sinon = require('sinon') const sinon = require('sinon')
const chai = require('chai') const { expect } = require('chai')
const should = chai.should()
const { expect } = chai
const modulePath = '../../../../app/js/RestoreManager.js' const modulePath = '../../../../app/js/RestoreManager.js'
const SandboxedModule = require('sandboxed-module') const SandboxedModule = require('sandboxed-module')
@ -20,10 +18,6 @@ describe('RestoreManager', function () {
beforeEach(function () { beforeEach(function () {
this.RestoreManager = SandboxedModule.require(modulePath, { this.RestoreManager = SandboxedModule.require(modulePath, {
requires: { requires: {
'logger-sharelatex': (this.logger = {
log: sinon.stub(),
error: sinon.stub()
}),
'./DocumentUpdaterManager': (this.DocumentUpdaterManager = {}), './DocumentUpdaterManager': (this.DocumentUpdaterManager = {}),
'./DiffManager': (this.DiffManager = {}) './DiffManager': (this.DiffManager = {})
} }

View file

@ -10,9 +10,7 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const sinon = require('sinon') const sinon = require('sinon')
const chai = require('chai') const { expect } = require('chai')
const should = chai.should()
const { expect } = chai
const modulePath = '../../../../app/js/UpdateCompressor.js' const modulePath = '../../../../app/js/UpdateCompressor.js'
const SandboxedModule = require('sandboxed-module') const SandboxedModule = require('sandboxed-module')

View file

@ -10,9 +10,7 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const sinon = require('sinon') const sinon = require('sinon')
const chai = require('chai') const { expect } = require('chai')
const should = chai.should()
const { expect } = chai
const modulePath = '../../../../app/js/UpdateTrimmer.js' const modulePath = '../../../../app/js/UpdateTrimmer.js'
const SandboxedModule = require('sandboxed-module') const SandboxedModule = require('sandboxed-module')
const tk = require('timekeeper') const tk = require('timekeeper')
@ -24,10 +22,6 @@ describe('UpdateTrimmer', function () {
this.UpdateTrimmer = SandboxedModule.require(modulePath, { this.UpdateTrimmer = SandboxedModule.require(modulePath, {
requires: { requires: {
'logger-sharelatex': (this.logger = {
log: sinon.stub(),
error: sinon.stub()
}),
'./WebApiManager': (this.WebApiManager = {}), './WebApiManager': (this.WebApiManager = {}),
'./MongoManager': (this.MongoManager = {}) './MongoManager': (this.MongoManager = {})
} }

View file

@ -14,9 +14,7 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const sinon = require('sinon') const sinon = require('sinon')
const chai = require('chai') const { expect } = require('chai')
const should = chai.should()
const { expect } = chai
const { ObjectId } = require('mongodb') const { ObjectId } = require('mongodb')
const modulePath = '../../../../app/js/UpdatesManager.js' const modulePath = '../../../../app/js/UpdatesManager.js'
const SandboxedModule = require('sandboxed-module') const SandboxedModule = require('sandboxed-module')
@ -34,7 +32,6 @@ describe('UpdatesManager', function () {
'./WebApiManager': (this.WebApiManager = {}), './WebApiManager': (this.WebApiManager = {}),
'./UpdateTrimmer': (this.UpdateTrimmer = {}), './UpdateTrimmer': (this.UpdateTrimmer = {}),
'./DocArchiveManager': (this.DocArchiveManager = {}), './DocArchiveManager': (this.DocArchiveManager = {}),
'logger-sharelatex': { log: sinon.stub(), error: sinon.stub() },
'settings-sharelatex': { 'settings-sharelatex': {
redis: { redis: {
lock: { lock: {

View file

@ -10,9 +10,7 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const sinon = require('sinon') const sinon = require('sinon')
const chai = require('chai') const { expect } = require('chai')
const should = chai.should()
const { expect } = chai
const modulePath = '../../../../app/js/WebApiManager.js' const modulePath = '../../../../app/js/WebApiManager.js'
const SandboxedModule = require('sandboxed-module') const SandboxedModule = require('sandboxed-module')
@ -21,10 +19,6 @@ describe('WebApiManager', function () {
this.WebApiManager = SandboxedModule.require(modulePath, { this.WebApiManager = SandboxedModule.require(modulePath, {
requires: { requires: {
requestretry: (this.request = {}), requestretry: (this.request = {}),
'logger-sharelatex': (this.logger = {
log: sinon.stub(),
error: sinon.stub()
}),
'settings-sharelatex': (this.settings = { 'settings-sharelatex': (this.settings = {
apis: { apis: {
web: { web: {