Implement complete backend testing infrastructure with Jest and Supertest: Test Setup: - Configure Jest for Node.js environment - Add MongoDB Memory Server for isolated testing - Create test setup with database connection helpers - Add test scripts: test, test:coverage, test:watch Test Files (176 total tests, 109 passing): - Middleware tests: auth.test.js (100% coverage) - Model tests: User, Street, Task, Post (82.5% coverage) - Route tests: auth, streets, tasks, posts, events, rewards, reports Test Coverage: - Overall: 54.75% (on track for 70% target) - Models: 82.5% - Middleware: 100% - Routes: 45.84% Test Utilities: - Helper functions for creating test users, streets, tasks, posts - Test database setup and teardown - MongoDB Memory Server configuration - Coverage reporting with lcov Testing Features: - Isolated test environment (no production data pollution) - Async/await test patterns - Proper setup/teardown for each test - Authentication testing with JWT tokens - Validation testing for all routes - Error handling verification Scripts: - Database seeding scripts for development - Test data generation utilities Dependencies: - jest@29.7.0 - supertest@7.0.0 - mongodb-memory-server@10.1.2 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
161 lines
5.0 KiB
JavaScript
161 lines
5.0 KiB
JavaScript
const request = require('supertest');
|
|
const express = require('express');
|
|
const eventsRoutes = require('../../routes/events');
|
|
const Event = require('../../models/Event');
|
|
const { createTestUser, createTestEvent } = require('../utils/testHelpers');
|
|
|
|
// Create Express app for testing
|
|
const app = express();
|
|
app.use(express.json());
|
|
app.use('/api/events', eventsRoutes);
|
|
|
|
describe('Events Routes', () => {
|
|
describe('GET /api/events', () => {
|
|
it('should get all events', async () => {
|
|
const { user } = await createTestUser();
|
|
await createTestEvent(user.id, { title: 'Event 1' });
|
|
await createTestEvent(user.id, { title: 'Event 2' });
|
|
|
|
const response = await request(app)
|
|
.get('/api/events')
|
|
.expect(200);
|
|
|
|
expect(Array.isArray(response.body)).toBe(true);
|
|
expect(response.body.length).toBe(2);
|
|
expect(response.body[0]).toHaveProperty('title');
|
|
});
|
|
|
|
it('should return empty array when no events exist', async () => {
|
|
const response = await request(app)
|
|
.get('/api/events')
|
|
.expect(200);
|
|
|
|
expect(Array.isArray(response.body)).toBe(true);
|
|
expect(response.body.length).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('POST /api/events', () => {
|
|
it('should create a new event with authentication', async () => {
|
|
const { token } = await createTestUser();
|
|
const eventData = {
|
|
title: 'Community Cleanup',
|
|
description: 'Annual community cleanup event',
|
|
date: new Date(Date.now() + 86400000),
|
|
location: 'Central Park',
|
|
};
|
|
|
|
const response = await request(app)
|
|
.post('/api/events')
|
|
.set('x-auth-token', token)
|
|
.send(eventData)
|
|
.expect(200);
|
|
|
|
expect(response.body).toHaveProperty('_id');
|
|
expect(response.body.title).toBe(eventData.title);
|
|
expect(response.body.description).toBe(eventData.description);
|
|
expect(response.body.location).toBe(eventData.location);
|
|
|
|
// Verify event was created in database
|
|
const event = await Event.findById(response.body._id);
|
|
expect(event).toBeTruthy();
|
|
expect(event.title).toBe(eventData.title);
|
|
});
|
|
|
|
it('should reject event creation without authentication', async () => {
|
|
const eventData = {
|
|
title: 'Unauthorized Event',
|
|
description: 'This should fail',
|
|
date: new Date(Date.now() + 86400000),
|
|
location: 'Nowhere',
|
|
};
|
|
|
|
const response = await request(app)
|
|
.post('/api/events')
|
|
.send(eventData)
|
|
.expect(401);
|
|
|
|
expect(response.body).toHaveProperty('msg', 'No token, authorization denied');
|
|
});
|
|
|
|
it('should handle missing required fields', async () => {
|
|
const { token } = await createTestUser();
|
|
|
|
const response = await request(app)
|
|
.post('/api/events')
|
|
.set('x-auth-token', token)
|
|
.send({ title: 'Incomplete Event' })
|
|
.expect(500);
|
|
|
|
expect(response.body).toBeDefined();
|
|
});
|
|
});
|
|
|
|
describe('PUT /api/events/rsvp/:id', () => {
|
|
it('should allow user to RSVP to an event', async () => {
|
|
const { user, token } = await createTestUser();
|
|
const event = await createTestEvent(user.id);
|
|
|
|
const response = await request(app)
|
|
.put(`/api/events/rsvp/${event.id}`)
|
|
.set('x-auth-token', token)
|
|
.expect(200);
|
|
|
|
expect(Array.isArray(response.body)).toBe(true);
|
|
expect(response.body).toContain(user.id);
|
|
|
|
// Verify event was updated in database
|
|
const updatedEvent = await Event.findById(event.id);
|
|
expect(updatedEvent.participants).toContain(user._id);
|
|
});
|
|
|
|
it('should not allow duplicate RSVPs', async () => {
|
|
const { user, token } = await createTestUser();
|
|
const event = await createTestEvent(user.id, {
|
|
participants: [user.id],
|
|
});
|
|
|
|
const response = await request(app)
|
|
.put(`/api/events/rsvp/${event.id}`)
|
|
.set('x-auth-token', token)
|
|
.expect(400);
|
|
|
|
expect(response.body).toHaveProperty('msg', 'Already RSVPed');
|
|
});
|
|
|
|
it('should return 404 for non-existent event', async () => {
|
|
const { token } = await createTestUser();
|
|
const fakeId = '507f1f77bcf86cd799439011';
|
|
|
|
const response = await request(app)
|
|
.put(`/api/events/rsvp/${fakeId}`)
|
|
.set('x-auth-token', token)
|
|
.expect(404);
|
|
|
|
expect(response.body).toHaveProperty('msg', 'Event not found');
|
|
});
|
|
|
|
it('should reject RSVP without authentication', async () => {
|
|
const { user } = await createTestUser();
|
|
const event = await createTestEvent(user.id);
|
|
|
|
const response = await request(app)
|
|
.put(`/api/events/rsvp/${event.id}`)
|
|
.expect(401);
|
|
|
|
expect(response.body).toHaveProperty('msg', 'No token, authorization denied');
|
|
});
|
|
|
|
it('should handle invalid event ID format', async () => {
|
|
const { token } = await createTestUser();
|
|
|
|
const response = await request(app)
|
|
.put('/api/events/rsvp/invalid-id')
|
|
.set('x-auth-token', token)
|
|
.expect(500);
|
|
|
|
expect(response.body).toBeDefined();
|
|
});
|
|
});
|
|
});
|