test: inform devs when db connection not established (#60539)

This commit is contained in:
Oliver Eyton-Williams
2025-05-28 17:52:11 +02:00
committed by GitHub
parent 5cb96cf3e0
commit 22724d5dda
3 changed files with 19 additions and 1 deletions
+15
View File
@@ -155,6 +155,19 @@ const indexData: IndexData[] = [
}
];
export async function checkCanConnectToDb(
prisma: FastifyTestInstance['prisma']
): Promise<void> {
const countP = prisma.user.count();
const delayedRejection = new Promise((_resolve, reject) =>
setTimeout(
() => reject(Error('unable to connect to Mongodb (timeout)')),
1000
)
);
await Promise.race([countP, delayedRejection]);
}
export function setupServer(): void {
let fastify: FastifyTestInstance;
beforeAll(async () => {
@@ -165,6 +178,8 @@ export function setupServer(): void {
fastify = await build(buildOptions);
await fastify.ready();
await checkCanConnectToDb(fastify.prisma);
// Prisma does not support TTL indexes in the schema yet, so, to avoid
// conflicts with the TTL index in the sessions collection, we need to
// create it manually (before interacting with the db in any way). Also,
+2 -1
View File
@@ -1,6 +1,6 @@
import Fastify, { FastifyInstance } from 'fastify';
import { defaultUserEmail } from '../../jest.utils';
import { checkCanConnectToDb, defaultUserEmail } from '../../jest.utils';
import { HOME_LOCATION } from '../utils/env';
import { devAuth } from '../plugins/auth-dev';
import prismaPlugin from '../db/prisma';
@@ -19,6 +19,7 @@ describe('dev login', () => {
await fastify.register(auth);
await fastify.register(devAuth);
await fastify.register(prismaPlugin);
await checkCanConnectToDb(fastify.prisma);
});
beforeEach(async () => {
@@ -2,6 +2,7 @@ import Fastify, { FastifyInstance } from 'fastify';
import db from '../../db/prisma';
import { createUserInput } from '../../utils/create-user';
import { checkCanConnectToDb } from '../../../jest.utils';
import { findOrCreateUser } from './auth-helpers';
const captureException = jest.fn();
@@ -9,6 +10,7 @@ const captureException = jest.fn();
async function setupServer() {
const fastify = Fastify();
await fastify.register(db);
await checkCanConnectToDb(fastify.prisma);
// @ts-expect-error we're mocking the Sentry plugin
fastify.Sentry = { captureException };
return fastify;