moaazsidat / react-native-qrcode-scanner

A QR code scanner component for React Native.
MIT License
2.04k stars 516 forks source link

Test fails due to react-native-permissions #372

Closed rajAmukhliS closed 2 years ago

rajAmukhliS commented 2 years ago

Discussed in https://github.com/moaazsidat/react-native-qrcode-scanner/discussions/371

Originally posted by **rajAmukhliS** February 9, 2022 ### Test fails due to react-native-permissions **Output:** react-native-permissions: NativeModule.RNPermissions is null. To fix this issue try these steps: • If you are using CocoaPods on iOS, run `pod install` in the `ios` directory and then clean, rebuild and re-run the app. You may also need to re-open Xcode to get the new pods. • If you are getting this error while unit testing you need to mock the native module. You can use this to get started: https://github.com/react-native-community/react-native-permissions/blob/master/mock.js If none of these fix the issue, please open an issue on the Github repository: https://github.com/react-native-community/react-native-permissions at Object. (node_modules/react-native-qrcode-scanner/node_modules/react-native-permissions/lib/commonjs/index.ts:6:9) at Object. (node_modules/react-native-qrcode-scanner/index.js:19:1) **Plateform:** both ios and android _before installation of `react-native-qrcode-scanner` and `react-native-permissions` all tests were ok and working perfect here is my output attached the error I am facing_ I have also mocked `react-native-permissions `in **jest.setup.js** file as shown below , still its not working ``` jest.mock('react-native-permissions', () => require('react-native-permissions/mock'), ); ``` at time of installation i ignored ios configrations required for this pakage because i am working on ubuntu for android only shall be thankfull for your help
rajAmukhliS commented 2 years ago

I have found 2 ways to get rid of this issue

  1. mock react-native-qrcode-scanner inside the test file , this is solution for single file as shown below write mock of react-native-qrcode-scanner on top of test file after imports

jest.mock('react-native-qrcode-scanner', () => jest.fn());

  1. mock react-native-qrcode-scanner inside the jest.setup.js file , this is solution for all tests files as shown below write mock of react-native-qrcode-scanner inside jest.setup.js file in root dir as shown below

jest.mock( 'react-native-qrcode-scanner/node_modules/react-native-permissions', () => require('react-native-permissions/mock'), );

moaazsidat commented 2 years ago

Do you think one of them is better than the other? If so, please consider documenting and submitting a pull request to improve the readme, thanks 🙏