Skip to content

Extract server bootstrap from index.ts so startup is unit-testable #80

Description

@mikewheeleer

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.

Metadata

Metadata

Assignees

No fields configured for Feature.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions