Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions apps/api/.env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Development .env for API\
# DATABASE_URL="postgresql://neondb_owner:npg_PCxbvGSROi20@ep-spring-term-a1xllvq2-pooler.ap-southeast-1.aws.neon.tech/neondb?sslmode=require&channel_binding=require"
DATABASE_URL="postgresql://composter:composter@localhost:5435/composter_test?schema=public"
BETTER_AUTH_SECRET="232522c037dd020b411fda839874791acf1dc4c9c15f3c41996ff27cc68481ba"
NODE_ENV="development"
PORT=3000
RESEND_API_KEY=""
FROM_EMAIL=""
# Local URLs
BETTER_AUTH_URL="http://localhost:3000"
CLIENT_URL="http://localhost:5173"
13 changes: 13 additions & 0 deletions docker-compose.test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
services:
postgres-test:
image: postgres:17
container_name: composter-db-test
restart: "no"
ports:
- "5435:5432"
environment:
POSTGRES_USER: composter
POSTGRES_PASSWORD: composter
POSTGRES_DB: composter_test
tmpfs:
- /var/lib/postgresql/data
46 changes: 46 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"format": "prettier --write \"**/*.{ts,tsx,md}\"",
"test:e2e": "npx playwright test",
"test:e2e:watch": "npx playwright test --watch",
"setup:dev": "node scripts/test-bootstrap.js dev",
"setup:test": "node scripts/test-bootstrap.js test",
"show-report:e2e": "npx playwright show-report",
"changeset": "changeset",
"version-packages": "changeset version",
Expand All @@ -23,6 +25,7 @@
"@changesets/cli": "^2.27.0",
"@playwright/test": "^1.58.0",
"@types/node": "^25.0.10",
"dotenv-cli": "^11.0.0",
"prettier": "^3.0.0",
"turbo": "^2.3.0"
},
Expand Down
16 changes: 10 additions & 6 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { defineConfig, devices } from '@playwright/test';
import dotenv from 'dotenv';
import path from 'path';

dotenv.config({ path: path.resolve(__dirname, 'apps/api/.env.test') });

/**
* Read environment variables from file.
Expand Down Expand Up @@ -26,7 +30,7 @@ export default defineConfig({
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Base URL to use in actions like `await page.goto('')`. */
baseURL: 'http://localhost:3000',
baseURL: process.env.CLIENT_URL || 'http://localhost:5173',

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace:'on',
Expand Down Expand Up @@ -86,9 +90,9 @@ export default defineConfig({
],

/* Run your local dev server before starting the tests */
// webServer: {
// command: 'npm run start',
// url: 'http://localhost:3000',
// reuseExistingServer: !process.env.CI,
// },
webServer: {
command: 'npm run dev',
url: 'http://localhost:5173',
reuseExistingServer: !process.env.CI,
},
});
74 changes: 74 additions & 0 deletions scripts/test-bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const { execSync, spawn } = require('child_process');
const fs = require('fs');
const path = require('path');

const mode = process.argv[2];

if(!['dev', 'test'].includes(mode)) {
console.error("Usage: node scripts/test-bootstrap.js dev or test");
process.exit(0);
}

const isTest = mode === 'test'
const ROOT_DIR = process.cwd();
const API_DIR = path.join(ROOT_DIR, 'apps/api');

//configuration
const CONFIG = {
dockerFile: isTest ? 'docker-compose.test.yaml' : 'docker-compose.yaml',
envFile: isTest ? '.env.test' : '.env',
}

const run = (cmd, cwd = ROOT_DIR) => {
try {
console.log(`> ${cmd}`);
execSync(cmd, { stdio: 'inherit', cwd, env: process.env });
} catch (e) {
console.error(`❌ Command failed: ${cmd}`);
process.exit(1);
}
};

async function main() {
console.log(`🚀 Setting up ${mode.toUpperCase()} environment...`);

if(!fs.existsSync(ROOT_DIR, 'node_modules')){
run('npm install');
}

console.log('🐳 Resetting Database Containers...');
try{
execSync(`docker compose -f ${CONFIG.dockerFile} down -v`)
} catch(error){
//Ignore if already down
}

console.log('🐳 Starting Database...');
run(`docker compose -f ${CONFIG.dockerFile} up -d`);

console.log(`Waiting for db to be ready...`)
await new Promise(r=>setTimeout(r, 5000));

console.log('🔄 Syncing Schema...');

const envFile = `npx dotenv-cli -e ${CONFIG.envFile} --`

if(isTest){
run(`${envFile} npx prisma migrate dev --name test`, API_DIR)

console.log(`better-auth migrations`)
run(`${envFile} npx @better-auth/cli migrate --config auth/auth.js`, API_DIR)
} else {
run(`${envFile} npx prisma migrate dev --name dev`, API_DIR)

console.log(`better-auth migrations`)
run(`${envFile} npx @better-auth/cli migrate --config auth/auth.js`, API_DIR)
}

console.log(`✅ Setup complete!`)
}

main().catch((e)=>{
console.error(e);
process.exit(1);
})
6 changes: 6 additions & 0 deletions tests/api/health.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {test, expect } from "@playwright/test";

test('API Health Check', async({request}) =>{
const response = await request.get('api/health')
expect(response.ok()).toBeTruthy()
})