test(frontend): Backend in memory
This commit is contained in:
parent
e4945fac66
commit
04ac71dbed
4 changed files with 53 additions and 23 deletions
|
@ -11,3 +11,5 @@ DWENGO_PORT=3000
|
|||
|
||||
DWENGO_DB_NAME=":memory:"
|
||||
DWENGO_DB_UPDATE=true
|
||||
|
||||
DWENGO_CORS_ALLOWED_ORIGINS=http://localhost:5173,http://localhost:3000
|
||||
|
|
|
@ -15,13 +15,13 @@ import { makeTestStudents } from '../tests/test_assets/users/students.testdata.j
|
|||
import { makeTestTeachers } from '../tests/test_assets/users/teachers.testdata.js';
|
||||
import { getLogger, Logger } from '../src/logging/initalize.js';
|
||||
import { Collection } from '@mikro-orm/core';
|
||||
import { Group } from '../dist/entities/assignments/group.entity.js';
|
||||
import { Group } from '../src/entities/assignments/group.entity.js';
|
||||
|
||||
const logger: Logger = getLogger();
|
||||
|
||||
export async function seedDatabase(): Promise<void> {
|
||||
dotenv.config({ path: '.env.development.local' });
|
||||
const orm = await initORM();
|
||||
export async function seedDatabase(envFile = '.env.development.local', testMode = false): Promise<void> {
|
||||
dotenv.config({ path: envFile });
|
||||
const orm = await initORM(testMode);
|
||||
await orm.schema.clearDatabase();
|
||||
|
||||
const em = forkEntityManager();
|
||||
|
|
33
backend/tool/startTestApp.ts
Normal file
33
backend/tool/startTestApp.ts
Normal file
|
@ -0,0 +1,33 @@
|
|||
import express, { Express } from 'express';
|
||||
import { initORM } from '../src/orm.js';
|
||||
import apiRouter from '../src/routes/router.js';
|
||||
import { errorHandler } from '../src/middleware/error-handling/error-handler.js';
|
||||
import dotenv from 'dotenv';
|
||||
import cors from '../src/middleware/cors';
|
||||
import { authenticateUser } from '../src/middleware/auth/auth';
|
||||
import { seedDatabase } from './seed';
|
||||
|
||||
const envFile = '../.env.test';
|
||||
console.log(`Using env file: ${envFile}`);
|
||||
|
||||
dotenv.config({ path: envFile });
|
||||
|
||||
const app: Express = express();
|
||||
|
||||
app.use(express.json());
|
||||
app.use(cors);
|
||||
app.use(authenticateUser);
|
||||
|
||||
app.use('/api', apiRouter);
|
||||
app.use(errorHandler);
|
||||
|
||||
async function startServer(): Promise<void> {
|
||||
await seedDatabase(envFile, true);
|
||||
await initORM(true);
|
||||
|
||||
app.listen(9876, () => {
|
||||
console.log('Server is running on http://localhost:9876/api');
|
||||
});
|
||||
}
|
||||
|
||||
await startServer();
|
|
@ -1,7 +1,6 @@
|
|||
import { spawn } from "child_process";
|
||||
import { ChildProcess, execSync } from 'node:child_process';
|
||||
import { ChildProcess, spawnSync } from 'node:child_process';
|
||||
|
||||
let wasRunningBefore: boolean;
|
||||
let backendProcess: ChildProcess;
|
||||
|
||||
async function waitForEndpoint(url: string, delay = 1000, retries = 60): Promise<void> {
|
||||
|
@ -16,32 +15,28 @@ async function waitForEndpoint(url: string, delay = 1000, retries = 60): Promise
|
|||
}
|
||||
|
||||
export async function setup(): Promise<void> {
|
||||
// Check if the database container is already running
|
||||
const containerCheck = execSync("docker ps --filter 'name=db' --format '{{.Names}}'");
|
||||
wasRunningBefore = !(containerCheck.toString().includes("db"));
|
||||
|
||||
// Spin up the database
|
||||
execSync("docker compose up db --detach");
|
||||
|
||||
// Spin up the backend
|
||||
backendProcess = spawn("npm", ["run", "dev"], {
|
||||
// Precompile needed packages
|
||||
spawnSync("npm", ["run", "predev"], {
|
||||
cwd: "../backend",
|
||||
stdio: "inherit",
|
||||
});
|
||||
|
||||
// Spin up the backend
|
||||
backendProcess = spawn("tsx", ["--env-file=.env.development.example", "tool/startTestApp.ts"], {
|
||||
cwd: "../backend",
|
||||
stdio: "inherit",
|
||||
env: {
|
||||
...process.env,
|
||||
NODE_ENV: 'test',
|
||||
}
|
||||
});
|
||||
|
||||
// Wait until you can curl the backend
|
||||
await waitForEndpoint("http://localhost:3000/api");
|
||||
await waitForEndpoint("http://localhost:9876/api");
|
||||
}
|
||||
|
||||
export async function teardown(): Promise<void> {
|
||||
if (backendProcess) {
|
||||
backendProcess.kill();
|
||||
}
|
||||
|
||||
if (wasRunningBefore) {
|
||||
spawn("docker", ["compose", "down"], {
|
||||
cwd: "..",
|
||||
stdio: "inherit",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue