Skip to content
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

fix: Use .mock extension for MMKV mocks #647

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Mister-CK
Copy link

The issue I was having that when I was running metro in mock mode. isJest() returned false, so the createMMKV from the regular file was imported, but since I was running in mock mode. That file didn't exist, so createMMKV was undefined.

Maybe I am missing something, but .mock files are supposed to replace their regular counterparts when running in mock mode. It doesn't make much sense to me to import from both and have a check to determine which to use. Instead, metro should pick the one that is currently relevant. This is achieved by giving the functions the same name and only importing from the regular one. the .mock one is used when running in mock mode.
For jest to use the mock I have added jest.mock in the unit test. This feels like a more appropriate way to add a mock then changing the imports in the actual code.

Alternatively, we could add a .mock for that platform checker that always returns true, that is how I initially patched it and a smaller change.

I am not sure what the .web variant is for, I didn't want to remove it, since it is unrelated, but it does not appear to be used anywhere.

This is my first PR to this repo, please let me know if I should do something else or in a different way.

Chris Kapinga added 2 commits March 11, 2024 23:50
.mock files are supposed to replace their regular counterparts when running in mock mode. It doesn't make much sense to import from both and have a check to determine which to use. Instead, metro  should pick the one that is currently relevant. This is achieved by giving the functions the same name and only importing from the regular one. the .mock one is used when running in mock mode.
jest doesn't pick the .mock file by default. adding jest.mock in the test file solves this.

If there are more testFiles, the mock can be made globally in a jest.setup
@mrousavy mrousavy changed the title Fix/use mock based on running config fix: Use .mock extension for MMKV mocks Mar 12, 2024
@mrousavy
Copy link
Owner

Thanks for your PR, this looks good to me! I'll do some tests to confirm.

The .mock files will be compiled out I assume?

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.

None yet

2 participants