Skip to content

Commit d733599

Browse files
committed
fix: open handles
1 parent 6a8fc6b commit d733599

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

jest.config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,9 @@ module.exports = {
2929
lines: 70,
3030
statements: 70
3131
}
32-
}
32+
},
33+
// Help detect open handles that prevent Jest from exiting
34+
detectOpenHandles: true,
35+
// Force exit after tests complete (as a safety net)
36+
forceExit: true
3337
};

src/app.test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,20 @@
22

33
import request from 'supertest';
44
// import express from 'express';
5-
import { app } from './app';
5+
import { app, server } from './app';
66

77
describe('Express App Tests', () => {
8+
// Clean up after all tests
9+
afterAll(async () => {
10+
if (server) {
11+
await new Promise<void>((resolve) => {
12+
server.close(() => {
13+
resolve();
14+
});
15+
});
16+
}
17+
});
18+
819
// Root endpoint test
920
describe('GET /', () => {
1021
it('should return Hello, TypeScript Express!', async () => {

src/app.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { loadPaperScoreCriteria } from './agents/paper-score.1.criteria.schema';
1010
import { joinSuggestions, PaperScoreResultSchema } from './agents/paper-score.schema';
1111
import { z } from 'zod';
1212
import { asyncHandler } from './middleware/error-handler';
13+
import { Server } from 'http';
1314

1415
export const app = express();
1516
const port = process.env['PORT'] || 8000;
@@ -145,10 +146,16 @@ app.use((err: Error, _: Request, res: Response, __: express.NextFunction) => {
145146
res.status(500).json(response);
146147
});
147148

148-
app
149-
.listen(port, () => {
150-
console.log(`app listening on port ${port}`);
151-
})
152-
.on('error', (err: any) => {
153-
console.error('Failed to start server:', err);
154-
});
149+
// Export server instance for testing
150+
export let server: Server;
151+
152+
// Only start the server if this file is run directly (not imported)
153+
if (require.main === module) {
154+
server = app
155+
.listen(port, () => {
156+
console.log(`app listening on port ${port}`);
157+
})
158+
.on('error', (err: any) => {
159+
console.error('Failed to start server:', err);
160+
});
161+
}

0 commit comments

Comments
 (0)