Add global test setup

Configure chai and SandboxedModule in a central place. Configure
SandboxedModule globals that are needed in Node 12.
This commit is contained in:
Eric Mc Sween 2021-03-18 16:19:31 -04:00
parent aa69ac13c4
commit b9e7cbf73b
24 changed files with 757 additions and 304 deletions

View file

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

File diff suppressed because it is too large Load diff

View file

@ -54,11 +54,11 @@
"eslint-plugin-prettier": "^3.1.2",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"mocha": "^4.0.1",
"mocha": "^8.3.2",
"prettier": "^2.0.0",
"prettier-eslint-cli": "^5.0.0",
"sandboxed-module": "~0.3.0",
"sinon": "^2.4.1",
"sinon": "^9.2.4",
"timekeeper": "0.0.4",
"uid-safe": "^2.1.5"
}

View file

@ -13,9 +13,7 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const async = require('async')
const chai = require('chai')
const { expect } = chai
chai.should()
const { expect } = require('chai')
const RealTimeClient = require('./helpers/RealTimeClient')
const FixturesManager = require('./helpers/FixturesManager')

View file

@ -12,9 +12,7 @@
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const chai = require('chai')
const { expect } = chai
chai.should()
const { expect } = require('chai')
const RealTimeClient = require('./helpers/RealTimeClient')
const MockWebServer = require('./helpers/MockWebServer')

View file

@ -11,9 +11,7 @@
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const chai = require('chai')
const { expect } = chai
chai.should()
const { expect } = require('chai')
const RealTimeClient = require('./helpers/RealTimeClient')
const MockDocUpdaterServer = require('./helpers/MockDocUpdaterServer')

View file

@ -10,9 +10,7 @@
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const chai = require('chai')
const { expect } = chai
chai.should()
const { expect } = require('chai')
const RealTimeClient = require('./helpers/RealTimeClient')
const MockWebServer = require('./helpers/MockWebServer')

View file

@ -13,9 +13,7 @@
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const chai = require('chai')
const { expect } = chai
chai.should()
const { expect } = require('chai')
const sinon = require('sinon')
const RealTimeClient = require('./helpers/RealTimeClient')

View file

@ -47,8 +47,7 @@ There is additional meta-data that UserItems and SessionItems may use to skip
/* eslint-disable
camelcase,
*/
const chai = require('chai')
const { expect } = chai
const { expect } = require('chai')
const async = require('async')
const RealTimeClient = require('./helpers/RealTimeClient')

View file

@ -11,9 +11,7 @@
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const chai = require('chai')
const { expect } = chai
chai.should()
const { expect } = require('chai')
const RealTimeClient = require('./helpers/RealTimeClient')
const MockWebServer = require('./helpers/MockWebServer')

View file

@ -11,8 +11,7 @@
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const chai = require('chai')
const { expect } = chai
const { expect } = require('chai')
const RealTimeClient = require('./helpers/RealTimeClient')

View file

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

View file

@ -9,9 +9,7 @@
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const chai = require('chai')
chai.should()
const { expect } = chai
const { expect } = require('chai')
const sinon = require('sinon')
const SandboxedModule = require('sandboxed-module')
const path = require('path')

View file

@ -9,9 +9,7 @@
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const chai = require('chai')
const should = chai.should()
const { expect } = chai
const { expect } = require('chai')
const sinon = require('sinon')
const modulePath = '../../../app/js/ChannelManager.js'
const SandboxedModule = require('sandboxed-module')
@ -26,11 +24,6 @@ describe('ChannelManager', function () {
'@overleaf/metrics': (this.metrics = {
inc: sinon.stub(),
summary: sinon.stub()
}),
'logger-sharelatex': (this.logger = {
log: sinon.stub(),
warn: sinon.stub(),
error: sinon.stub()
})
}
}))

View file

@ -12,7 +12,6 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const should = require('chai').should()
const SandboxedModule = require('sandboxed-module')
const assert = require('assert')
const path = require('path')
@ -58,7 +57,6 @@ describe('ConnectedUsersManager', function () {
this.ConnectedUsersManager = SandboxedModule.require(modulePath, {
requires: {
'settings-sharelatex': this.settings,
'logger-sharelatex': { log() {} },
'@overleaf/redis-wrapper': {
createClient: () => {
return this.rClient

View file

@ -12,7 +12,6 @@
*/
const SandboxedModule = require('sandboxed-module')
const sinon = require('sinon')
require('chai').should()
const modulePath = require('path').join(
__dirname,
'../../../app/js/DocumentUpdaterController'
@ -29,11 +28,6 @@ describe('DocumentUpdaterController', function () {
this.RoomEvents = { on: sinon.stub() }
this.EditorUpdatesController = SandboxedModule.require(modulePath, {
requires: {
'logger-sharelatex': (this.logger = {
error: sinon.stub(),
log: sinon.stub(),
warn: sinon.stub()
}),
'settings-sharelatex': (this.settings = {
redis: {
documentupdater: {

View file

@ -10,7 +10,6 @@
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
require('chai').should()
const sinon = require('sinon')
const SandboxedModule = require('sandboxed-module')
const path = require('path')
@ -43,11 +42,6 @@ describe('DocumentUpdaterManager', function () {
return (this.DocumentUpdaterManager = SandboxedModule.require(modulePath, {
requires: {
'settings-sharelatex': this.settings,
'logger-sharelatex': (this.logger = {
log: sinon.stub(),
error: sinon.stub(),
warn: sinon.stub()
}),
request: (this.request = {}),
'@overleaf/redis-wrapper': { createClient: () => this.rclient },
'@overleaf/metrics': (this.Metrics = {
@ -56,9 +50,6 @@ describe('DocumentUpdaterManager', function () {
done() {}
})
})
},
globals: {
JSON: (this.JSON = Object.create(JSON))
}
}))
}) // avoid modifying JSON object directly
@ -325,7 +316,9 @@ describe('DocumentUpdaterManager', function () {
describe('with null byte corruption', function () {
beforeEach(function () {
this.JSON.stringify = () => '["bad bytes! \u0000 <- here"]'
this.stringifyStub = sinon
.stub(JSON, 'stringify')
.callsFake(() => '["bad bytes! \u0000 <- here"]')
return this.DocumentUpdaterManager.queueChange(
this.project_id,
this.doc_id,
@ -334,6 +327,10 @@ describe('DocumentUpdaterManager', function () {
)
})
afterEach(function () {
this.stringifyStub.restore()
})
it('should return an error', function () {
return this.callback
.calledWithExactly(sinon.match(Error))

View file

@ -9,7 +9,6 @@
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const should = require('chai').should()
const sinon = require('sinon')
const SandboxedModule = require('sandboxed-module')
const path = require('path')
@ -17,11 +16,7 @@ const modulePath = path.join(__dirname, '../../../app/js/DrainManager')
describe('DrainManager', function () {
beforeEach(function () {
this.DrainManager = SandboxedModule.require(modulePath, {
requires: {
'logger-sharelatex': (this.logger = { log: sinon.stub() })
}
})
this.DrainManager = SandboxedModule.require(modulePath, {})
return (this.io = {
sockets: {
clients: sinon.stub()

View file

@ -8,7 +8,6 @@
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
require('chai').should()
const { expect } = require('chai')
const SandboxedModule = require('sandboxed-module')
const modulePath = '../../../app/js/EventLogger'
@ -21,10 +20,6 @@ describe('EventLogger', function () {
tk.freeze(new Date(this.start))
this.EventLogger = SandboxedModule.require(modulePath, {
requires: {
'logger-sharelatex': (this.logger = {
error: sinon.stub(),
warn: sinon.stub()
}),
'@overleaf/metrics': (this.metrics = { inc: sinon.stub() })
}
})

View file

@ -10,9 +10,7 @@
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const chai = require('chai')
const { expect } = chai
const should = chai.should()
const { expect } = require('chai')
const sinon = require('sinon')
const modulePath = '../../../app/js/RoomManager.js'
const SandboxedModule = require('sandboxed-module')
@ -26,11 +24,6 @@ describe('RoomManager', function () {
this.RoomManager = SandboxedModule.require(modulePath, {
requires: {
'settings-sharelatex': (this.settings = {}),
'logger-sharelatex': (this.logger = {
log: sinon.stub(),
warn: sinon.stub(),
error: sinon.stub()
}),
'@overleaf/metrics': (this.metrics = { gauge: sinon.stub() })
}
})

View file

@ -11,11 +11,9 @@
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
require('chai').should()
const { expect } = require('chai')
const SandboxedModule = require('sandboxed-module')
const modulePath = '../../../app/js/SafeJsonParse'
const sinon = require('sinon')
describe('SafeJsonParse', function () {
beforeEach(function () {
@ -23,8 +21,7 @@ describe('SafeJsonParse', function () {
requires: {
'settings-sharelatex': (this.Settings = {
maxUpdateSize: 16 * 1024
}),
'logger-sharelatex': (this.logger = { error: sinon.stub() })
})
}
}))
})

View file

@ -9,8 +9,6 @@
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const chai = require('chai')
const should = chai.should()
const sinon = require('sinon')
const modulePath = '../../../app/js/WebApiManager.js'
const SandboxedModule = require('sandboxed-module')
@ -33,10 +31,6 @@ describe('WebApiManager', function () {
pass: 'password'
}
}
}),
'logger-sharelatex': (this.logger = {
log: sinon.stub(),
error: sinon.stub()
})
}
}))

View file

@ -12,10 +12,8 @@
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const chai = require('chai')
const should = chai.should()
const sinon = require('sinon')
const { expect } = chai
const { expect } = require('chai')
const modulePath = '../../../app/js/WebsocketController.js'
const SandboxedModule = require('sandboxed-module')
const tk = require('timekeeper')
@ -50,11 +48,6 @@ describe('WebsocketController', function () {
'./DocumentUpdaterManager': (this.DocumentUpdaterManager = {}),
'./ConnectedUsersManager': (this.ConnectedUsersManager = {}),
'./WebsocketLoadBalancer': (this.WebsocketLoadBalancer = {}),
'logger-sharelatex': (this.logger = {
log: sinon.stub(),
error: sinon.stub(),
warn: sinon.stub()
}),
'@overleaf/metrics': (this.metrics = {
inc: sinon.stub(),
set: 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/WebsocketLoadBalancer'
@ -26,10 +25,6 @@ describe('WebsocketLoadBalancer', function () {
'./RedisClientManager': {
createClientList: () => []
},
'logger-sharelatex': (this.logger = {
log: sinon.stub(),
error: sinon.stub()
}),
'./SafeJsonParse': (this.SafeJsonParse = {
parse: (data, cb) => cb(null, JSON.parse(data))
}),