UNPKG

myex-cli

Version:

Opinionated Express.js framework with CLI tools

61 lines (50 loc) 1.8 kB
import { jest } from '@jest/globals'; import request from 'supertest'; import jwt from 'jsonwebtoken'; // Mock database connection jest.unstable_mockModule('../src/db/connection.js', () => ({ connectToDatabase: jest.fn().mockResolvedValue(true), })); // Import the app after mocking dependencies const { default: app } = await import('../src/app.js'); describe('API Routes', () => { let authToken; // Before all tests beforeAll(async () => { // Set up test environment process.env.NODE_ENV = 'test'; process.env.JWT_SECRET = 'test_jwt_secret'; // Generate token for protected route test authToken = jwt.sign( { id: '123456789012345678901234', email: 'test@example.com', role: 'user' }, process.env.JWT_SECRET, { expiresIn: '1h' } ); }); // Health check endpoint describe('GET /health', () => { it('should return 200 and UP status', async () => { const response = await request(app).get('/health'); expect(response.status).toBe(200); expect(response.body.status).toBe('UP'); }); }); // API info endpoint describe('GET /api', () => { it('should return welcome message and API info', async () => { const response = await request(app).get('/api'); expect(response.status).toBe(200); expect(response.body.message).toBe('Welcome to the API'); expect(response.body.version).toBeDefined(); expect(response.body.endpoints).toBeDefined(); }); }); // Protected route describe('GET /api/protected', () => { it('should return 401 without auth token', async () => { const response = await request(app).get('/api/protected'); expect(response.status).toBe(401); expect(response.body.status).toBe('error'); }); }); });