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)
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()
}));
Ping @mandrigin. Not sure we want to fix this.
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?
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.
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!
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...
the cause is here https://github.com/oblador/react-native-keychain/blob/01755de085b6e7725e565bf5d4dd0be06da1414a/index.js#L5
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?
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...
@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()
}));
Thanks @fpauer that worked a treat. Legend!!
I was able to mock this, sufficiently, in my jest's before all, using the following code:
RNNativeModules.RNKeychainManager = RNNativeModules.RNKeychainManager || {};
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?
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
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 notifications@github.com 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
https://github.com/oblador/react-native-keychain/issues/192#issuecomment-469060171,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AB1mZd7nYxFmqdBRmzbw7OYnmqDr6Kifks5vTC7ogaJpZM4a6LZP
.
@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?
I try to avoid pods if I can and just use react native link. It’s not
always perfect though. The manual process does feel a bit wrong but at
least it’s easily fixed. When pods breaks stuff it’s always a nightmare for
me.
On Mon 4 Mar 2019 at 11:07, David Goldberg notifications@github.com wrote:
@brianfoody https://github.com/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?—
You are receiving this because you were mentioned.Reply to this email directly, view it on GitHub
https://github.com/oblador/react-native-keychain/issues/192#issuecomment-469102990,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AB1mZeL8dpM023KLhZQiuseFgwCPFTrhks5vTI39gaJpZM4a6LZP
.
Good to hear you are unblocked, I know the pain of getting blocked on these
things only too well.
On Mon 4 Mar 2019 at 11:18, Brian Foody brianfoody@gmail.com wrote:
I try to avoid pods if I can and just use react native link. It’s not
always perfect though. The manual process does feel a bit wrong but at
least it’s easily fixed. When pods breaks stuff it’s always a nightmare for
me.
On Mon 4 Mar 2019 at 11:07, David Goldberg notifications@github.com
wrote:@brianfoody https://github.com/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?—
You are receiving this because you were mentioned.Reply to this email directly, view it on GitHub
https://github.com/oblador/react-native-keychain/issues/192#issuecomment-469102990,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AB1mZeL8dpM023KLhZQiuseFgwCPFTrhks5vTI39gaJpZM4a6LZP
.
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.
If anybody wants to submit a PR, I recommend doing something like the above examples?
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.
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
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.
@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
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.
Most helpful comment
@marciok Yes, but you need to include it in your jest setup file and in
package.jsondefine the setup file path.jest-setup.js