refactor: module system patching for module-level mocks #49
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.
Description
The module mocking system has been rewritten to improve compatibility with different versions of React Native. Instead of fully overwriting Metro's module system, the new implementation surgically redirects responsibility for imports to Harness, allowing for better integration with various React Native versions while maintaining the same mocking capabilities. The module mocking API has been slightly modified as part of this rewrite.
Related Issue
This pull request should resolve #39.
Context
The previous implementation fully replaced Metro's module system, which could cause compatibility issues with different React Native and Metro versions. The new approach:
__d(define) function: Instead of replacing the entire module system, we wrap the factory function passed to__dto interceptrequire,importDefault, andimportAllparameters__dfunction is preserved and called with the wrapped factory, ensuring Metro's module initialization logic remains intactmyRequire,myImportDefault,myImportAll), which then falls back to Metro's original__rsystem when modules aren't mockedmock,unmock,requireActual,resetModules) remain available, some internal behavior and edge cases may have changedThis surgical approach reduces the risk of breaking changes when React Native or Metro updates their internal module system implementation, as we're only intercepting at the factory level rather than replacing core infrastructure.
Testing
resetModules()