feat(sdk): implement mock RPC server for unit tests#264
feat(sdk): implement mock RPC server for unit tests#264mandyslovestories-sudo wants to merge 1 commit into
Conversation
…l#182) Replaces the minimal stub in test-utils/mockRpcServer.ts with a fully-typed, reusable mock that covers every Soroban RPC method used by the SDK. Tests no longer depend on any network connection. Changes: - Expand mockRpcServer.ts to mock all 9 RPC methods: getAccount, simulateTransaction, sendTransaction, getTransaction, getNetwork, getLatestLedger, getLedger, getFeeStats, getEvents - Add 12 pre-canned scenario helpers (rpc.scenarios.*) for the most common test situations (success, pendingThenSuccess, simulationError, transactionFailed, accountNotFound, networkError, etc.) - Intercept both @stellar/stellar-sdk/rpc and @stellar/stellar-sdk import paths via vi.mock() so all SDK modules are covered - Add resetMockRpcServer() that clears call history and return values for clean test isolation in beforeEach - Add mockRpcServer.test.ts with 49 tests covering the mock contract - Fix missing mockTxNone helper in DistributorClient.test.ts and PaymentStreamClient.test.ts (caused ReferenceError at runtime) Closes Fundable-Protocol#182
|
@mandyslovestories-sudo Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
📝 WalkthroughWalkthroughAdds a reusable mock RPC server for SDK unit tests, with typed response shapes, preset scenarios, module-level SDK/RPC mocks, and coverage for those behaviors. Two client tests also gain helpers for transaction mocks that return undefined results. ChangesUndefined transaction result helpers
Reusable mock RPC server
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
packages/sdk/src/__tests__/DistributorClient.test.tsParsing error: Cannot read file '/tsconfig.json'. packages/sdk/src/__tests__/PaymentStreamClient.test.tsParsing error: Cannot read file '/tsconfig.json'. packages/sdk/src/__tests__/mockRpcServer.test.tsParsing error: Cannot read file '/tsconfig.json'.
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 |
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 `@packages/sdk/src/test-utils/mockRpcServer.ts`:
- Around line 444-452: resetMockRpcServer() is skipping vi.fn RPC method mocks
because Object.values(mock) returns functions, but the current guard only resets
objects. Update the resetMockRpcServer logic in mockRpcServer to also detect
function values (the RPC method mocks like getAccount and getTransaction) and
call mockReset on them, while still excluding the scenarios container so call
history and return values are cleared between tests.
🪄 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: defaults
Review profile: CHILL
Plan: Pro
Run ID: fd56a0aa-ba2b-479e-ae7e-2a7fa9339058
📒 Files selected for processing (4)
packages/sdk/src/__tests__/DistributorClient.test.tspackages/sdk/src/__tests__/PaymentStreamClient.test.tspackages/sdk/src/__tests__/mockRpcServer.test.tspackages/sdk/src/test-utils/mockRpcServer.ts
Summary
Closes #182
Replaces the minimal 5-method stub in
test-utils/mockRpcServer.tswith a fully-typed, reusable mock that covers every Soroban RPC method used across the SDK. Unit tests no longer have any network dependency.Changes
src/test-utils/mockRpcServer.ts(rewrite)vi.fn():getAccount,simulateTransaction,sendTransaction,getTransaction,getNetwork,getLatestLedger,getLedger,getFeeStats,getEventsrpc.scenarios.*helpers for common situations:success,pendingThenSuccess,notFoundThenSuccess,transactionFailed,simulationError,simulationNetworkError,sendTransactionError,accountNotFound,networkError,highCongestionFeeStats,contractEvents,getEventsError@stellar/stellar-sdk/rpcand@stellar/stellar-sdkimport paths viavi.mock()so all SDK modules are coveredresetMockRpcServer()clears call history and return values for cleanbeforeEachisolationsrc/__tests__/mockRpcServer.test.ts(new)Apinamespace, all scenario helpers, reset behaviour, and per-test override compositionBug fixes
DistributorClient.test.ts— add missingmockTxNonehelper (3 tests were calling it but it was never defined, would have thrownReferenceErrorat runtime)PaymentStreamClient.test.ts— same fix (1 test affected)Testing
All existing tests continue to pass. The new
mockRpcServer.test.tsadds 49 additional tests.Summary by CodeRabbit