-
Notifications
You must be signed in to change notification settings - Fork 24.3k
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
Image.resolveAssetSource(require(path/${image}.png')) does not work on tests as previous version #41907
Comments
|
@idrissakhi could you add a repro? |
I have the same using react-native-fast-image, which is also using It's because in 0.73 this function
A quick fix could be:
New image mock was introduced in 0.73, before we only had : |
resolveAssetSource should not return nothing by default as some librairies (ex: react native fast image) rely on it to compute the image source without this mock implementation, all sources disappear from images in test environnement see issue facebook#41907 the input source - leaving it empty breaks
This issue is waiting for author's feedback since 24 days. Please provide the requested feedback or this will be closed in 7 days. |
This issue was closed because the author hasn't provided the requested feedback after 7 days. |
Summary: ### Context - Since RN 0.73, in the jest.setup file, methods of the Image module (Image.getSize, Image.resolveAssetSource...) are mocked on the **JS side** (introduced in #36996) - It causes issues like #41907 : `Image.resolveAssetSource` returns nothing in test env with the new JS mock, when some test relies on it. - On my project, it broke the snapshots : the URL of images disappeared. I use `react-native-fast-image` which uses `Image.resolveAssetSource` to compute URLs. - I first opened a PR to fix exclusively Image.resolveAssetSource: #41957. I will close it to focus on this new one. - As suggested by ryancat and idrissakhi, it should be better to return to the previous mock, where no method is mocked on the JS side, and we can trust the actual JS to work in test. This is what this PR intends to do. ### Content Along fixing the Image module mock in jest.setup, this PR : - adds unit test on each one of the methods, ensuring they have a consistent behavior even when the module is mocked. - adds 3 missing native mocks for `NativeImageLoader`: `prefetchImageWithMetadata`, `getSizeWithHeaders` & `queryCache`. After this PR, no method from NativeImageLoader remains unmocked. ## Changelog: [GENERAL][FIXED] - fix jest setup for Image methods (resolveAssetSource, getSize, prefetch, queryCache) Pull Request resolved: #43497 Test Plan: See exhaustive unit tests in PR. You can re-use the mock with all the methods mocked and see how the new unit tests fail. I also patched those changes on my project: my snapshot did have their URL back (see demonstrative screenshots in my original PR: #41957 - NB; fixed mock was different but result was the same -> those screenshots cover only two cases, but anyway they illustrate well the case!) Reviewed By: ryancat Differential Revision: D54959063 Pulled By: tdn120 fbshipit-source-id: 837266bd6991eb8292d9f6af1774e897ac7a8890
Do we have a plan to delegate this fix to 0.73.x, I still got a same error |
seems still present in 0.74.3 and 0.74.5. Only 0.74.4 works for me. What is going on? |
@JacobDel yes, the fix will be present in the 0.75 only (see d53cc2b) You can use this patch like - cc @ducminhleeh: diff --git a/node_modules/react-native/jest/setup.js b/node_modules/react-native/jest/setup.js
index e98550f..a829f99 100644
--- a/node_modules/react-native/jest/setup.js
+++ b/node_modules/react-native/jest/setup.js
@@ -109,17 +109,8 @@ jest
Constants: {},
},
}))
- .mock('../Libraries/Image/Image', () => {
- const Image = mockComponent('../Libraries/Image/Image');
- Image.getSize = jest.fn();
- Image.getSizeWithHeaders = jest.fn();
- Image.prefetch = jest.fn();
- Image.prefetchWithMetadata = jest.fn();
- Image.queryCache = jest.fn();
- Image.resolveAssetSource = jest.fn();
-
- return Image;
- })
+ .mock('../Libraries/Image/Image', () => mockComponent('../Libraries/Image/Image'),
+ )
.mock('../Libraries/Text/Text', () =>
mockComponent('../Libraries/Text/Text', MockNativeMethods),
) It's the one I use on 0.73.7; maybe there is some line number difference in 0.74.X, I hav'nt checked. It should not be hard to adapt, the idea is to replace the Image mock by the default one |
Description
Describe the bug
TypeError: Cannot read properties of undefined (reading 'width')Jest
Expected behavior
Tests passe on 0.72.X
Steps to Reproduce
install react native 0.73.0 and run a test having
Versions
npmPackages:
@testing-library/react-native: 12.4.1 => 12.4.1
react: 18.2.0 => 18.2.0
react-native: 0.73.0 => 0.73.0
react-test-renderer: 18.2.0 => 18.2.0
Steps to reproduce
Steps to Reproduce
install react native 0.73.0 and run a test having
React Native Version
0.73.0
Affected Platforms
Runtime - iOS
Output of
npx react-native info
Stacktrace or Logs
Reproducer
c
Screenshots and Videos
No response
The text was updated successfully, but these errors were encountered: