Skip to content

Fix issue #109: Correct Promise type unwrapping for mockResolvedValue#112

Open
timkindberg wants to merge 9 commits intomasterfrom
claude/fix-issue-109-mk76cnhgr4r90le4-a3gBU
Open

Fix issue #109: Correct Promise type unwrapping for mockResolvedValue#112
timkindberg wants to merge 9 commits intomasterfrom
claude/fix-issue-109-mk76cnhgr4r90le4-a3gBU

Conversation

@timkindberg
Copy link
Owner

No description provided.

timkindberg and others added 9 commits September 4, 2025 21:48
🎯 **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 ✓
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants