react-nativejestjsenzymereact-native-testing-libraryjest-enzyme

How to achieve 100% in unit testcase writing for React native functional component?


I have tried to write test case using jest as matcher and React NativeTesting library as a test base. But I can't reach the 100% test coverage.

I have tried to mock the use effect and use state hooks but I won't mock that still it's asking for to cover the function inside the use effect.


Solution

  • It's good to strive for high test-coverage, but 100% is often not achievable, nor worth the effort. test-coverage also only tracks if a test has been through the code without errors, it doesn't nececarily mean that it works as you want it to. that being said, it depends on what parts of your code are not yet covered. You mention useEffect, they work asynchronously. So just mocking them won't give you the coverage you want. One way to check if the code in your use effect works is to use queryByText. Which waits until some text becomes visible and thus can check if your async code runs correctly. You of course need some text that changes so that you can check if it has. If you provide more details on the code that you want to test you could get more help, it is hard to be more specific without seeing the code.