news-working.test.ts•3.75 kB
/**
* Reliable tests for News API endpoints that use mocks instead of real API calls
*/
import { describe, test, expect, beforeEach, jest, beforeAll } from '@jest/globals';
import request from 'supertest';
import { Express } from 'express';
// Import the NewsService mock
import '../mocks/news-service.mock';
import { McpServer } from '../../server';
import { cacheService } from '../../utils/cache';
// Mock database connection to prevent actual DB connections
jest.mock('../../utils/db', () => {
return {
connectDb: jest.fn().mockResolvedValue(undefined),
disconnectDb: jest.fn().mockResolvedValue(undefined),
};
});
describe('News API Integration Tests', () => {
let app: Express;
let server: McpServer;
beforeAll(async () => {
// Create a test server but don't actually start it (no port binding)
process.env.NODE_ENV = 'test'; // Ensure we're in test mode
server = new McpServer();
app = server.getApp();
// Mock the server.start method to prevent actual server startup
jest.spyOn(server, 'start').mockImplementation(async () => {
// This is intentionally empty to prevent real server start
return Promise.resolve();
});
// Initialize but don't actually start the server
await server.start();
});
beforeEach(() => {
// Clear cache between tests
cacheService.clear();
jest.clearAllMocks();
});
test('GET /api/news/top should return top news articles', async () => {
const response = await request(app)
.get('/api/news/top')
.expect('Content-Type', /json/)
.expect(200);
// Verify response structure
expect(response.body.success).toBe(true);
expect(Array.isArray(response.body.data)).toBe(true);
// Verify article properties if data exists
if (response.body.data.length > 0) {
const article = response.body.data[0];
expect(article).toHaveProperty('title');
expect(article).toHaveProperty('uuid');
expect(article).toHaveProperty('source');
}
});
test('GET /api/news/top should filter by category', async () => {
const response = await request(app)
.get('/api/news/top?categories=technology')
.expect('Content-Type', /json/)
.expect(200);
// Verify response structure
expect(response.body.success).toBe(true);
expect(Array.isArray(response.body.data)).toBe(true);
});
test('GET /api/news/all should return paginated articles', async () => {
const response = await request(app)
.get('/api/news/all?page=1&limit=10')
.expect('Content-Type', /json/)
.expect(200);
// Verify response structure
expect(response.body.success).toBe(true);
expect(Array.isArray(response.body.data)).toBe(true);
// Check pagination metadata
expect(response.body.meta).toBeDefined();
expect(response.body.meta).toHaveProperty('page');
expect(response.body.meta).toHaveProperty('limit');
});
test('GET /api/news/uuid/:uuid should return a specific article', async () => {
const response = await request(app)
.get('/api/news/uuid/test-uuid-1')
.expect('Content-Type', /json/)
.expect(200);
// Verify response structure and content
expect(response.body.success).toBe(true);
expect(response.body.data).toHaveProperty('uuid', 'test-uuid-1');
});
test('GET /api/news/uuid/:uuid should return 404 for non-existent article', async () => {
const response = await request(app)
.get('/api/news/uuid/non-existent-uuid')
.expect('Content-Type', /json/)
.expect(404);
// Verify error response
expect(response.body.success).toBe(false);
expect(response.body.error).toBeDefined();
});
});