Skip to main content
Glama
setup.tsโ€ข3.29 kB
import { PrismaClient } from '@prisma/client'; import { connectRedis, disconnectRedis } from '../config/redis'; import redisClient from '../config/redis'; import { getTestDatabaseUrl } from './config'; // Set test environment process.env.NODE_ENV = 'test'; // Get test database URL const testDatabaseUrl = getTestDatabaseUrl(); // Create a test-specific Prisma client const prisma = new PrismaClient({ datasources: { db: { url: testDatabaseUrl, }, }, }); // Track if environment is initialized let isInitialized = false; /** * Clean up database and cache */ async function cleanupTestEnvironment() { try { // Clear database in correct order to avoid foreign key constraints await prisma.task.deleteMany(); await prisma.project.deleteMany(); // Clear Redis cache if connected if (redisClient.isOpen) { await redisClient.flushDb(); } } catch (error) { console.error('Error during test cleanup:', error); // Don't throw to avoid stopping tests } } /** * Initialize test environment */ async function initializeTestEnvironment() { if (isInitialized) { return; // Already initialized } try { console.log('๐Ÿงช Test Environment Setup'); console.log('๐Ÿ“Š Database URL:', testDatabaseUrl); console.log('๐Ÿ”— Connecting to test database...'); // Try to connect to the test database await prisma.$connect(); await connectRedis(); await cleanupTestEnvironment(); isInitialized = true; console.log('โœ… Test environment initialized successfully'); console.log('๐Ÿงน Database and cache cleaned'); } catch (error) { console.error('โŒ Error initializing test environment:', error); // Provide helpful error message for database connection issues if ( error instanceof Error && (error.message.includes('P1010') || error.message.includes('denied access')) ) { console.error('\n๐Ÿ”ง Database Setup Required:'); console.error('The test database does not exist or you do not have access.'); console.error('\n๐Ÿ“‹ To fix this, run these commands:'); console.error('1. Create test database: createdb taskflow_test'); console.error('2. Run migrations: npx prisma migrate deploy'); console.error('3. Or use the setup script: npm run test:setup'); console.error('\n๐Ÿ’ก Alternative: Use the main database for tests by setting:'); console.error('export TEST_DATABASE_URL="postgresql://localhost:5432/taskflow"'); console.error('\n๐Ÿ” Debug: Run "npm run test:verify-env" to check environment variables'); } throw error; } } /** * Clean up test environment */ async function teardownTestEnvironment() { try { console.log('๐Ÿงน Cleaning up test environment...'); await cleanupTestEnvironment(); await prisma.$disconnect(); await disconnectRedis(); isInitialized = false; console.log('โœ… Test environment cleaned up successfully'); } catch (error) { console.error('โŒ Error during test teardown:', error); // Don't throw to avoid stopping tests } } // Jest lifecycle hooks beforeAll(initializeTestEnvironment); afterAll(teardownTestEnvironment); beforeEach(cleanupTestEnvironment); // Export prisma for use in tests if needed export { prisma };

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/jatinderbhola/mcp-taskflow-tracker-api'

If you have feedback or need assistance with the MCP directory API, please join our Discord server