fawaz-ahmed / react-native-read-more

React native library to show text in a condensed way and expand when needed. Drop in replacement for Text component and highly customizable. Can be used with expo or native cli for react native.
https://www.npmjs.com/package/@fawazahmed/react-native-read-more
MIT License
275 stars 37 forks source link

Unable to test my component with ReadMore imported #19

Closed Garytophatter closed 3 years ago

Garytophatter commented 3 years ago

<ReadMore> component works fine but I have trouble passing our unit tests, as shown below. I tried to mock this component but without much success. Do you know what is going on? Screen Shot 2021-02-16 at 4 46 37 PM

fawaz-ahmed commented 3 years ago

@Garytophatter I have found a solution mentioned here. In your package.json file you will see:

"jest": {
  "preset": "react-native"
}

replace it with

"jest": {
  "preset": "react-native",
  "transform": {
    "^.+\\.(js|jsx)?$": "babel-jest"
  },
  "transformIgnorePatterns": [
    "!node_modules/react-runtime"
  ]
}

Now run your tests and verify. This will resolve the import issue with jest. Give it a try! Also I'd appreciate if you can acknowledge whether it works or not.

Garytophatter commented 3 years ago

Unfortunately, I didn't see the line you mentioned in our package.json. I just saw "jest": "^24.5.0".

fawaz-ahmed commented 3 years ago

@Garytophatter I created a new react native project and I could see it. Nevertheless you can simply add this in your package.json:

"jest": {
  "preset": "react-native",
  "transform": { "^.+\\.(js|jsx)?$": "babel-jest" },
  "transformIgnorePatterns": [ "!node_modules/react-runtime" ]
}
Garytophatter commented 3 years ago

I added these lines to the package.json and yarn install, but the test is still failing. Will look into this more.

fawaz-ahmed commented 3 years ago

@Garytophatter can you share the error ? Also make sure the package babel-jest is also installed

Garytophatter commented 3 years ago

I tried mocking this component in the test file, and it ended up working. Thanks for your help!

fawaz-ahmed commented 3 years ago

That's great news @Garytophatter . Also if you can share how you managed to resolve this issue, that'll be very helpful. Thanks 👍

Garytophatter commented 3 years ago

I added this line to mock the component and resolved the issue:

  jest.mock('@fawazahmed/react-native-read-more', () => ({
     ReadMore: jest.fn(),
  }));