chore: add E2E tests for graceful shutdown and Hono error handling#1213
chore: add E2E tests for graceful shutdown and Hono error handling#1213
Conversation
📝 WalkthroughWalkthroughThis PR refines the middleware's TypeScript server discovery typing by using a more specific socket cast, and introduces two E2E test suites: one verifying graceful shutdown behavior when the HTTP server closes, and another validating error handling in the Hono middleware integration. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
commit: |
There was a problem hiding this comment.
🧹 Nitpick comments (3)
test/e2e/graceful-shutdown.spec.ts (3)
29-48: Consider closingproxyServerinafterEachto avoid leaks on test failure.
proxyServeris started inside eachitand only closed at the end of the test body. If an assertion fails (orcloseServeris never reached), the listening server leaks across tests, which can cascade into port conflicts or hung Vitest workers. Moving the cleanup intoafterEachmakes the suite more robust.♻️ Proposed cleanup
afterEach(async () => { + if (proxyServer?.listening) { + await closeServer(proxyServer); + } await mockTargetServer.stop(); });🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@test/e2e/graceful-shutdown.spec.ts` around lines 29 - 48, The tests can leak the listening proxyServer if a test fails before calling closeServer; update the cleanup to close any started proxyServer in afterEach by invoking closeServer(proxyServer) when proxyServer is defined (and await it), and swallow or rethrow errors as appropriate; locate the proxyServer variable and the existing afterEach/mockTargetServer.stop sequence and add the proxyServer shutdown there (and reset proxyServer to undefined after closing) so each test always attempts to close the server even on failure.
61-61: Minor: prefer127.0.0.1for consistency.Other E2E suites in this PR (e.g.,
test/e2e/hono.spec.ts) targethttp://127.0.0.1:${port}. Usinglocalhosthere can cause flakiness on dual-stack hosts where DNS resolves to::1while the server listens on IPv4 only. Aligning on127.0.0.1is safer and consistent with the rest of the suite.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@test/e2e/graceful-shutdown.spec.ts` at line 61, Replace the hardcoded "localhost" host in the E2E request with the IPv4 loopback to avoid DNS/IPv6 flakiness: update the call that constructs the URL using SERVER_PORT (the request(`http://localhost:${SERVER_PORT}`).get('/api').expect(200) invocation) to use "127.0.0.1" instead of "localhost" so the test targets http://127.0.0.1:${SERVER_PORT} and matches the other E2E suites.
79-79: Redundant mock setup.
mockTargetServer.forGet('/api').thenReply(200, 'OK')is already configured inbeforeEach(line 42). This duplicate registration adds another handler for the same path; it's harmless here but obscures intent. Safe to remove.♻️ Proposed fix
- await mockTargetServer.forGet('/api').thenReply(200, 'OK'); - // Multiple requests should not cause multiple close subscriptions🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@test/e2e/graceful-shutdown.spec.ts` at line 79, Remove the redundant mock registration: delete the duplicate call to mockTargetServer.forGet('/api').thenReply(200, 'OK') in the test body since the same route is already registered in the beforeEach setup; look for the mockTargetServer.forGet invocation in graceful-shutdown.spec.ts and keep only the beforeEach-registered handler to avoid adding duplicate handlers for '/api'.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@test/e2e/graceful-shutdown.spec.ts`:
- Around line 29-48: The tests can leak the listening proxyServer if a test
fails before calling closeServer; update the cleanup to close any started
proxyServer in afterEach by invoking closeServer(proxyServer) when proxyServer
is defined (and await it), and swallow or rethrow errors as appropriate; locate
the proxyServer variable and the existing afterEach/mockTargetServer.stop
sequence and add the proxyServer shutdown there (and reset proxyServer to
undefined after closing) so each test always attempts to close the server even
on failure.
- Line 61: Replace the hardcoded "localhost" host in the E2E request with the
IPv4 loopback to avoid DNS/IPv6 flakiness: update the call that constructs the
URL using SERVER_PORT (the
request(`http://localhost:${SERVER_PORT}`).get('/api').expect(200) invocation)
to use "127.0.0.1" instead of "localhost" so the test targets
http://127.0.0.1:${SERVER_PORT} and matches the other E2E suites.
- Line 79: Remove the redundant mock registration: delete the duplicate call to
mockTargetServer.forGet('/api').thenReply(200, 'OK') in the test body since the
same route is already registered in the beforeEach setup; look for the
mockTargetServer.forGet invocation in graceful-shutdown.spec.ts and keep only
the beforeEach-registered handler to avoid adding duplicate handlers for '/api'.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 30dca38e-a42b-4b81-b086-c8cdccfe3ba7
📒 Files selected for processing (3)
src/http-proxy-middleware.tstest/e2e/graceful-shutdown.spec.tstest/e2e/hono.spec.ts
Summary by CodeRabbit
Tests
Refactor