From 5c8b94f23612aa01f5cc024566c5d767a80ce39b Mon Sep 17 00:00:00 2001 From: Oliver Eyton-Williams Date: Wed, 4 Oct 2023 00:00:21 +0200 Subject: [PATCH] test(api): setup mongo schema before running tests (#51686) Co-authored-by: Muhammed Mustafa --- api/jest.utils.ts | 3 ++- api/src/db/prisma.ts | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/api/jest.utils.ts b/api/jest.utils.ts index 09c7b5951a6..698922a058d 100644 --- a/api/jest.utils.ts +++ b/api/jest.utils.ts @@ -64,7 +64,8 @@ export function setupServer(): void { await fastify.ready(); global.fastifyTestInstance = fastify; - }); + // allow a little time to setup the db + }, 10000); afterAll(async () => { // Due to a prisma bug, this is not enough, we need to --force-exit jest: diff --git a/api/src/db/prisma.ts b/api/src/db/prisma.ts index 390be6f388b..777aaf31bd0 100644 --- a/api/src/db/prisma.ts +++ b/api/src/db/prisma.ts @@ -1,3 +1,4 @@ +import { execSync } from 'node:child_process'; import fp from 'fastify-plugin'; import { FastifyPluginAsync } from 'fastify'; import { PrismaClient } from '@prisma/client'; @@ -18,6 +19,19 @@ const isTest = (workerId: string | undefined): workerId is string => !!workerId && FREECODECAMP_NODE_ENV === 'development'; const prismaPlugin: FastifyPluginAsync = fp(async (server, _options) => { + if (isTest(process.env.JEST_WORKER_ID)) { + // push the schema to the test db to setup indexes, unique constraints, etc + execSync('pnpm prisma db push', { + env: { + ...process.env, + MONGOHQ_URL: createTestConnectionURL( + MONGOHQ_URL, + process.env.JEST_WORKER_ID + ) + } + }); + } + const prisma = isTest(process.env.JEST_WORKER_ID) ? new PrismaClient({ datasources: {