From 22724d5dda6b28b00780e3b24e790981d96ecbc9 Mon Sep 17 00:00:00 2001 From: Oliver Eyton-Williams Date: Wed, 28 May 2025 17:52:11 +0200 Subject: [PATCH] test: inform devs when db connection not established (#60539) --- api/jest.utils.ts | 15 +++++++++++++++ api/src/plugins/auth-dev.test.ts | 3 ++- api/src/routes/helpers/auth-helpers.test.ts | 2 ++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/api/jest.utils.ts b/api/jest.utils.ts index ffb18c193ca..c45419ff4ca 100644 --- a/api/jest.utils.ts +++ b/api/jest.utils.ts @@ -155,6 +155,19 @@ const indexData: IndexData[] = [ } ]; +export async function checkCanConnectToDb( + prisma: FastifyTestInstance['prisma'] +): Promise { + 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, diff --git a/api/src/plugins/auth-dev.test.ts b/api/src/plugins/auth-dev.test.ts index b99384e1f7c..639634001b3 100644 --- a/api/src/plugins/auth-dev.test.ts +++ b/api/src/plugins/auth-dev.test.ts @@ -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 () => { diff --git a/api/src/routes/helpers/auth-helpers.test.ts b/api/src/routes/helpers/auth-helpers.test.ts index 48173fa59f5..7bef32befde 100644 --- a/api/src/routes/helpers/auth-helpers.test.ts +++ b/api/src/routes/helpers/auth-helpers.test.ts @@ -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;