callstack / react-native-testing-library

🦉 Simple and complete React Native testing utilities that encourage good testing practices.
https://callstack.github.io/react-native-testing-library/
MIT License
3.07k stars 271 forks source link

[react native 0.73] - require does not work #1542

Closed idrissakhi closed 9 months ago

idrissakhi commented 9 months ago

Describe the bug

const emailMailImage = require('assets/images/email-mail/mail.png');

const emailMailImageDimensions = Image.resolveAssetSource(emailMailImage);

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

            source={emailMailImage}
            resizeMode={'contain'}
            style={[
              styles.emailSentImage,
              {
                width: resizeEmailImageWidth,
                aspectRatio: emailMailImageDimensions.width / emailMailImageDimensions.height,
              },
            ]}
          />

Screenshots

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

thymikee commented 9 months ago

You'll likely need to mock Image.resolveAssetSource method to return something

idrissakhi commented 9 months ago

@thymikee but it was working fine on previous versions, also i saw the new react native changelog and no breaking change is introduced in this part

thymikee commented 9 months ago

It's possible it was unintended breaking change. To unblock yourself you could do something like:

Image.resolveAssetSource = jest.fn(() => ({width: 128, height: 128}));

or use jest.mock/jest.spyOn if jest.fn won't work here

mdjastrzebski commented 9 months ago

Closing as it appears to be RN issue, not a RNTL one.

jforaker commented 2 months ago

Future googlers, @thymikee's solution worked in 2024 for react-native 0.73.0 and @testing-library/react-native@12.5.2