Make server bootstrap and shutdown testable
Description
src/server.ts wires app.listen, signal handlers, and the 10-second drain timeout at module top level. Importing it starts a real listener and registers process-level SIGTERM/SIGINT handlers, which keeps the Jest event loop alive — so jest.config.js and the README.md explicitly exclude server.ts from coverage and cap global thresholds below the campaign's 95%. This issue refactors the bootstrap into exported functions so it can be exercised under test, unblocking the coverage bump.
Requirements and context
- Repository scope: StableRoute-Org/Stableroute-backend only.
- Refactor
src/server.ts into exported, side-effect-free functions: e.g. createServer(app), registerSignalHandlers(server), and start() — only start() (or a require.main === module guard) actually listens.
- Keep the existing graceful-shutdown semantics (drain on close, forced exit after 10s) and the
PORT env behavior.
- Once testable, remove the
server.ts coverage exclusion in jest.config.js and raise the thresholds toward 95% as the README note anticipates; update the README/jest comments accordingly.
- No change to runtime behavior when run via
npm start.
Suggested execution
- Fork the repo and create a branch
git checkout -b refactor/bootstrap-32-testable-server
- Implement changes
- Write code in:
src/server.ts — export createServer/registerSignalHandlers/start; guard the listen behind require.main === module. Update jest.config.js thresholds.
- Write comprehensive tests in: extend
src/__tests__/server.test.ts — start on an ephemeral port, hit /health, invoke the shutdown path, and assert the drain timer is unref'd (no hanging handles).
- Add documentation: update the coverage note in
README.md to reflect the raised thresholds.
- Add TSDoc on each exported function.
- Validate security: ensure signal handlers are not double-registered across imports.
- Test and commit
Test and commit
- Run
npm run build, npm run lint, and npm test.
- Cover edge cases: clean shutdown,
server.close error path, port already in use.
- Paste the full
npm test output and a coverage summary in the PR.
Example commit message
refactor(bootstrap): make server startup/shutdown unit-testable
Guidelines
- Minimum 95 percent test coverage for impacted code.
- Clear, reviewer-focused documentation.
- Timeframe: 96 hours.
Community & contribution rewards
- 💬 Join the StableRoute community on Discord for questions, reviews, and faster merges: https://discord.gg/37aCpusvx
- ⭐ This is a GrantFox OSS / Official Campaign task and may be rewarded. When your PR is merged you'll be prompted to rate the project — if this issue and the maintainers helped you ship, we'd be grateful for a 5-star rating. Clear questions in Discord and tidy, well-tested PRs are the fastest path to a merge and a reward.
Make server bootstrap and shutdown testable
Description
src/server.tswiresapp.listen, signal handlers, and the 10-second drain timeout at module top level. Importing it starts a real listener and registers process-levelSIGTERM/SIGINThandlers, which keeps the Jest event loop alive — sojest.config.jsand theREADME.mdexplicitly excludeserver.tsfrom coverage and cap global thresholds below the campaign's 95%. This issue refactors the bootstrap into exported functions so it can be exercised under test, unblocking the coverage bump.Requirements and context
src/server.tsinto exported, side-effect-free functions: e.g.createServer(app),registerSignalHandlers(server), andstart()— onlystart()(or arequire.main === moduleguard) actually listens.PORTenv behavior.server.tscoverage exclusion injest.config.jsand raise the thresholds toward 95% as the README note anticipates; update the README/jest comments accordingly.npm start.Suggested execution
git checkout -b refactor/bootstrap-32-testable-serversrc/server.ts— exportcreateServer/registerSignalHandlers/start; guard the listen behindrequire.main === module. Updatejest.config.jsthresholds.src/__tests__/server.test.ts— start on an ephemeral port, hit/health, invoke the shutdown path, and assert the drain timer isunref'd (no hanging handles).README.mdto reflect the raised thresholds.Test and commit
npm run build,npm run lint, andnpm test.server.closeerror path, port already in use.npm testoutput and a coverage summary in the PR.Example commit message
refactor(bootstrap): make server startup/shutdown unit-testableGuidelines
Community & contribution rewards