React-native-permissions: Test suite failed to run with Jest

Created on 30 Mar 2020  路  2Comments  路  Source: zoontek/react-native-permissions

Bug

Screen Shot 2563-03-30 at 10 34 43 AM

When running with Jest, I get the above error message.

  • If you are getting this error while unit testing you need to mock the native module. Follow the guide in the README. However, there is no instruction in the README.

This issue is very similar to #368, and I have tried all the answers but it couldn't help.

Environment info

react-native info output:

System:
    OS: macOS Mojave 10.14.6
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 12.14.1 - /usr/local/bin/node
    Yarn: 1.21.1 - /usr/local/bin/yarn
    npm: 6.13.4 - /usr/local/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  IDEs:
    Android Studio: 3.2 AI-181.5540.7.32.5056338
    Xcode: 11.0/11A420a - /usr/bin/xcodebuild
  npmPackages:
    react: 16.9.0 => 16.9.0 
    react-native: 0.61.2 => 0.61.2 
  npmGlobalPackages:
    create-react-native-app: 1.0.0
    react-native-cli: 2.0.1
    react-native-rename: 2.4.1

Library version:

  • "react-native-permissions": "^2.0.10"

  • "jest": "^25.2.4"

Steps To Reproduce

yarn test

Reproducible sample code

  • __tests__/App.test.js
import React from 'react';
import App from '../app/src/App';

import renderer from 'react-test-renderer';

test('renders correctly', () => {
  const tree = renderer.create(<App />).toJSON();
  expect(tree).toMatchSnapshot();
});
  • __mocks__/react-native-permission.js (Copy from mock.js in repo)
/* eslint-disable no-undef */

const {PERMISSIONS, RESULTS} = require('react-native-permissions/lib/commonjs/constants');
export {PERMISSIONS, RESULTS};

export const openSettings = jest.fn(async () => {});
export const check = jest.fn(async permission => RESULTS.GRANTED);
export const request = jest.fn(async permission => RESULTS.GRANTED);

const notificationOptions = [
  'alert',
  'badge',
  'sound',
  'criticalAlert',
  'carPlay',
  // 'provisional', // excluded as it's not included in NotificationSettings
];

const notificationSettings = {
  alert: true,
  badge: true,
  sound: true,
  carPlay: true,
  criticalAlert: true,
  lockScreen: true,
  notificationCenter: true,
};

export const checkNotifications = jest.fn(async () => ({
  status: RESULTS.GRANTED,
  settings: notificationSettings,
}));

export const requestNotifications = jest.fn(async options => ({
  status: RESULTS.GRANTED,
  settings: options
    .filter(option => notificationOptions.includes(option))
    .reduce((acc, option) => ({...acc, [option]: true}), {
      lockScreen: true,
      notificationCenter: true,
    }),
}));

export default {
  PERMISSIONS,
  RESULTS,
  openSettings,
  check,
  request,
  checkNotifications,
  requestNotifications,
};
  • jest.config.js
module.exports = {
  preset: 'react-native',
  transformIgnorePatterns: ['node_modules/(?!((jest-)?react-native|react-navigation|@react-navigation/.*))'],
};

Any solution?
I'm glad to share any further information if you need.

Most helpful comment

Oops, My bad! Actually, it's a bug with my eye 馃ゴ
Thanks for noticing me!

All 2 comments

__mocks__/react-native-permission.js

Hi. Is this a typo? Try __mocks__/react-native-permissions.js (with an S)

Oops, My bad! Actually, it's a bug with my eye 馃ゴ
Thanks for noticing me!

Was this page helpful?
0 / 5 - 0 ratings