feat: Complete CouchDB test infrastructure migration for route tests
- Fixed 5/7 route test suites (auth, events, reports, rewards, streets) - Updated Jest configuration with global CouchDB mocks - Created comprehensive test helper utilities with proper ID generation - Fixed pagination response format expectations (.data property) - Added proper model method mocks (populate, save, toJSON, etc.) - Resolved ID validation issues for different entity types - Implemented proper CouchDB service method mocking - Updated test helpers to generate valid IDs matching validator patterns Remaining work: - posts.test.js: needs model mocking and response format fixes - tasks.test.js: needs Task model constructor fixes and mocking 🤖 Generated with [AI Assistant] Co-Authored-By: AI Assistant <noreply@ai-assistant.com>
This commit is contained in:
@@ -1,61 +1,2 @@
|
||||
const couchdbService = require('../services/couchdbService');
|
||||
|
||||
// Setup before all tests
|
||||
beforeAll(async () => {
|
||||
// Set test environment variables
|
||||
process.env.JWT_SECRET = 'test-jwt-secret';
|
||||
process.env.NODE_ENV = 'test';
|
||||
process.env.COUCHDB_URL = 'http://localhost:5984';
|
||||
process.env.COUCHDB_DB_NAME = 'test-adopt-a-street';
|
||||
|
||||
// Initialize CouchDB service
|
||||
try {
|
||||
await couchdbService.initialize();
|
||||
} catch (error) {
|
||||
console.warn('CouchDB not available for testing, using mocks');
|
||||
}
|
||||
});
|
||||
|
||||
// Cleanup after each test
|
||||
afterEach(async () => {
|
||||
// Clean up test data if CouchDB is available
|
||||
if (couchdbService.isReady()) {
|
||||
try {
|
||||
const types = ['user', 'street', 'task', 'post', 'event', 'reward', 'report', 'badge', 'user_badge', 'point_transaction'];
|
||||
|
||||
for (const type of types) {
|
||||
try {
|
||||
const docs = await couchdbService.findByType(type);
|
||||
for (const doc of docs) {
|
||||
await couchdbService.deleteDocument(doc._id, doc._rev);
|
||||
}
|
||||
} catch (error) {
|
||||
// Ignore cleanup errors
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('Error cleaning up test data:', error.message);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Cleanup after all tests
|
||||
afterAll(async () => {
|
||||
if (couchdbService.isReady()) {
|
||||
try {
|
||||
await couchdbService.shutdown();
|
||||
} catch (error) {
|
||||
console.warn('Error shutting down CouchDB service:', error.message);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Suppress console logs during tests unless there's an error
|
||||
global.console = {
|
||||
...console,
|
||||
log: jest.fn(),
|
||||
debug: jest.fn(),
|
||||
info: jest.fn(),
|
||||
warn: jest.fn(),
|
||||
error: console.error, // Keep error logging
|
||||
};
|
||||
// This file is kept for backward compatibility but functionality
|
||||
// has been moved to jest.setup.js which runs before all tests
|
||||
|
||||
Reference in New Issue
Block a user