Merge pull request #17362 from overleaf/bg-chai-object-id-tests

fix for chai object id tests

GitOrigin-RevId: 98123dbc930c6ee57be3a118177426120482c5f4
This commit is contained in:
Brian Gough
2024-07-16 08:04:46 +00:00
committed by Copybot
parent 3083d8082f
commit 9babc70df7
24 changed files with 85 additions and 499 deletions
+3
View File
@@ -0,0 +1,3 @@
{
"require": "test/setup.js"
}
+9
View File
@@ -0,0 +1,9 @@
import chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
import { ObjectId } from 'mongodb'
// ensure every ObjectId has the id string as a property for correct comparisons
ObjectId.cacheHexString = true
chai.should()
chai.use(chaiAsPromised)
+4
View File
@@ -1,6 +1,10 @@
import chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
import sinonChai from 'sinon-chai'
import { ObjectId } from 'mongodb'
// ensure every ObjectId has the id string as a property for correct comparisons
ObjectId.cacheHexString = true
chai.should()
chai.use(chaiAsPromised)
-1
View File
@@ -31,7 +31,6 @@
"celebrate": "^15.0.3",
"express": "^4.19.2",
"lodash": "^4.17.21",
"mongodb": "^6.2.0",
"mongodb-legacy": "^6.0.1",
"p-map": "^4.0.0",
"request": "^2.88.2"
@@ -11,7 +11,7 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const sinon = require('sinon')
const { ObjectId } = require('mongodb')
const { ObjectId } = require('mongodb-legacy')
const async = require('async')
const DocstoreApp = require('./helpers/DocstoreApp')
@@ -10,7 +10,7 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const sinon = require('sinon')
const { ObjectId } = require('mongodb')
const { ObjectId } = require('mongodb-legacy')
const DocstoreApp = require('./helpers/DocstoreApp')
const DocstoreClient = require('./helpers/DocstoreClient')
@@ -10,7 +10,7 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const sinon = require('sinon')
const { ObjectId } = require('mongodb')
const { ObjectId } = require('mongodb-legacy')
const DocstoreApp = require('./helpers/DocstoreApp')
const DocstoreClient = require('./helpers/DocstoreClient')
+5
View File
@@ -5,6 +5,9 @@ const chaiAsPromised = require('chai-as-promised')
const SandboxedModule = require('sandboxed-module')
const timersPromises = require('timers/promises')
// ensure every ObjectId has the id string as a property for correct comparisons
require('mongodb-legacy').ObjectId.cacheHexString = true
process.env.BACKEND = 'gcs'
// Chai configuration
@@ -31,6 +34,8 @@ SandboxedModule.configure({
requires: {
'@overleaf/logger': stubs.logger,
'timers/promises': timersPromises,
'mongodb-legacy': require('mongodb-legacy'),
bson: require('bson'),
},
globals: { Buffer, JSON, Math, console, process },
})
@@ -2,7 +2,7 @@ const sinon = require('sinon')
const { expect } = require('chai')
const modulePath = '../../../app/js/DocArchiveManager.js'
const SandboxedModule = require('sandboxed-module')
const { ObjectId } = require('mongodb')
const { ObjectId } = require('mongodb-legacy')
const Errors = require('../../../app/js/Errors')
describe('DocArchiveManager', function () {
@@ -2,7 +2,7 @@ const SandboxedModule = require('sandboxed-module')
const sinon = require('sinon')
const { expect } = require('chai')
const modulePath = require('path').join(__dirname, '../../../app/js/DocManager')
const { ObjectId } = require('mongodb')
const { ObjectId } = require('mongodb-legacy')
const Errors = require('../../../app/js/Errors')
describe('DocManager', function () {
@@ -5,7 +5,7 @@ const modulePath = require('path').join(
__dirname,
'../../../app/js/HttpController'
)
const { ObjectId } = require('mongodb')
const { ObjectId } = require('mongodb-legacy')
const Errors = require('../../../app/js/Errors')
describe('HttpController', function () {
@@ -4,7 +4,7 @@ const modulePath = require('path').join(
__dirname,
'../../../app/js/MongoManager'
)
const { ObjectId } = require('mongodb')
const { ObjectId } = require('mongodb-legacy')
const { assert, expect } = require('chai')
const Errors = require('../../../app/js/Errors')
@@ -16,7 +16,7 @@ const modulePath = require('path').join(
__dirname,
'../../../app/js/RangeManager'
)
const { ObjectId } = require('mongodb')
const { ObjectId } = require('mongodb-legacy')
describe('RangeManager', function () {
beforeEach(function () {
-1
View File
@@ -32,7 +32,6 @@
"diff-match-patch": "overleaf/diff-match-patch#89805f9c671a77a263fc53461acd62aa7498f688",
"express": "^4.19.2",
"lodash": "^4.17.21",
"mongodb": "^6.2.0",
"mongodb-legacy": "^6.0.1",
"request": "^2.88.2",
"requestretry": "^7.1.0"
+4
View File
@@ -4,6 +4,9 @@ const sinonChai = require('sinon-chai')
const SandboxedModule = require('sandboxed-module')
const sinon = require('sinon')
// ensure every ObjectId has the id string as a property for correct comparisons
require('mongodb-legacy').ObjectId.cacheHexString = true
// Chai configuration
chai.should()
chai.use(chaiAsPromised)
@@ -27,6 +30,7 @@ const stubs = {
SandboxedModule.configure({
requires: {
'@overleaf/logger': stubs.logger,
'mongodb-legacy': require('mongodb-legacy'), // for ObjectId comparisons
},
globals: { Buffer, JSON, Math, console, process },
})
+3
View File
@@ -0,0 +1,3 @@
{
"require": "test/setup.js"
}
+34
View File
@@ -0,0 +1,34 @@
const sinon = require('sinon')
const SandboxedModule = require('sandboxed-module')
// ensure every ObjectId has the id string as a property for correct comparisons
require('mongodb').ObjectId.cacheHexString = true
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(),
fatal: sandbox.stub(),
},
}
SandboxedModule.configure({
requires: {
'@overleaf/logger': stubs.logger,
},
})
exports.mochaHooks = {
beforeEach() {
this.logger = stubs.logger
},
afterEach() {
sandbox.reset()
},
}
@@ -6,20 +6,16 @@ const { FailedCommandError } = require('../../../app/js/Errors')
const SandboxedModule = require('sandboxed-module')
describe('ImageOptimiser', function () {
let ImageOptimiser, SafeExec, logger
let ImageOptimiser, SafeExec
const sourcePath = '/wombat/potato.eps'
beforeEach(function () {
SafeExec = {
promises: sinon.stub().resolves(),
}
logger = {
warn: sinon.stub(),
}
ImageOptimiser = SandboxedModule.require(modulePath, {
requires: {
'./SafeExec': SafeExec,
'@overleaf/logger': logger,
'@overleaf/metrics': {
Timer: sinon.stub().returns({ done: sinon.stub() }),
},
@@ -65,7 +61,7 @@ describe('ImageOptimiser', function () {
})
it('should log a warning', function () {
expect(logger.warn).to.have.been.calledOnce
expect(this.logger.warn).to.have.been.calledOnce
})
})
})
+3
View File
@@ -4,6 +4,9 @@ const config = require('config')
const fetch = require('node-fetch')
const { knex, mongodb } = require('../storage')
// ensure every ObjectId has the id string as a property for correct comparisons
require('mongodb').ObjectId.cacheHexString = true
chai.use(chaiAsPromised)
async function setupPostgresDatabase() {
-1
View File
@@ -27,7 +27,6 @@
"bunyan": "^1.8.15",
"express": "^4.19.2",
"method-override": "^3.0.0",
"mongodb": "^6.1.0",
"mongodb-legacy": "^6.0.1",
"request": "^2.88.2"
},
+4
View File
@@ -4,6 +4,9 @@ const SandboxedModule = require('sandboxed-module')
// Chai configuration
chai.should()
// ensure every ObjectId has the id string as a property for correct comparisons
require('mongodb-legacy').ObjectId.cacheHexString = true
// SandboxedModule configuration
SandboxedModule.configure({
requires: {
@@ -16,6 +19,7 @@ SandboxedModule.configure({
error() {},
fatal() {},
},
'mongodb-legacy': require('mongodb-legacy'), // for ObjectId comparisons
},
globals: { Buffer, JSON, console, process },
})
@@ -15,7 +15,7 @@ const { expect } = require('chai')
const modulePath = '../../../app/js/Notifications.js'
const SandboxedModule = require('sandboxed-module')
const assert = require('assert')
const { ObjectId } = require('mongodb')
const { ObjectId } = require('mongodb-legacy')
const userId = '51dc93e6fb625a261300003b'
const notificationId = '574ee8d6f40c3a244e704249'
+5
View File
@@ -1,6 +1,11 @@
import chai from 'chai'
import sinonChai from 'sinon-chai'
import chaiAsPromised from 'chai-as-promised'
import mongodb from 'mongodb-legacy'
const { ObjectId } = mongodb
// ensure every ObjectId has the id string as a property for correct comparisons
ObjectId.cacheHexString = true
// Chai configuration
chai.should()