Skip to content
This repository has been archived by the owner on Jan 24, 2020. It is now read-only.

'Invariant Violation' exception when running tests #16

Closed
jdzak opened this issue Mar 22, 2016 · 15 comments
Closed

'Invariant Violation' exception when running tests #16

jdzak opened this issue Mar 22, 2016 · 15 comments

Comments

@jdzak
Copy link

jdzak commented Mar 22, 2016

I'm running into an issue while running my test suite. I get an 'Invariant Violation' exception on a line that does require('react-native-cookies').

Here's my stacktrace.

/Users/tablexi/Code/Project/node_modules/invariant/invariant.js:49
    throw error;
    ^

Invariant Violation: Add RNCookieMangerIOS.h and RNCookieManagerIOS.m to your Xcode project
    at invariant (/Users/tablexi/Code/Project/node_modules/invariant/invariant.js:42:15)
    at Object.<anonymous> (/Users/tablexi/Code/Project/node_modules/react-native-cookies/index.js:10:5)
    at Module._compile (module.js:413:34)
    at Module._extensions..js (module.js:422:10)
    at require.extensions.(anonymous function) (/Users/tablexi/Code/Project/node_modules/babel-core/node_modules/babel-register/lib/node.js:134:7)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/tablexi/Code/Project/node_modules/babel-register/lib/node.js:138:7)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Module.require (module.js:367:17)
    at require (internal/module.js:16:19)
    at getCookieManager (UserSessionRequest.js:9:21)
    at new UserSessionRequest (UserSessionRequest.js:14:59)
    at Object.<anonymous> (Logout.js:43:38)
    at Module._compile (module.js:413:34)
    at loader (/Users/tablexi/Code/Project/node_modules/babel-register/lib/node.js:130:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/tablexi/Code/Project/node_modules/babel-register/lib/node.js:140:7)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Module.require (module.js:367:17)
    at require (internal/module.js:16:19)
    at Object.<anonymous> (Dashboard.js:8:1)
    at Module._compile (module.js:413:34)
    at loader (/Users/tablexi/Code/Project/node_modules/babel-register/lib/node.js:130:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/tablexi/Code/Project/node_modules/babel-register/lib/node.js:140:7)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Module.require (module.js:367:17)
    at require (internal/module.js:16:19)
    at Object.<anonymous> (Dashboard.test.js:3:1)
    at Module._compile (module.js:413:34)
    at loader (/Users/tablexi/Code/Project/node_modules/babel-register/lib/node.js:130:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/tablexi/Code/Project/node_modules/babel-register/lib/node.js:140:7)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Module.require (module.js:367:17)
    at require (internal/module.js:16:19)
    at /Users/tablexi/Code/Project/node_modules/mocha/lib/mocha.js:219:27
    at Array.forEach (native)
    at Mocha.loadFiles (/Users/tablexi/Code/Project/node_modules/mocha/lib/mocha.js:216:14)
    at Mocha.run (/Users/tablexi/Code/Project/node_modules/mocha/lib/mocha.js:468:10)
    at Object.<anonymous> (/Users/tablexi/Code/Project/node_modules/mocha/bin/_mocha:403:18)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at startup (node.js:141:18)
    at node.js:933:3
npm ERR! Test failed.  See above for more details.

And here's my package.json

{
  "name": "ProjectReactNative",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "lint": "eslint '{app,test}/**/*.js' --cache --fix",
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "mocha"
  },
  "dependencies": {
    "react-native": "^0.21.0",
    "react-native-cookies": "0.0.5",
    "react-native-radio-buttons": "^0.9.1",
    "react-native-scrollable-tab-view": "^0.4.0"
  },
  "devDependencies": {
    "babel": "^6.3.26",
    "babel-core": "^6.7.2",
    "babel-eslint": "^6.0.0-beta.5",
    "babel-jest": "^6.0.1",
    "babel-plugin-syntax-async-functions": "^6.5.0",
    "babel-plugin-syntax-class-properties": "^6.5.0",
    "babel-plugin-syntax-trailing-function-commas": "^6.5.0",
    "babel-plugin-transform-class-properties": "^6.6.0",
    "babel-plugin-transform-es2015-arrow-functions": "^6.5.2",
    "babel-plugin-transform-es2015-block-scoping": "^6.7.1",
    "babel-plugin-transform-es2015-classes": "^6.6.5",
    "babel-plugin-transform-es2015-computed-properties": "^6.6.5",
    "babel-plugin-transform-es2015-constants": "^6.1.4",
    "babel-plugin-transform-es2015-destructuring": "^6.6.5",
    "babel-plugin-transform-es2015-for-of": "^6.6.0",
    "babel-plugin-transform-es2015-modules-commonjs": "^6.7.0",
    "babel-plugin-transform-es2015-parameters": "^6.7.0",
    "babel-plugin-transform-es2015-shorthand-properties": "^6.5.0",
    "babel-plugin-transform-es2015-spread": "^6.6.5",
    "babel-plugin-transform-es2015-template-literals": "^6.6.5",
    "babel-plugin-transform-flow-strip-types": "^6.7.0",
    "babel-plugin-transform-object-assign": "^6.5.0",
    "babel-plugin-transform-object-rest-spread": "^6.6.5",
    "babel-plugin-transform-react-display-name": "^6.5.0",
    "babel-plugin-transform-react-jsx": "^6.6.5",
    "babel-plugin-transform-regenerator": "^6.6.5",
    "babel-preset-airbnb": "^1.0.1",
    "chai": "^3.4.1",
    "chai-as-promised": "^5.2.0",
    "enzyme": "^2.2.0",
    "eslint": "~2.2.0",
    "eslint-config-standard": "^5.1.0",
    "eslint-plugin-promise": "^1.1.0",
    "eslint-plugin-react": "^4.2.1",
    "eslint-plugin-standard": "^1.3.2",
    "mocha": "^2.3.4",
    "mockery": "^1.4.1",
    "react-addons-test-utils": "^0.14.7",
    "react-dom": "^0.14.7",
    "react-native-mock": "^0.0.6",
    "sinon": "^1.17.3"
  }
}
@rseemann
Copy link

+1

@rseemann
Copy link

Solved it - add this to your testHelper file:

const NativeModules = React.NativeModules;

NativeModules.RNCookieManagerIOS = {};
NativeModules.RNCookieManagerAndroid = {};

@emmby
Copy link

emmby commented Jun 28, 2016

What do you mean by a testHelper file?

EDIT: http://stackoverflow.com/a/20780657/82156 is handy here

@rseemann
Copy link

rseemann commented Jul 3, 2016

@emmby the file that is loaded as the first required when testin

@fistuk
Copy link

fistuk commented Mar 29, 2017

Could you share the location and the exact content of your testHelper file please?

@carlosconnected
Copy link

@rseemann Can you give more context about the tsetHelper you mentioned? Is there a link that could help?

@carlosconnected
Copy link

I solved it by mocking the react-native-cookies module on my jest tests. I only clear cookies to let met token based connections don't get mess up. I mock network calls on my jests anyways, so this approach works for me. For more info on jest mocks: https://facebook.github.io/jest/docs/manual-mocks.html

@rseemann
Copy link

@carlos-peru spec/testHelper.js

@natterstefan
Copy link

Hi @carlos-peru do you have a gist for me, showing me how you mock the react-native-cookies module? Thank you!

@jayesbe
Copy link

jayesbe commented Nov 1, 2017

@carlos-peru a gist would be very helpful

@Sunshine168
Copy link

Any new Idea?

@Sunshine168
Copy link

mine solution
in package.json

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

in setup.js


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

@natterstefan
Copy link

@Sunshine168 so this works for you now? :)

@simonarcher
Copy link

Im still unable to get this to work.

When I add the above setup.js suggestion, i get the following error now..

TypeError: _reactNativeCookies2.default.get is not a function

I then would add get: jest.fn() to the react-native-cookies helper mock, but then I get the error

TypeError: Cannot read property 'then' of undefined

Anybody able to assist in getting this work with Jest testing?

@evan-007
Copy link

@simonarcher the react-native-cookies methods return promises so something like get: () => Promise.resolve(null) in your mock should work, that's how i ended up doing it

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests