DEV Community

Cover image for Testing React Native app includes react-native-cookies with Jest
DIGITAL SQUAD
DIGITAL SQUAD

Posted on

Testing React Native app includes react-native-cookies with Jest

When got an error below.

Test suite failed to run

    Invariant Violation: react-native-cookies: Add RNCookieManagerIOS.h and RNCookieManagerIOS.m to your Xcode project

      1 | import { Alert, AsyncStorage, FlatList, Platform, StyleSheet, Text, View } from 'react-native'
    > 2 | import CookieManager from 'react-native-cookies'
        |                                                              ^
Enter fullscreen mode Exit fullscreen mode

Add setup.js anywhere on project root directory or somewhere.

jest.mock('react-native-cookies', () => {
  return {
    addEventListener: jest.fn(),
    removeEventListener: jest.fn(),
    openURL: jest.fn(),
    canOpenURL: jest.fn(),
    getInitialURL: jest.fn(),
  }
})
Enter fullscreen mode Exit fullscreen mode

Modify package.json.

 "jest": {
    "preset": "react-native",
    "moduleDirectories": [
      "node_modules",
      "src",
      "test"
    ],
    "setupFiles": [
      "<rootDir>/setup.js"
    ]
  }
Enter fullscreen mode Exit fullscreen mode

'Invariant Violation' exception when running tests

Top comments (0)