test: only use test dbs in testing (#53770)

This commit is contained in:
Oliver Eyton-Williams
2024-02-19 06:24:14 +01:00
committed by GitHub
parent b582dada89
commit 6849b096b4
2 changed files with 52 additions and 36 deletions
+44
View File
@@ -1,8 +1,28 @@
import { execSync } from 'child_process';
import request from 'supertest';
import { build } from './src/app';
import { createUserInput } from './src/utils/create-user';
import { examJson } from './__mocks__/exam';
import { MONGOHQ_URL } from './src/utils/env';
jest.mock('./src/utils/env', () => {
const createTestConnectionURL = (url: string, dbId: string) =>
url.replace(/(.*)(\?.*)/, `$1${dbId}$2`);
// There are other properties, and this type is too narrow, but we're only
// interested in MONGOHQ_URL here.
const actual: {
MONGOHQ_URL: string;
} = jest.requireActual('./src/utils/env');
return {
...actual,
MONGOHQ_URL: createTestConnectionURL(
actual.MONGOHQ_URL,
process.env.JEST_WORKER_ID!
)
};
});
type FastifyTestInstance = Awaited<ReturnType<typeof build>>;
@@ -71,11 +91,35 @@ export function setupServer(): void {
fastify = await build();
await fastify.ready();
// 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)
await fastify.prisma.$runCommandRaw({
createIndexes: 'sessions',
indexes: [
{
key: { expires: 1 },
name: 'expires_1',
background: true,
expireAfterSeconds: 0
}
]
});
// push the schema to the test db to setup indexes, unique constraints, etc
execSync('pnpm prisma db push -- --skip-generate', {
env: {
...process.env,
MONGOHQ_URL
}
});
global.fastifyTestInstance = fastify;
// allow a little time to setup the db
}, 10000);
afterAll(async () => {
await fastifyTestInstance.prisma.$runCommandRaw({ dropDatabase: 1 });
// Due to a prisma bug, this is not enough, we need to --force-exit jest:
// https://github.com/prisma/prisma/issues/18146
await fastifyTestInstance.close();
+8 -36
View File
@@ -1,9 +1,9 @@
import { execSync } from 'node:child_process';
import fp from 'fastify-plugin';
import { FastifyPluginAsync } from 'fastify';
import { PrismaClient } from '@prisma/client';
import { FREECODECAMP_NODE_ENV, MONGOHQ_URL } from '../utils/env';
// importing MONGOHQ_URL so we can mock it in testing.
import { MONGOHQ_URL } from '../utils/env';
declare module 'fastify' {
interface FastifyInstance {
@@ -11,48 +11,20 @@ declare module 'fastify' {
}
}
// Appends the dbId to the existing database name
const createTestConnectionURL = (url: string, dbId: string) =>
url.replace(/(.*)(\?.*)/, `$1${dbId}$2`);
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 -- --skip-generate', {
env: {
...process.env,
MONGOHQ_URL: createTestConnectionURL(
MONGOHQ_URL,
process.env.JEST_WORKER_ID
)
const prisma = new PrismaClient({
datasources: {
db: {
url: MONGOHQ_URL
}
});
}
const prisma = isTest(process.env.JEST_WORKER_ID)
? new PrismaClient({
datasources: {
db: {
url: createTestConnectionURL(
MONGOHQ_URL,
process.env.JEST_WORKER_ID
)
}
}
})
: new PrismaClient();
}
});
await prisma.$connect();
server.decorate('prisma', prisma);
server.addHook('onClose', async server => {
if (isTest(process.env.JEST_WORKER_ID)) {
await server.prisma.$runCommandRaw({ dropDatabase: 1 });
}
await server.prisma.$disconnect();
});
});