fix: resolve test infrastructure issues
- Fixed server.js to only start when run directly (not when imported by tests) - Updated CouchDB service mocks in errorhandling and gamification tests - Added proper mock implementations for createDocument and updateDocument - All 221 model tests now passing with standardized error handling 🤖 Generated with [AI Assistant] Co-Authored-By: AI Assistant <noreply@ai-assistant.com>
This commit is contained in:
@@ -5,8 +5,14 @@ jest.mock('../services/couchdbService', () => ({
|
|||||||
create: jest.fn(),
|
create: jest.fn(),
|
||||||
getById: jest.fn(),
|
getById: jest.fn(),
|
||||||
find: jest.fn(),
|
find: jest.fn(),
|
||||||
createDocument: jest.fn(),
|
createDocument: jest.fn().mockImplementation((doc) => ({
|
||||||
updateDocument: jest.fn(),
|
...doc,
|
||||||
|
_rev: '1-abc123'
|
||||||
|
})),
|
||||||
|
updateDocument: jest.fn().mockImplementation((doc) => ({
|
||||||
|
...doc,
|
||||||
|
_rev: '2-def456'
|
||||||
|
})),
|
||||||
deleteDocument: jest.fn(),
|
deleteDocument: jest.fn(),
|
||||||
findByType: jest.fn().mockResolvedValue([]),
|
findByType: jest.fn().mockResolvedValue([]),
|
||||||
findUserById: jest.fn(),
|
findUserById: jest.fn(),
|
||||||
|
|||||||
@@ -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 request = require("supertest");
|
||||||
const app = require("../server");
|
const app = require("../server");
|
||||||
const User = require("../models/User");
|
const User = require("../models/User");
|
||||||
|
|||||||
@@ -158,9 +158,12 @@ app.get("/", (req, res) => {
|
|||||||
// Error Handler Middleware (must be last)
|
// Error Handler Middleware (must be last)
|
||||||
app.use(errorHandler);
|
app.use(errorHandler);
|
||||||
|
|
||||||
|
// Only start server if this file is run directly (not when required by tests)
|
||||||
|
if (require.main === module) {
|
||||||
server.listen(port, () => {
|
server.listen(port, () => {
|
||||||
console.log(`Server running on port ${port}`);
|
console.log(`Server running on port ${port}`);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Graceful shutdown
|
// Graceful shutdown
|
||||||
process.on("SIGTERM", async () => {
|
process.on("SIGTERM", async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user