fawaz-ahmed/react-native-read-more

Unable to test my component with ReadMore imported

Garytophatter opened this issue · 8 comments

<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

@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.

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

@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" ]
}

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

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

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

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

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

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