Skip to content

Commit a62764f

Browse files
fix: separate app and server to fix open handle warning in tests
1 parent d6d488f commit a62764f

File tree

5 files changed

+26
-20
lines changed

5 files changed

+26
-20
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Simple CI/CD backend testing",
55
"main": "server.js",
66
"scripts": {
7-
"start": "node server.js",
7+
"start": "node src/start.js",
88
"test": "jest --detectOpenHandles"
99
},
1010
"dependencies": {

server.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

server.test.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
const request = require('supertest');
2-
const app = require('./server');
2+
const app = require('./src/server'); // Only the app, no server listening!
33

44
describe('GET /', () => {
55
it('responds with Hello CI/CD World!', async () => {
6-
const response = await request(app).get('/');
7-
expect(response.text).toBe('Hello CI/CD World!');
8-
expect(response.statusCode).toBe(200);
6+
const res = await request(app).get('/');
7+
expect(res.text).toBe('Hello CI/CD World!');
98
});
109
});
10+
describe('GET /nonexistent', () => {
11+
it('responds with 404 Not Found', async () => {
12+
const res = await request(app).get('/nonexistent');
13+
expect(res.statusCode).toBe(404);
14+
});
15+
});

src/server.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const express = require('express');
2+
const app = express();
3+
4+
app.get('/', (req, res) => {
5+
res.send('Hello CI/CD World!');
6+
});
7+
8+
module.exports = app;

src/start.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const app = require('./server');
2+
const port = 3000;
3+
4+
app.listen(port, () => {
5+
console.log(`Server running on port ${port}`);
6+
console.log(`http://localhost:${port}`);
7+
console.log('Press Ctrl+C to stop the server');
8+
});

0 commit comments

Comments
 (0)