Add a global test setup file

Configure chai and SandboxedModule globally instead of in every test
file. Also add globals that are required for SandboxedModule to work in
Node 12.
This commit is contained in:
Eric Mc Sween 2021-03-12 17:16:01 -05:00
parent 5d4105dcda
commit 5efae3bee6
12 changed files with 71 additions and 79 deletions

View file

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

View file

@ -14,9 +14,7 @@
*/
process.env.BACKEND = 'gcs'
const Settings = require('settings-sharelatex')
const chai = require('chai')
const { expect } = chai
const should = chai.should()
const { expect } = require('chai')
const { db, ObjectId } = require('../../../app/js/mongodb')
const async = require('async')
const DocstoreApp = require('./helpers/DocstoreApp')
@ -96,8 +94,8 @@ describe('Archiving', function () {
((doc) => {
return (callback) => {
return db.docs.findOne({ _id: doc._id }, (error, doc) => {
should.not.exist(doc.lines)
should.not.exist(doc.ranges)
expect(doc.lines).not.to.exist
expect(doc.ranges).not.to.exist
doc.inS3.should.equal(true)
return callback()
})
@ -155,7 +153,7 @@ describe('Archiving', function () {
return db.docs.findOne({ _id: doc._id }, (error, doc) => {
doc.lines.should.deep.equal(this.docs[i].lines)
doc.ranges.should.deep.equal(this.docs[i].ranges)
should.not.exist(doc.inS3)
expect(doc.inS3).not.to.exist
return callback()
})
}
@ -218,8 +216,8 @@ describe('Archiving', function () {
if (error != null) {
throw error
}
should.not.exist(doc.lines)
should.not.exist(doc.ranges)
expect(doc.lines).not.to.exist
expect(doc.ranges).not.to.exist
doc.inS3.should.equal(true)
doc.deleted.should.equal(true)
return done()
@ -267,7 +265,7 @@ describe('Archiving', function () {
}
doc.lines.should.deep.equal(this.doc.lines)
doc.ranges.should.deep.equal(this.doc.ranges)
should.not.exist(doc.inS3)
expect(doc.inS3).not.to.exist
doc.deleted.should.equal(true)
return done()
})
@ -365,8 +363,8 @@ describe('Archiving', function () {
if (error) {
return done(error)
}
should.not.exist(doc.lines)
should.not.exist(doc.ranges)
expect(doc.lines).not.to.exist
expect(doc.ranges).not.to.exist
doc.inS3.should.equal(true)
done()
})
@ -433,8 +431,8 @@ describe('Archiving', function () {
if (error != null) {
throw error
}
should.not.exist(doc.lines)
should.not.exist(doc.ranges)
expect(doc.lines).not.to.exist
expect(doc.ranges).not.to.exist
doc.inS3.should.equal(true)
return done()
})
@ -476,7 +474,7 @@ describe('Archiving', function () {
}
doc.lines.should.deep.equal(this.doc.lines)
doc.ranges.should.deep.equal(this.doc.ranges)
should.not.exist(doc.inS3)
expect(doc.inS3).not.to.exist
return done()
})
})
@ -907,8 +905,8 @@ describe('Archiving', function () {
if (error != null) {
throw error
}
should.not.exist(doc.lines)
should.not.exist(doc.ranges)
expect(doc.lines).not.to.exist
expect(doc.ranges).not.to.exist
doc.inS3.should.equal(true)
return done()
})
@ -950,7 +948,7 @@ describe('Archiving', function () {
}
doc.lines.should.deep.equal(this.doc.lines)
doc.ranges.should.deep.equal(this.doc.ranges)
should.not.exist(doc.inS3)
expect(doc.inS3).not.to.exist
return done()
})
})
@ -1026,8 +1024,8 @@ describe('Archiving', function () {
if (error != null) {
throw error
}
should.not.exist(doc.lines)
should.not.exist(doc.ranges)
expect(doc.lines).not.to.exist
expect(doc.ranges).not.to.exist
doc.inS3.should.equal(true)
return done()
})
@ -1070,7 +1068,7 @@ describe('Archiving', function () {
}
doc.lines.should.deep.equal(this.doc.lines)
doc.ranges.should.deep.equal(this.doc.ranges)
should.not.exist(doc.inS3)
expect(doc.inS3).not.to.exist
return done()
})
})
@ -1123,8 +1121,8 @@ describe('Archiving', function () {
if (error != null) {
throw error
}
should.not.exist(doc.lines)
should.not.exist(doc.ranges)
expect(doc.lines).not.to.exist
expect(doc.ranges).not.to.exist
doc.inS3.should.equal(true)
return done()
})
@ -1166,7 +1164,7 @@ describe('Archiving', function () {
}
doc.lines.should.deep.equal(this.doc.lines)
doc.ranges.should.deep.equal(this.doc.ranges)
should.not.exist(doc.inS3)
expect(doc.inS3).not.to.exist
return done()
})
})
@ -1220,7 +1218,7 @@ describe('Archiving', function () {
throw error
}
doc.lines.should.deep.equal(this.doc.lines)
should.not.exist(doc.inS3)
expect(doc.inS3).not.to.exist
return done()
})
})

View file

@ -11,10 +11,8 @@
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const chai = require('chai')
chai.should()
const { db, ObjectId } = require('../../../app/js/mongodb')
const { expect } = chai
const { expect } = require('chai')
const DocstoreApp = require('./helpers/DocstoreApp')
const Errors = require('../../../app/js/Errors')
const Settings = require('settings-sharelatex')

View file

@ -12,8 +12,6 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const sinon = require('sinon')
const chai = require('chai')
chai.should()
const { ObjectId } = require('mongodb')
const async = require('async')
const DocstoreApp = require('./helpers/DocstoreApp')

View file

@ -12,8 +12,6 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const sinon = require('sinon')
const chai = require('chai')
chai.should()
const { ObjectId } = require('mongodb')
const DocstoreApp = require('./helpers/DocstoreApp')

View file

@ -11,8 +11,6 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const sinon = require('sinon')
const chai = require('chai')
chai.should()
const { ObjectId } = require('mongodb')
const DocstoreApp = require('./helpers/DocstoreApp')

View file

@ -0,0 +1,40 @@
const chai = require('chai')
const sinon = require('sinon')
const sinonChai = require('sinon-chai')
const chaiAsPromised = require('chai-as-promised')
const SandboxedModule = require('sandboxed-module')
// Chai configuration
chai.should()
chai.use(sinonChai)
chai.use(chaiAsPromised)
// Global stubs
const sandbox = sinon.createSandbox()
const stubs = {
logger: {
log: sandbox.stub(),
warn: sandbox.stub(),
err: sandbox.stub(),
error: sandbox.stub(),
fatal: sandbox.stub()
}
}
// SandboxedModule configuration
SandboxedModule.configure({
requires: {
'logger-sharelatex': stubs.logger
},
globals: { Buffer, JSON, console, process }
})
exports.mochaHooks = {
beforeEach() {
this.logger = stubs.logger
},
afterEach() {
sandbox.reset()
}
}

View file

@ -1,21 +1,16 @@
const sinon = require('sinon')
const chai = require('chai')
const { expect } = chai
const { expect } = require('chai')
const modulePath = '../../../app/js/DocArchiveManager.js'
const SandboxedModule = require('sandboxed-module')
const { ObjectId } = require('mongodb')
const Errors = require('../../../app/js/Errors')
chai.use(require('chai-as-promised'))
chai.use(require('sinon-chai'))
describe('DocArchiveManager', function () {
let DocArchiveManager,
PersistorManager,
MongoManager,
RangeManager,
Settings,
Logger,
Crypto,
Streamifier,
HashDigest,
@ -39,10 +34,6 @@ describe('DocArchiveManager', function () {
bucket: 'wombat'
}
}
Logger = {
log: sinon.stub(),
err: sinon.stub()
}
HashDigest = sinon.stub().returns(md5Sum)
HashUpdate = sinon.stub().returns({ digest: HashDigest })
Crypto = {
@ -141,17 +132,12 @@ describe('DocArchiveManager', function () {
DocArchiveManager = SandboxedModule.require(modulePath, {
requires: {
'settings-sharelatex': Settings,
'logger-sharelatex': Logger,
crypto: Crypto,
streamifier: Streamifier,
'./MongoManager': MongoManager,
'./RangeManager': RangeManager,
'./PersistorManager': PersistorManager,
'./Errors': Errors
},
globals: {
console,
JSON
}
})
})

View file

@ -14,11 +14,7 @@
*/
const SandboxedModule = require('sandboxed-module')
const sinon = require('sinon')
const chai = require('chai')
chai.use(require('sinon-chai'))
const { assert } = require('chai')
chai.should()
const { expect } = chai
const { assert, expect } = require('chai')
const modulePath = require('path').join(__dirname, '../../../app/js/DocManager')
const { ObjectId } = require('mongodb')
const Errors = require('../../../app/js/Errors')
@ -36,11 +32,6 @@ describe('DocManager', function () {
shouldUpdateRanges: sinon.stub().returns(false)
}),
'settings-sharelatex': (this.settings = { docstore: {} }),
'logger-sharelatex': (this.logger = {
log: sinon.stub(),
warn() {},
err() {}
}),
'./Errors': Errors
}
})
@ -493,7 +484,6 @@ describe('DocManager', function () {
this.DocArchiveManager.archiveDocById = sinon
.stub()
.yields(this.err)
this.logger.warn = sinon.stub()
this.callback = sinon.stub().callsFake(done)
this.DocManager.deleteDoc(
this.project_id,
@ -630,7 +620,6 @@ describe('DocManager', function () {
beforeEach(function () {
this.settings.docstore.archiveOnSoftDelete = true
this.meta.deleted = true
this.logger.warn = sinon.stub()
})
describe('when the background flush succeeds', function () {

View file

@ -10,11 +10,8 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const SandboxedModule = require('sandboxed-module')
const { assert } = require('chai')
const sinon = require('sinon')
const chai = require('chai')
chai.should()
const { expect } = chai
const { assert, expect } = require('chai')
const modulePath = require('path').join(
__dirname,
'../../../app/js/HttpController'
@ -30,15 +27,9 @@ describe('HttpController', function () {
requires: {
'./DocManager': (this.DocManager = {}),
'./DocArchiveManager': (this.DocArchiveManager = {}),
'logger-sharelatex': (this.logger = {
log: sinon.stub(),
error: sinon.stub(),
fatal: sinon.stub()
}),
'settings-sharelatex': settings,
'./HealthChecker': {}
},
globals: { process }
}
})
this.res = {
send: sinon.stub(),

View file

@ -11,7 +11,6 @@
*/
const SandboxedModule = require('sandboxed-module')
const sinon = require('sinon')
require('chai').should()
const modulePath = require('path').join(
__dirname,
'../../../app/js/MongoManager'
@ -28,11 +27,7 @@ describe('MongoManager', function () {
ObjectId
},
'@overleaf/metrics': { timeAsyncMethod: sinon.stub() },
'logger-sharelatex': { log() {} },
'settings-sharelatex': { max_deleted_docs: 42 }
},
globals: {
console
}
})
this.project_id = ObjectId().toString()

View file

@ -12,14 +12,12 @@
*/
const SandboxedModule = require('sandboxed-module')
const sinon = require('sinon')
require('chai').should()
const { expect } = require('chai')
const { assert, expect } = require('chai')
const modulePath = require('path').join(
__dirname,
'../../../app/js/RangeManager'
)
const { ObjectId } = require('mongodb')
const { assert } = require('chai')
const _ = require('underscore')
describe('RangeManager', function () {