-
Notifications
You must be signed in to change notification settings - Fork 0
test: add convenient API for applying custom schema based mocks #148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Large PR Detected
This PR exceeds 1000 lines of changes and requires justification before it can be reviewed.
How to unblock this PR:
Add a section to your PR description with the following format:
## Large PR Justification
[Explain why this PR must be large, such as:]
- Generated code that cannot be split
- Large refactoring that must be atomic
- Multiple related changes that would break if separated
- Migration or data transformationAlternative:
Consider splitting this PR into smaller, focused changes (< 1000 lines each) for easier review and reduced risk.
See our Contributing Guidelines for more details on the pull request process.
This review will be automatically dismissed once you add the justification section.
|
@claude can you please do a deep review of this branch? |
|
Claude finished @kantord's task —— View job Deep Review of Mocking Improvements PR ✅OverviewThis PR introduces a sophisticated test-scoped mock override system for the MSW auto-mocker. The implementation is excellent overall, with strong TypeScript typing, good architecture, and comprehensive documentation. The changes transform the mocking system from basic fixtures into a powerful, type-safe testing framework. 🎉 Strengths1. Excellent TypeScript Strict Mode Compliance
2. Well-Architected Mock SystemThe
3. Comprehensive DocumentationThe 280-line
4. Excellent Test CoverageBoth test files demonstrate best practices:
5. Smart Fixture GenerationThe
🔍 Issues & SuggestionsCritical IssuesNone! This is high-quality code. Minor Issues & Improvements1. Biome Lint Violation (
|
Large PR justification has been provided. Thank you!
|
✅ Large PR justification has been provided. The size review has been dismissed and this PR can now proceed with normal review. |
|
can you review it again @claude? focus on finding things that might be redundant on overly verbose, and my changes in response to your previous points |
|
Claude finished @kantord's task —— View job Focused Re-Review of Mocking Improvements PR ✅Reviewing for redundancy, verbosity, and changes from previous feedback. Summary of Changes from Previous ReviewExcellent work addressing the previous feedback! Here's what changed: ✅ Previous Issues Resolved
✅ Path Aliases ResolvedBoth
🔍 Redundancy & Verbosity AnalysisCode Quality: Excellent ⭐The code is not overly verbose. It's well-structured with:
Specific Assessment1. autoAPIMock.ts - Concise & Well-Designed ✅
Potential micro-optimization (optional): // Current implementation is fine - extracting might actually reduce clarity
generatedHandler(info: ResponseResolverInfo) {
const headerScenario = info.request.headers.get(SCENARIO_HEADER);
if (headerScenario) {
const scenarioFn = scenarios.get(headerScenario as MockScenarioName);
if (scenarioFn) {
// Temporarily apply scenario logic...
}
}
// ...
}Verdict: Leave as-is. The inline approach is more readable here. 2. Documentation (docs/mocks.md) - Appropriate Length ✅280 lines might seem long, but it's actually well-justified:
Assessment: Not verbose. Each section serves a distinct purpose. The examples are comprehensive but necessary for a complex API. Minor suggestion: Consider adding a table of contents at the top for easier navigation: ## Table of Contents
- [Overview](#overview)
- [Test-Scoped Overrides](#test-scoped-overrides-with-autoapimock)
- [API Reference](#api-reference)
- [Choosing Between override and overrideHandler](#choosing-between-override-and-overridehandler)
- [Reusable Scenarios](#reusable-scenarios)
- [Global Scenario Activation](#global-scenario-activation)But this is truly optional - the current structure is clear. 3. scenarioNames.ts - Perfect ✅19 lines with clear JSDoc. Not verbose at all. 4. index.ts - Perfect ✅12 lines of clean exports. Exactly what's needed. 5. test.setup.ts - Good ✅8 lines, now has clear intent. No issues. 6. actions.test.ts - Excellent Examples ✅73 lines demonstrating all patterns. This is living documentation and not verbose - it shows:
One micro-improvement suggestion: Lines 29-36 have comments starting with "Demo:". Consider removing "Demo:" prefix as the test names are already descriptive: // Current (fine):
// Demo: using .activateScenario() for reusable test scenarios
it("returns empty array when using empty-servers scenario", async () => {
// Alternative (slightly cleaner):
// Uses .activateScenario() for reusable test scenarios
it("returns empty array when using empty-servers scenario", async () => {But this is purely stylistic - current version is perfectly acceptable. 7. Fixture File Size - Acceptable ✅The main fixture is 501 lines with 15 servers. While large, this is justified:
Observation: Only 10 lines (2%) are actual mock infrastructure (scenarios). The rest is data. This is a good ratio. 🎯 Issues FoundNone! 🎉I found zero issues with redundancy or verbosity. The code is clean, well-documented, and appropriately sized for its complexity. Optional Micro-Improvements (Not Required)These are purely stylistic suggestions - the code is excellent as-is:
📊 Comparison: Before vs After Previous Review
✅ Final VerdictStrong Approval - Ready to Merge ⭐⭐⭐⭐⭐ Strengths Reaffirmed
Code Metrics
RecommendationMerge immediately. This PR:
The two optional micro-improvements mentioned above are truly optional - they're minor style preferences that don't impact functionality or maintainability. Great work, @kantord! This is production-ready code. 🚀 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR introduces AutoAPIMock<T>, a type-safe wrapper for MSW fixtures that enables test-scoped overrides and reusable scenarios. It's a substantial refactoring of the mock infrastructure that replaces the previous header-based scenario activation with a more direct API while maintaining backward compatibility for browser-based testing.
Key changes:
- New
AutoAPIMockwrapper provides.override(),.overrideHandler(),.activateScenario(), and.scenario()methods for flexible test control - Automatic reset of all overrides between tests via
resetAllAutoAPIMocks()in test setup - Migration from standalone scenario handlers to scenario definitions within fixtures themselves
Reviewed changes
Copilot reviewed 13 out of 14 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
src/mocks/autoAPIMock.ts |
Core implementation of AutoAPIMock wrapper with scenario management and override capabilities |
src/mocks/mockTemplate.ts |
Updated template generator to use AutoAPIMock wrapper and derive consistent mock names from response types |
src/mocks/mocker.ts |
Removed Ajv validation logic; updated to load named exports from fixtures and use fixture.generatedHandler() |
src/mocks/test.setup.ts |
Added beforeEach hook to reset all AutoAPIMock instances between tests |
src/mocks/scenarioNames.ts |
Centralized scenario name definitions with TypeScript const object for autocomplete |
src/mocks/index.ts |
Public API exports for AutoAPIMock, scenario utilities, and types |
src/mocks/handlers.ts |
Removed old scenario-based handlers; scenarios now defined in fixtures |
src/mocks/mockScenario.ts |
Deleted file (old header-based scenario approach) |
src/mocks/server-detail/index.ts |
Updated to access fixture data via .defaultValue property |
src/mocks/fixtures/registry_v0_1_servers/get.ts |
Migrated to AutoAPIMock wrapper with "empty-servers" and "server-error" scenarios defined |
src/mocks/fixtures/registry_v0_1_servers__serverName__versions__version_/get.ts |
Migrated to AutoAPIMock wrapper |
src/app/catalog/actions.test.ts |
New test file demonstrating AutoAPIMock usage patterns (scenarios, overrides, error handling) |
docs/mocks.md |
Comprehensive documentation of AutoAPIMock API, override patterns, and scenario usage |
tsconfig.json |
Added @mocks path alias for convenient imports |
peppescg
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀
Summary
Introduces
AutoAPIMock<T>, a type-safe wrapper for MSW fixtures thatenables test-scoped overrides and reusable scenarios.
After this PR is merged, we can migrate the changed back to the toolhive-studio repo and migrate the existing fixtures there. So it can be considered a "pilot" of parts of the test refactoring effort in that repo.
Changes
AutoAPIMock<T>wrapper - All generated fixtures are now wrapped,providing
.override(),.overrideHandler(),.activateScenario(), and.scenario()methodsthem per-test or globally via
activateMockScenario()resetAllAutoAPIMocks()in test setupUsage
Basic overrides
Design rationale
Why a wrapper instead of separate override functions?
Why override() vs overrideHandler()?
automatically - use for 90% of cases
invalid data shapes
Why named scenarios?
Large PR Justification
The reason why this PR is large is mostly because of a lot of added documentation and because there were whitespace change in very large automatically generated mocks.