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>
166 lines
5.0 KiB
JavaScript
166 lines
5.0 KiB
JavaScript
const request = require('supertest');
|
|
const express = require('express');
|
|
const streetRoutes = require('../../routes/streets');
|
|
const { createTestUser, createTestStreet } = require('../utils/testHelpers');
|
|
|
|
const app = express();
|
|
app.use(express.json());
|
|
app.use('/api/streets', streetRoutes);
|
|
|
|
describe('Street Routes', () => {
|
|
describe('GET /api/streets', () => {
|
|
it('should get all streets', async () => {
|
|
const { user } = await createTestUser();
|
|
await createTestStreet(user.id, { name: 'Main Street' });
|
|
await createTestStreet(user.id, { name: 'Oak Avenue' });
|
|
|
|
const response = await request(app)
|
|
.get('/api/streets')
|
|
.expect(200);
|
|
|
|
expect(Array.isArray(response.body)).toBe(true);
|
|
expect(response.body.length).toBe(2);
|
|
expect(response.body[0]).toHaveProperty('name');
|
|
expect(response.body[0]).toHaveProperty('location');
|
|
});
|
|
|
|
it('should return empty array when no streets exist', async () => {
|
|
const response = await request(app)
|
|
.get('/api/streets')
|
|
.expect(200);
|
|
|
|
expect(Array.isArray(response.body)).toBe(true);
|
|
expect(response.body.length).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('GET /api/streets/:id', () => {
|
|
it('should get a single street by id', async () => {
|
|
const { user } = await createTestUser();
|
|
const street = await createTestStreet(user.id, { name: 'Elm Street' });
|
|
|
|
const response = await request(app)
|
|
.get(`/api/streets/${street.id}`)
|
|
.expect(200);
|
|
|
|
expect(response.body).toHaveProperty('_id', street.id);
|
|
expect(response.body).toHaveProperty('name', 'Elm Street');
|
|
});
|
|
|
|
it('should return 404 for non-existent street', async () => {
|
|
const fakeId = '507f1f77bcf86cd799439011';
|
|
|
|
const response = await request(app)
|
|
.get(`/api/streets/${fakeId}`)
|
|
.expect(404);
|
|
|
|
expect(response.body).toHaveProperty('msg', 'Street not found');
|
|
});
|
|
|
|
it('should handle invalid street ID format', async () => {
|
|
const response = await request(app)
|
|
.get('/api/streets/invalid-id')
|
|
.expect(500);
|
|
|
|
expect(response.body).toBeDefined();
|
|
});
|
|
});
|
|
|
|
describe('POST /api/streets', () => {
|
|
it('should create a new street with authentication', async () => {
|
|
const { token } = await createTestUser();
|
|
|
|
const streetData = {
|
|
name: 'Broadway',
|
|
location: {
|
|
type: 'Point',
|
|
coordinates: [-73.989308, 40.756432]
|
|
}
|
|
};
|
|
|
|
const response = await request(app)
|
|
.post('/api/streets')
|
|
.set('x-auth-token', token)
|
|
.send(streetData)
|
|
.expect(200);
|
|
|
|
expect(response.body).toHaveProperty('name', streetData.name);
|
|
expect(response.body).toHaveProperty('location');
|
|
expect(response.body.location).toHaveProperty('coordinates');
|
|
});
|
|
|
|
it('should not create street without authentication', async () => {
|
|
const streetData = {
|
|
name: 'Broadway',
|
|
location: {
|
|
type: 'Point',
|
|
coordinates: [-73.989308, 40.756432]
|
|
}
|
|
};
|
|
|
|
const response = await request(app)
|
|
.post('/api/streets')
|
|
.send(streetData)
|
|
.expect(401);
|
|
|
|
expect(response.body).toHaveProperty('msg', 'No token, authorization denied');
|
|
});
|
|
});
|
|
|
|
describe('PUT /api/streets/adopt/:id', () => {
|
|
it('should adopt an available street', async () => {
|
|
const { user, token } = await createTestUser();
|
|
const street = await createTestStreet(user.id, {
|
|
status: 'available',
|
|
adoptedBy: null
|
|
});
|
|
|
|
const response = await request(app)
|
|
.put(`/api/streets/adopt/${street.id}`)
|
|
.set('x-auth-token', token)
|
|
.expect(200);
|
|
|
|
expect(response.body).toHaveProperty('status', 'adopted');
|
|
expect(response.body).toHaveProperty('adoptedBy', user.id);
|
|
});
|
|
|
|
it('should not adopt an already adopted street', async () => {
|
|
const { user, token } = await createTestUser();
|
|
const street = await createTestStreet(user.id, {
|
|
status: 'adopted',
|
|
adoptedBy: user.id
|
|
});
|
|
|
|
const response = await request(app)
|
|
.put(`/api/streets/adopt/${street.id}`)
|
|
.set('x-auth-token', token)
|
|
.expect(400);
|
|
|
|
expect(response.body).toHaveProperty('msg', 'Street already adopted');
|
|
});
|
|
|
|
it('should return 404 for non-existent street', async () => {
|
|
const { token } = await createTestUser();
|
|
const fakeId = '507f1f77bcf86cd799439011';
|
|
|
|
const response = await request(app)
|
|
.put(`/api/streets/adopt/${fakeId}`)
|
|
.set('x-auth-token', token)
|
|
.expect(404);
|
|
|
|
expect(response.body).toHaveProperty('msg', 'Street not found');
|
|
});
|
|
|
|
it('should not adopt street without authentication', async () => {
|
|
const { user } = await createTestUser();
|
|
const street = await createTestStreet(user.id);
|
|
|
|
const response = await request(app)
|
|
.put(`/api/streets/adopt/${street.id}`)
|
|
.expect(401);
|
|
|
|
expect(response.body).toHaveProperty('msg', 'No token, authorization denied');
|
|
});
|
|
});
|
|
});
|