Merge pull request #27936 from overleaf/em-unit-tests-mongo

Make Mongo available to unit tests in all services

GitOrigin-RevId: b65bbb69883d5bba31d09802b89f35bdc523fe4d
This commit is contained in:
Eric Mc Sween
2025-08-25 08:05:19 +00:00
committed by Copybot
parent 7dc7b322a8
commit 8f0913fafe
57 changed files with 404 additions and 162 deletions
+30
View File
@@ -1,3 +1,5 @@
// @ts-check
const crypto = require('node:crypto')
const os = require('node:os')
const { promisify } = require('node:util')
@@ -168,6 +170,34 @@ async function runWithTimeout({ runner, timeout, context }) {
])
}
/**
* Delete all data from the test Redis instance
*
* @param {Redis} rclient
*/
async function cleanupTestRedis(rclient) {
ensureTestRedis(rclient)
await rclient.flushall()
}
/**
* Checks that the Redis client points to a test database
*
* In tests, the Redis instance is on a host called redis_test
*
* @param {Redis} rclient
*/
function ensureTestRedis(rclient) {
const host = rclient.options.host
const env = process.env.NODE_ENV
if (host !== 'redis_test' || env !== 'test') {
throw new Error(
`Refusing to clear Redis instance '${host}' in environment '${env}'`
)
}
}
module.exports = {
createClient,
cleanupTestRedis,
}