🐛 Fixed flaky unit tests caused by DNS stubs lost after sinon.restore()#27168
🐛 Fixed flaky unit tests caused by DNS stubs lost after sinon.restore()#27168
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (5)
💤 Files with no reviewable changes (2)
✅ Files skipped from review due to trivial changes (2)
🚧 Files skipped from review as they are similar to previous changes (1)
WalkthroughRemoved test-level DNS stubs from several unit tests: request-external, ghost-mailer, and mention-discovery-service no longer import or stub Node DNS. The mention-sending-service test now stubs 🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@ghost/core/test/utils/overrides.js`:
- Around line 106-115: The afterEach hook currently calls originalAfterEach()
and then mockManager.disableNetwork(), but if originalAfterEach() throws the
disableNetwork call is skipped; update the logic in the afterEach override
(where originalAfterEach is invoked) to call originalAfterEach() inside a
try/finally and move mockManager.disableNetwork() into the finally block so
disableNetwork always runs even when originalAfterEach throws.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: d2876cbd-cfed-4d8a-a0eb-1411bdb282e4
📒 Files selected for processing (5)
ghost/core/test/unit/server/lib/request-external.test.jsghost/core/test/unit/server/services/mail/ghost-mailer.test.jsghost/core/test/unit/server/services/mentions/mention-discovery-service.test.jsghost/core/test/unit/server/services/mentions/mention-sending-service.test.jsghost/core/test/utils/overrides.js
💤 Files with no reviewable changes (2)
- ghost/core/test/unit/server/lib/request-external.test.js
- ghost/core/test/unit/server/services/mail/ghost-mailer.test.js
| if (originalAfterEach) { | ||
| await originalAfterEach(); | ||
| } | ||
|
|
||
| // Re-apply network disabling after each test. Individual test afterEach hooks | ||
| // often call sinon.restore() which removes the DNS stubs set up by | ||
| // disableNetwork() in beforeAll. Without re-applying, subsequent tests make | ||
| // real DNS lookups on nocked domains which can be slow on CI, causing flaky | ||
| // timeouts (e.g. ExternalMediaInliner CDN storage adapter tests). | ||
| mockManager.disableNetwork(); |
There was a problem hiding this comment.
Harden re-stubbing with finally to avoid fallback to real DNS on hook failures.
If originalAfterEach() throws, mockManager.disableNetwork() is skipped, which can reintroduce cross-test DNS flakiness.
Proposed fix
- if (originalAfterEach) {
- await originalAfterEach();
- }
-
- // Re-apply network disabling after each test. Individual test afterEach hooks
- // often call sinon.restore() which removes the DNS stubs set up by
- // disableNetwork() in beforeAll. Without re-applying, subsequent tests make
- // real DNS lookups on nocked domains which can be slow on CI, causing flaky
- // timeouts (e.g. ExternalMediaInliner CDN storage adapter tests).
- mockManager.disableNetwork();
+ try {
+ if (originalAfterEach) {
+ await originalAfterEach();
+ }
+ } finally {
+ // Re-apply network disabling after each test. Individual test afterEach hooks
+ // often call sinon.restore() which removes the DNS stubs set up by
+ // disableNetwork() in beforeAll. Without re-applying, subsequent tests make
+ // real DNS lookups on nocked domains which can be slow on CI, causing flaky
+ // timeouts (e.g. ExternalMediaInliner CDN storage adapter tests).
+ mockManager.disableNetwork();
+ }📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if (originalAfterEach) { | |
| await originalAfterEach(); | |
| } | |
| // Re-apply network disabling after each test. Individual test afterEach hooks | |
| // often call sinon.restore() which removes the DNS stubs set up by | |
| // disableNetwork() in beforeAll. Without re-applying, subsequent tests make | |
| // real DNS lookups on nocked domains which can be slow on CI, causing flaky | |
| // timeouts (e.g. ExternalMediaInliner CDN storage adapter tests). | |
| mockManager.disableNetwork(); | |
| try { | |
| if (originalAfterEach) { | |
| await originalAfterEach(); | |
| } | |
| } finally { | |
| // Re-apply network disabling after each test. Individual test afterEach hooks | |
| // often call sinon.restore() which removes the DNS stubs set up by | |
| // disableNetwork() in beforeAll. Without re-applying, subsequent tests make | |
| // real DNS lookups on nocked domains which can be slow on CI, causing flaky | |
| // timeouts (e.g. ExternalMediaInliner CDN storage adapter tests). | |
| mockManager.disableNetwork(); | |
| } |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@ghost/core/test/utils/overrides.js` around lines 106 - 115, The afterEach
hook currently calls originalAfterEach() and then mockManager.disableNetwork(),
but if originalAfterEach() throws the disableNetwork call is skipped; update the
logic in the afterEach override (where originalAfterEach is invoked) to call
originalAfterEach() inside a try/finally and move mockManager.disableNetwork()
into the finally block so disableNetwork always runs even when originalAfterEach
throws.
ref https://github.com/TryGhost/Ghost/actions/runs/24012086446/job/70025449313 The global disableNetwork() helper stubs dnsPromises.lookup via sinon during beforeAll to prevent real DNS lookups in tests. However, many tests call sinon.restore() in their afterEach, which nukes all sinon stubs — including the DNS one. Subsequent tests then perform real DNS lookups against nocked domains (e.g. external.com), which can be slow or unreliable on CI, causing intermittent 2000ms timeouts. This was the root cause of the flaky ExternalMediaInliner CDN storage adapter test failures. The fix re-applies disableNetwork() in the global mocha root afterEach hook, which runs after every test's own afterEach. This ensures DNS stubs are always restored after sinon.restore() removes them. Three tests that were redundantly re-stubbing DNS with the same fake values are cleaned up, and one test that was silently depending on the stub being absent (to get a real private-IP DNS result) is updated to explicitly stub the behavior it needs. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
7574deb to
113c166
Compare
|



Summary
disableNetwork()helper stubsdnsPromises.lookupvia sinon duringbeforeAllto prevent real DNS lookups in testssinon.restore()in theirafterEach, which removes all sinon stubs — including the DNS oneexternal.com), which can be slow or unreliable on CI, causing intermittent 2000ms timeoutsExternalMediaInlinerCDN storage adapter test failures seen in https://github.com/TryGhost/Ghost/actions/runs/24012086446/job/70025449313What changed
test/utils/overrides.js— Re-applydisableNetwork()in the global mocha rootafterEachhook, so DNS stubs are always present for the next testdisableNetwork()already provides (request-external.test.js,ghost-mailer.test.js,mention-discovery-service.test.js)mention-sending-service.test.js— Fixed a test that was silently depending on the DNS stub being absent (so real DNS would resolvedomaincontrol.com→127.0.0.1). Now explicitly stubs DNS to return a private IP, making it deterministicTest plan
external-media-inlinertests passrequest-external,ghost-mailer,mention-discovery-service,mention-sending-servicetests passyarn test:unitsuite passes (only pre-existing Node version mismatch failure)🤖 Generated with Claude Code