Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS Bridge Error - Cannot read property SECURITY_LEVEL_ANY of undefined #192

Closed
fpauer opened this issue Feb 13, 2019 · 25 comments · Fixed by #230
Closed

JS Bridge Error - Cannot read property SECURITY_LEVEL_ANY of undefined #192

fpauer opened this issue Feb 13, 2019 · 25 comments · Fixed by #230

Comments

@fpauer
Copy link

fpauer commented Feb 13, 2019

I've just got the latest versions 3.1.0 and 3.1.1 and unit tests started to fail

● Test suite failed to run

TypeError: Cannot read property 'SECURITY_LEVEL_ANY' of undefined



  at Object.<anonymous> (node_modules/react-native-keychain/index.js:6:26)
@fpauer
Copy link
Author

fpauer commented Feb 13, 2019

Added a global jest setup and mocked the entire Native Module:

jest.mock('react-native-keychain', () => ({
  setGenericPassword: jest.fn(),
  getGenericPassword: jest.fn(),
  resetGenericPassword: jest.fn()
}));

@oblador
Copy link
Owner

oblador commented Feb 13, 2019

Ping @mandrigin. Not sure we want to fix this.

@GraemeAllanBryce
Copy link

This is breaking a production build for me. The dependency comes from @haskkor/react-native-pincode

I am confused because it appears to have been fine before.
What is the cause?

@mandrigin
Copy link
Collaborator

It looks like it doesn't load native modules for some reason. But if it doesn't then the whole library is kind of useless. I probably need to learn more how exactly you are building the app.

@GraemeAllanBryce
Copy link

It is a react-native app.
We use YARN for dependency management
Build in xCode or vsCode essentially comes dow to a react-native ios:build command

Thing is I have it all working fine on one branch with PINCode working fine and on another it is failing. Currently investigating yarn.lock but can't really see what the significant difference is between the two builds.

I will post back here once we nail it.

Anyy suggestions as to where we should be looking will be well received as, for now, its a puzzle!

@mandrigin
Copy link
Collaborator

Thanks!
This code was introduced in 3.1.0, so before that you can't even have this issue. Yarn should theoretically lock the dependencies...

@vonovak
Copy link
Collaborator

vonovak commented Feb 20, 2019

the cause is here

export const SECURITY_LEVEL = Object.freeze({

you need to mock the constants exported from the native module for your JS tests

This does not explain though why builds suddenly stopped working for you. Are you sure you didn't upgrade?

@marciok
Copy link

marciok commented Feb 26, 2019

jest.mock('react-native-keychain', () => ({
setGenericPassword: jest.fn(),
getGenericPassword: jest.fn(),
resetGenericPassword: jest.fn()
}));

Were you able to mock?? I tried to mock the whole class as you described, but it's still throwing the SECURITY_LEVEL_ANY error...

@fpauer
Copy link
Author

fpauer commented Feb 26, 2019

@marciok Yes, but you need to include it in your jest setup file and in package.json define the setup file path.

"jest": {
    "preset": "react-native",
    .
    .
    .
    "setupFiles": [
      "./config/test/jest-setup.js"
    ]
  }

jest-setup.js

jest.autoMockOff();
jest.mock('react-native-keychain', () => ({
  setGenericPassword: jest.fn(),
  getGenericPassword: jest.fn(),
  resetGenericPassword: jest.fn()
}));

@brianfoody
Copy link

Thanks @fpauer that worked a treat. Legend!!

@fbartho
Copy link

fbartho commented Feb 28, 2019

I was able to mock this, sufficiently, in my jest's before all, using the following code:

RNNativeModules.RNKeychainManager = RNNativeModules.RNKeychainManager || {};

@gberg1
Copy link

gberg1 commented Mar 2, 2019

If I am getting this error merely when I import "import * as Keychain from 'react-native-keychain';"
into a react-native file after using Yarn for install and also running react-native link react-native-keychain what should I do?

@gberg1
Copy link

gberg1 commented Mar 3, 2019

Hey following up here. I get this error when I import "import * as Keychain from 'react-native-keychain';" into a react-native file. Ran yarn and react-native-link successfully. Not using Jest or anything. Can someone let me know what I need to fix this? Thanks! @oblador @fpauer @mandrigin

@brianfoody
Copy link

brianfoody commented Mar 4, 2019 via email

@gberg1
Copy link

gberg1 commented Mar 4, 2019

@brianfoody thanks I did successfully link via react-native link react-native-keychain before getting the error. I was able to manually link and the error went away. I'm unblocked thank you.

In general I assume its best practice to use cocoa pods instead of dragging the workspace file into the Libraries folder manually and adding the .a module to Link Binary with Libraries correct?

@brianfoody
Copy link

brianfoody commented Mar 4, 2019 via email

@brianfoody
Copy link

brianfoody commented Mar 4, 2019 via email

@fbartho
Copy link

fbartho commented May 16, 2019

As mentioned above, we are also exhibiting this issue when in a Jest context. It'd be awesome if that mock solution was in a file in this node module.

Other libraries going through this:

If anybody wants to submit a PR, I recommend doing something like the above examples?

@liranhershko
Copy link

liranhershko commented May 20, 2019

I solved the Cannot read property 'SECURITY_LEVEL_ANY' of undefined error by placing a mock file react-native-keychain.js under __mocks__ folder at the root of the app.
The file's content is simply

const RNKeychainManager = {
  SECURITY_LEVEL_ANY: "SECURITY_LEVEL_ANY",
  SECURITY_LEVEL_SECURE_SOFTWARE: "SECURITY_LEVEL_SECURE_SOFTWARE",
  SECURITY_LEVEL_SECURE_HARDWARE: "SECURITY_LEVEL_SECURE_HARDWARE"
};

export default RNKeychainManager;

Unfortunately all other methods described here didn’t work.

@vinaygoyal20
Copy link

If you are getting it in a live app then you haven’t linked the module natively. react-native link react-native-keychain Or else link manually by following RN docs on it.

On Mon 4 Mar 2019 at 04:22, David Goldberg @.***> wrote: Hey following up here. I get this error when I import "import * as Keychain from 'react-native-keychain';" into a react-native file. Ran yarn and react-native-link successfully. Not using Jest or anything. Can someone let me know what I need to fix this? Thanks! @oblador https://github.com/oblador @fpauer https://github.com/fpauer @mandrigin https://github.com/mandrigin — You are receiving this because you commented. Reply to this email directly, view it on GitHub <#192 (comment)>, or mute the thread https://github.com/notifications/unsubscribe-auth/AB1mZd7nYxFmqdBRmzbw7OYnmqDr6Kifks5vTC7ogaJpZM4a6LZP .

Thanks @brianfoody linking manually worked for me

@zackdotcomputer
Copy link
Contributor

Though I agree with @fbartho that the end goal would be a functional mock that could be imported by developers for testing, I think a good no-effort stopgap would be to surface @fpauer's solution in the README, which is what several other packages also do to help out RN + Jest users. I created a quick PR to add a section with the instructions for how to mock and (hopefully) unbreak the build of anyone else affected by this.

@fpauer
Copy link
Author

fpauer commented Jul 2, 2019

@genuinezack another approach to mock the whole package is:

1 - Create a folder in src called __mocks__
2 - Create a folder for the package, in this case react-native-keychain
3 - Create a index.js file to mocks all methods

/src/__mocks/react-native-keychain/index.js

export default {
  setGenericPassword: jest.fn(),
  getGenericPassword: jest.fn(),
  resetGenericPassword: jest.fn()
}

In that way you dont need to define jest setup files

@zackdotcomputer
Copy link
Contributor

Yup! However, the project I'm helping out with already had a setup file (for mocking local data & API endpoints) and didn't have a __mocks__ directory, so I went with your first solution personally. I'll update the PR to mention that solution as well.

@fahry-mohammed
Copy link

fahry-mohammed commented Jul 2, 2021

@fpauer I have the same code as you suggest but the test not working and indicates the following error:
TypeError: Cannot read property 'AES' of undefined

Using the current latest version of keychain: react-native-keychain": "^7.0.0

here is the code which I am using:

Defining the type globally:

const CONFIG = {
  storage: ReactNativeKeychain.STORAGE_TYPE.AES
}

Then use it for all places like the following:

export async function save(username: string, password: string, server?: string) {
  if (server) {
    await ReactNativeKeychain.setInternetCredentials(server, username, password, CONFIG)
    return true
  } else {
    return ReactNativeKeychain.setGenericPassword(username, password, CONFIG)
  }
}

Having moke on setup file:

jest.autoMockOff()
jest.mock('react-native-keychain', () => ({
  setGenericPassword: jest.fn(),
  getGenericPassword: jest.fn(),
  setInternetCredentials: jest.fn(),
  resetGenericPassword: jest.fn(),
  resetInternetCredentials: jest.fn(),
}))

Mentioned on package json file too:

"setupFiles": [
      "<rootDir>/node_modules/react-native/jest/setup.js",
      "<rootDir>/test/setup.ts"
    ],

Please let me know whether I am making any mistakes...
Thank you

@codewithraqib
Copy link

i reset the cache and some other stuff watchman watch-del-all && rm -f yarn.lock && rm -rf node_modules && yarn && yarn start --reset-cache

and closed the debug mode and it is working.

I suppose this happens if you are working on more than one projects at a time in same machine

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

Successfully merging a pull request may close this issue.