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

View file

@ -11,10 +11,8 @@
* DS207: Consider shorter variations of null checks * DS207: Consider shorter variations of null checks
* 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 { db, ObjectId } = require('../../../app/js/mongodb') const { db, ObjectId } = require('../../../app/js/mongodb')
const { expect } = chai const { expect } = require('chai')
const DocstoreApp = require('./helpers/DocstoreApp') const DocstoreApp = require('./helpers/DocstoreApp')
const Errors = require('../../../app/js/Errors') const Errors = require('../../../app/js/Errors')
const Settings = require('settings-sharelatex') const Settings = require('settings-sharelatex')

View file

@ -12,8 +12,6 @@
* 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')
chai.should()
const { ObjectId } = require('mongodb') const { ObjectId } = require('mongodb')
const async = require('async') const async = require('async')
const DocstoreApp = require('./helpers/DocstoreApp') const DocstoreApp = require('./helpers/DocstoreApp')

View file

@ -12,8 +12,6 @@
* 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')
chai.should()
const { ObjectId } = require('mongodb') const { ObjectId } = require('mongodb')
const DocstoreApp = require('./helpers/DocstoreApp') const DocstoreApp = require('./helpers/DocstoreApp')

View file

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

View file

@ -14,11 +14,7 @@
*/ */
const SandboxedModule = require('sandboxed-module') const SandboxedModule = require('sandboxed-module')
const sinon = require('sinon') const sinon = require('sinon')
const chai = require('chai') const { assert, expect } = require('chai')
chai.use(require('sinon-chai'))
const { assert } = require('chai')
chai.should()
const { expect } = chai
const modulePath = require('path').join(__dirname, '../../../app/js/DocManager') const modulePath = require('path').join(__dirname, '../../../app/js/DocManager')
const { ObjectId } = require('mongodb') const { ObjectId } = require('mongodb')
const Errors = require('../../../app/js/Errors') const Errors = require('../../../app/js/Errors')
@ -36,11 +32,6 @@ describe('DocManager', function () {
shouldUpdateRanges: sinon.stub().returns(false) shouldUpdateRanges: sinon.stub().returns(false)
}), }),
'settings-sharelatex': (this.settings = { docstore: {} }), 'settings-sharelatex': (this.settings = { docstore: {} }),
'logger-sharelatex': (this.logger = {
log: sinon.stub(),
warn() {},
err() {}
}),
'./Errors': Errors './Errors': Errors
} }
}) })
@ -493,7 +484,6 @@ describe('DocManager', function () {
this.DocArchiveManager.archiveDocById = sinon this.DocArchiveManager.archiveDocById = sinon
.stub() .stub()
.yields(this.err) .yields(this.err)
this.logger.warn = sinon.stub()
this.callback = sinon.stub().callsFake(done) this.callback = sinon.stub().callsFake(done)
this.DocManager.deleteDoc( this.DocManager.deleteDoc(
this.project_id, this.project_id,
@ -630,7 +620,6 @@ describe('DocManager', function () {
beforeEach(function () { beforeEach(function () {
this.settings.docstore.archiveOnSoftDelete = true this.settings.docstore.archiveOnSoftDelete = true
this.meta.deleted = true this.meta.deleted = true
this.logger.warn = sinon.stub()
}) })
describe('when the background flush succeeds', function () { 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 * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const SandboxedModule = require('sandboxed-module') const SandboxedModule = require('sandboxed-module')
const { assert } = require('chai')
const sinon = require('sinon') const sinon = require('sinon')
const chai = require('chai') const { assert, expect } = require('chai')
chai.should()
const { expect } = chai
const modulePath = require('path').join( const modulePath = require('path').join(
__dirname, __dirname,
'../../../app/js/HttpController' '../../../app/js/HttpController'
@ -30,15 +27,9 @@ describe('HttpController', function () {
requires: { requires: {
'./DocManager': (this.DocManager = {}), './DocManager': (this.DocManager = {}),
'./DocArchiveManager': (this.DocArchiveManager = {}), './DocArchiveManager': (this.DocArchiveManager = {}),
'logger-sharelatex': (this.logger = {
log: sinon.stub(),
error: sinon.stub(),
fatal: sinon.stub()
}),
'settings-sharelatex': settings, 'settings-sharelatex': settings,
'./HealthChecker': {} './HealthChecker': {}
}, }
globals: { process }
}) })
this.res = { this.res = {
send: sinon.stub(), send: sinon.stub(),

View file

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

View file

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