feat: add simple Express app with /hello endpoint and tests

This commit is contained in:
coaxial
2025-10-23 15:14:23 +02:00
committed by coaxial (aider)
parent 928c1afacd
commit 8bf16b0ac2
5 changed files with 63 additions and 0 deletions

5
src/greeting.js Normal file
View File

@@ -0,0 +1,5 @@
function getGreeting() {
return 'Hello world';
}
module.exports = { getGreeting };

17
src/server.js Normal file
View File

@@ -0,0 +1,17 @@
const express = require('express');
const { getGreeting } = require('./greeting');
const app = express();
const PORT = process.env.PORT || 3000;
app.get('/hello', (req, res) => {
res.send(getGreeting());
});
if (require.main === module) {
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});
}
module.exports = app;

24
tests/e2e/e2e.test.js Normal file
View File

@@ -0,0 +1,24 @@
const axios = require('axios');
const app = require('../../src/server');
let server;
let baseURL;
beforeAll(done => {
server = app.listen(0, () => {
const { port } = server.address();
baseURL = `http://127.0.0.1:${port}`;
done();
});
});
afterAll(done => {
server.close(done);
});
describe('E2E GET /hello', () => {
it('responds with Hello world', async () => {
const res = await axios.get(`${baseURL}/hello`);
expect(res.status).toBe(200);
expect(res.data).toBe('Hello world');
});
});

View File

@@ -0,0 +1,10 @@
const request = require('supertest');
const app = require('../../src/server');
describe('GET /hello', () => {
it('should return Hello world', async () => {
const res = await request(app).get('/hello');
expect(res.statusCode).toBe(200);
expect(res.text).toBe('Hello world');
});
});

View File

@@ -0,0 +1,7 @@
const { getGreeting } = require('../../src/greeting');
describe('getGreeting', () => {
it('returns the hello world message', () => {
expect(getGreeting()).toBe('Hello world');
});
});