Fix issue #109: Correct Promise type unwrapping for mockResolvedValue#112
Open
timkindberg wants to merge 9 commits intomasterfrom
Open
Fix issue #109: Correct Promise type unwrapping for mockResolvedValue#112timkindberg wants to merge 9 commits intomasterfrom
timkindberg wants to merge 9 commits intomasterfrom
Conversation
🎯 **Complete TypeScript Migration** **Core Changes:** - Convert src/when.js → src/when.ts with comprehensive type definitions - Convert src/when.test.js → src/when.test.ts with proper typing - Add TypeScript compilation and build configuration - Update package.json for TypeScript support with proper entry points - Set up npm publishing workflow with compiled output in dist/ **Type System Improvements:** - Implement type guards and proper generic inference - Add Jest type extensions for seamless integration - Zero 'as jest.Mock' casting required - excellent type inference - Full IntelliSense support for all jest-when methods - Proper generic type inference for return values - Seamless integration with both jest.Mock and jest.SpyInstance **Build & Publishing:** - Add comprehensive build verification system - Configure TypeScript compilation with source maps - Set up proper npm publishing with dist/ output - Add pre-publish validation pipeline - Include type declarations (when.d.ts) for TypeScript users **Quality Assurance:** - All 88 tests passing (100% success rate) - Fixed test isolation issues with proper cleanup - Enhanced type safety with 'unknown' instead of 'any' - Improved error handling and type guards - Comprehensive build validation scripts **Developer Experience:** - Excellent type inference without explicit casting - Full IntelliSense support - Proper generic type preservation - Source maps for debugging - Clean, maintainable codebase **Backwards Compatibility:** - Maintains 100% backwards compatibility with existing JavaScript users - No breaking changes to the public API - Existing code continues to work unchanged **Technical Details:** - TypeScript 5.9.2 with strict mode enabled - CommonJS output for maximum compatibility - Source maps for debugging support - Comprehensive type declarations - Build verification pipeline Breaking changes: None - fully backwards compatible
This commit fixes the TypeScript type inference issue where mockResolvedValue and related methods were incorrectly requiring Promise<T> instead of T. Changes: - Added Unpromisify<T> utility type to unwrap Promise types - Updated mockResolvedValue to accept Unpromisify<TReturn> instead of TReturn - Updated mockResolvedValueOnce with the same fix - Updated defaultResolvedValue with the same fix - Applied changes to all mockFunctions implementations in: - WhenMock constructor - calledWith method - expectCalledWith method - _mockImplementation method Tests: - Added 9 comprehensive tests for issue #109 covering: - Basic Promise type inference with jest.fn - mockResolvedValue and mockResolvedValueOnce - mockRejectedValue and mockRejectedValueOnce - defaultResolvedValue - jest.spyOn with Promise return types - Complex object types in Promises - Chained calls mixing resolved and rejected values All 97 tests passing (88 original + 9 new).
Added type tests using tsd to verify TypeScript compiler behavior: - Ensures mockResolvedValue accepts unwrapped Promise types - Verifies type inference with jest.fn, jest.spyOn, and complex types - Tests chaining, defaultResolvedValue, and mockRejectedValue - Validates non-Promise types still work correctly New scripts: - npm run test:types - Run type tests with tsd - npm run test:all - Run both runtime and type tests All type tests passing ✓
- Use expectError to verify TypeScript rejects incorrect types - Test that mockResolvedValue rejects Promise-wrapped values - Test that wrong types are properly rejected - Verify nested Promises only unwrap one level - Test union types, complex objects, and jest.spyOn - All assertions now actually validate type behavior All type tests passing ✓
The when function implementation had explicit type parameters that didn't match the WhenFunction interface overloads, causing TS2322 errors. Fixed by simplifying the implementation signature to use 'any' and letting the WhenFunction interface handle type checking through its overloads. All tests passing: - 97 runtime tests ✓ - All type tests ✓
…lity
Changed from:
export const when: WhenFunction = (fn: any): any => {...}
To:
export const when = ((fn: any): any => {...}) as WhenFunction;
This approach is more compatible with strict IDE type checking and should
resolve TS2322 errors that some IDEs report.
All tests passing:
- 97 runtime tests ✓
- All type tests ✓
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.