refactor: rework how nock is setup and used to harden tests#395
refactor: rework how nock is setup and used to harden tests#395kamilogorek wants to merge 4 commits into
Conversation
|
Important Review skippedAuto incremental reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
WalkthroughTest infrastructure refactoring centralizes Nock mock lifecycle management by moving scope-tracking and cleanup logic into global setup, then simplifies all test assertions to rely on direct result checks instead of explicit mock-consumption verification. ChangesNock Mock Lifecycle Refactoring
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Suggested reviewers
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 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 |
|
@kamilogorek Do you mind resolving the conflicts in the test files in this one, Happy to take over otherwise :) |
b940eb6 to
c4c9c23
Compare
|
Rebased. All tests pass locally 🙇 |
There was a problem hiding this comment.
Pull request overview
This PR refactors the test suite’s nock usage to make network-mocked tests more robust by centralizing setup/teardown and enforcing that all declared mocks are either consumed or explicitly cleaned up.
Changes:
- Centralize
nockconfiguration intests/setup.tsand add a globalafterEachassertion that fails tests when pending mocks remain. - Remove many per-test
scope.isDone()assertions and repeatednock.cleanAll()hooks, relying on the global pending-mock assertion instead. - Simplify
nocklifecycle handling in individual test files (e.g., removing customrestore/activateblocks).
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/setup.ts | Adds global nock net-connect configuration and a pending-mock assertion/cleanup in afterEach. |
| tests/index.test.ts | Removes manual scope assertions/cleanup and relies on global pending-mock checks (with a few explicit cleanups where mocks must remain unused). |
| tests/headers.test.ts | Removes per-test nock cleanup and scope.isDone() assertions in favor of global enforcement. |
| tests/credentials.test.ts | Drops local nock setup/teardown and scope.isDone() assertions, relying on the centralized setup. |
| tests/client.test.ts | Removes repeated nock.cleanAll() hooks and manual scope assertions; adds targeted cleanup where tests intentionally leave mocks unused. |
| tests/apiExecutor.test.ts | Removes custom nock lifecycle management and aligns imports/types with the updated test approach. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@tests/setup.ts`:
- Line 10: The nock.allow rule currently uses startsWith with
nock.enableNetConnect which allows spoofed hostnames; change the check in the
nock.enableNetConnect callback (the function passed to nock.enableNetConnect) to
compare the exact hostname portion against OPENFGA_API_URL_HOST (for example by
extracting host.split(':')[0] or otherwise parsing hostname) so only an exact
match to OPENFGA_API_URL_HOST is allowed rather than startsWith.
🪄 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: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: dca92d14-a484-49b9-831e-c16c2cc59df0
📒 Files selected for processing (6)
tests/apiExecutor.test.tstests/client.test.tstests/credentials.test.tstests/headers.test.tstests/index.test.tstests/setup.ts
SoulPancake
left a comment
There was a problem hiding this comment.
I guess this this re-introduces a strange issue we had with nocks that fails only in CI but passes locally, need to investigate
#312
This reverts commit 7bbf439.
|
Should be good now. Not sure why nock's interception is behaving different way locally vs in CI 🤔 |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #395 +/- ##
=======================================
Coverage 85.74% 85.74%
=======================================
Files 25 25
Lines 1305 1305
Branches 265 265
=======================================
Hits 1119 1119
Misses 108 108
Partials 78 78 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This PR hardens everything around
nockusage for network-based tests.nockrequestsscopeassertionsNOTE: will rebase once #394 is mergedSummary by CodeRabbit