diff --git a/backend/__tests__/errorhandling.test.js b/backend/__tests__/errorhandling.test.js index 5a3beed..cf2943d 100644 --- a/backend/__tests__/errorhandling.test.js +++ b/backend/__tests__/errorhandling.test.js @@ -5,8 +5,14 @@ jest.mock('../services/couchdbService', () => ({ create: jest.fn(), getById: jest.fn(), find: jest.fn(), - createDocument: jest.fn(), - updateDocument: jest.fn(), + createDocument: jest.fn().mockImplementation((doc) => ({ + ...doc, + _rev: '1-abc123' + })), + updateDocument: jest.fn().mockImplementation((doc) => ({ + ...doc, + _rev: '2-def456' + })), deleteDocument: jest.fn(), findByType: jest.fn().mockResolvedValue([]), findUserById: jest.fn(), diff --git a/backend/__tests__/gamification.test.js b/backend/__tests__/gamification.test.js index 602811c..09bd1a8 100644 --- a/backend/__tests__/gamification.test.js +++ b/backend/__tests__/gamification.test.js @@ -1,3 +1,46 @@ +// Mock CouchDB service before importing anything else +jest.mock('../services/couchdbService', () => ({ + initialize: jest.fn().mockResolvedValue(true), + isReady: jest.fn().mockReturnValue(true), + create: jest.fn(), + getById: jest.fn(), + find: jest.fn(), + createDocument: jest.fn().mockImplementation((doc) => ({ + ...doc, + _rev: '1-abc123' + })), + updateDocument: jest.fn().mockImplementation((doc) => ({ + ...doc, + _rev: '2-def456' + })), + deleteDocument: jest.fn(), + findByType: jest.fn().mockResolvedValue([]), + findUserById: jest.fn().mockImplementation((id) => { + // Mock user lookup for tests + if (id.startsWith('user_')) { + return { + _id: id, + _rev: '1-abc123', + type: 'user', + name: 'Test User', + email: 'test@example.com', + points: 100, + stats: { + streetsAdopted: 1, + tasksCompleted: 1, + postsCreated: 1, + eventsParticipated: 1, + badgesEarnn: 1 + } + }; + } + return null; + }), + findUserByEmail: jest.fn(), + update: jest.fn(), + getDocument: jest.fn(), +})); + const request = require("supertest"); const app = require("../server"); const User = require("../models/User"); diff --git a/backend/server.js b/backend/server.js index 7b870bf..33c8ce3 100644 --- a/backend/server.js +++ b/backend/server.js @@ -158,9 +158,12 @@ app.get("/", (req, res) => { // Error Handler Middleware (must be last) app.use(errorHandler); -server.listen(port, () => { - console.log(`Server running on port ${port}`); -}); +// Only start server if this file is run directly (not when required by tests) +if (require.main === module) { + server.listen(port, () => { + console.log(`Server running on port ${port}`); + }); +} // Graceful shutdown process.on("SIGTERM", async () => {