diff --git a/services/docstore/.mocharc.json b/services/docstore/.mocharc.json new file mode 100644 index 0000000000..dc3280aa96 --- /dev/null +++ b/services/docstore/.mocharc.json @@ -0,0 +1,3 @@ +{ + "require": "test/setup.js" +} diff --git a/services/docstore/test/acceptance/js/ArchiveDocsTests.js b/services/docstore/test/acceptance/js/ArchiveDocsTests.js index f8531f23a2..61ed52ad99 100644 --- a/services/docstore/test/acceptance/js/ArchiveDocsTests.js +++ b/services/docstore/test/acceptance/js/ArchiveDocsTests.js @@ -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() }) }) diff --git a/services/docstore/test/acceptance/js/DeletingDocsTests.js b/services/docstore/test/acceptance/js/DeletingDocsTests.js index 232931102e..d481e5795b 100644 --- a/services/docstore/test/acceptance/js/DeletingDocsTests.js +++ b/services/docstore/test/acceptance/js/DeletingDocsTests.js @@ -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') diff --git a/services/docstore/test/acceptance/js/GettingAllDocsTests.js b/services/docstore/test/acceptance/js/GettingAllDocsTests.js index 143052d71d..2d38ca5fba 100644 --- a/services/docstore/test/acceptance/js/GettingAllDocsTests.js +++ b/services/docstore/test/acceptance/js/GettingAllDocsTests.js @@ -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') diff --git a/services/docstore/test/acceptance/js/GettingDocsTests.js b/services/docstore/test/acceptance/js/GettingDocsTests.js index 367115d475..7847abd84a 100644 --- a/services/docstore/test/acceptance/js/GettingDocsTests.js +++ b/services/docstore/test/acceptance/js/GettingDocsTests.js @@ -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') diff --git a/services/docstore/test/acceptance/js/UpdatingDocsTests.js b/services/docstore/test/acceptance/js/UpdatingDocsTests.js index 3c7d0d36f0..45d3cb9239 100644 --- a/services/docstore/test/acceptance/js/UpdatingDocsTests.js +++ b/services/docstore/test/acceptance/js/UpdatingDocsTests.js @@ -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') diff --git a/services/docstore/test/setup.js b/services/docstore/test/setup.js new file mode 100644 index 0000000000..f738f1c70f --- /dev/null +++ b/services/docstore/test/setup.js @@ -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() + } +} diff --git a/services/docstore/test/unit/js/DocArchiveManagerTests.js b/services/docstore/test/unit/js/DocArchiveManagerTests.js index 000afb1660..7d3218226d 100644 --- a/services/docstore/test/unit/js/DocArchiveManagerTests.js +++ b/services/docstore/test/unit/js/DocArchiveManagerTests.js @@ -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 } }) }) diff --git a/services/docstore/test/unit/js/DocManagerTests.js b/services/docstore/test/unit/js/DocManagerTests.js index 50caa69501..624ca641af 100644 --- a/services/docstore/test/unit/js/DocManagerTests.js +++ b/services/docstore/test/unit/js/DocManagerTests.js @@ -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 () { diff --git a/services/docstore/test/unit/js/HttpControllerTests.js b/services/docstore/test/unit/js/HttpControllerTests.js index 4fc3651ada..219d35119c 100644 --- a/services/docstore/test/unit/js/HttpControllerTests.js +++ b/services/docstore/test/unit/js/HttpControllerTests.js @@ -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(), diff --git a/services/docstore/test/unit/js/MongoManagerTests.js b/services/docstore/test/unit/js/MongoManagerTests.js index 0b073937f0..2e23087d19 100644 --- a/services/docstore/test/unit/js/MongoManagerTests.js +++ b/services/docstore/test/unit/js/MongoManagerTests.js @@ -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() diff --git a/services/docstore/test/unit/js/RangeManagerTests.js b/services/docstore/test/unit/js/RangeManagerTests.js index 471f60e2c8..afea8f04c6 100644 --- a/services/docstore/test/unit/js/RangeManagerTests.js +++ b/services/docstore/test/unit/js/RangeManagerTests.js @@ -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 () {