RN 0.44
TypeError: Cannot read property 'create' of undefined
at Object.<anonymous> (node_modules/react-native-vector-icons/lib/icon-button.js:29:1595)
at Object.<anonymous> (node_modules/react-native-vector-icons/lib/create-icon-set.js:21:233)
at Object.<anonymous> (node_modules/react-native-vector-icons/EvilIcons.js:6:20)
Why is StyleSheet not available? I feel I need more context on your setup to be able to answer this.
I bet he is referring to Jest snapshot testing. Please refer to this gist @iegik, in particular the "jest.mock('react-native' ... )" part.
Unfortunately Jest is quite a pain to set up correctly, and you manually need to mock pretty much every complex library you're going to need.
Now you just need to add the following to your package.json:
"jest": {
"preset": "react-native",
"setupFiles": [
"./setupJest.js"
],
}
@oblador, this issue should be closed, as it isn't related to your project.
@iegik it didn't work
@iegik did u find solution for this issue?
Any update on this? After updating RN to 0.56.0 I'm still getting this error.
I had a wild run today hunting this down, although it is quite probable that we are seeing this bug for different reasons, but anyway:
'.' directory in your moduleDirectories in jest configuration, you are gonna have a bad time. Change the '.' to '<rootDir>'react-native 0.55.4
jest 23.6
The way jest module loading works, it checks for modules first in the directories configured by the moduleDirectories option, and if you have a . directory there, let's say because you want to write code like:
import { stuff } from 'src/of/stuff'
where src is at the top level of your project directory, then well you just told jest to always look in the directory that the importing file lives in first.... not what you expected probably. But anyways, so when react-native-vector-icons in icon-button.js imports StyleSheet from this little library file called react-native.js, which just imports and re-exports all of react-native (except now it's a cyclical import because there's a react-native that can be resolved from the . module directory), it just gets an empty module basically.
solution: change '.' in moduleDirectories to '<rootDir>' instead
I am still facing this issue while creating jest test cases. Can someone help me with this? I am new to this and could not find a solution.
` โ Test suite failed to run
TypeError: Cannot read property 'create' of undefined
at Object.<anonymous> (node_modules/react-native-vector-icons/lib/icon-button.js:8:27)
at Object.<anonymous> (node_modules/react-native-vector-icons/lib/create-icon-set.js:12:1)
`
Am sure you only need this :
jest.doMock('react-native', () => {
return Object.setPrototypeOf(
{
StyleSheet: {
create: jest.fn(),
},
},
ReactNative,
);
});
This is my set up
jest.doMock('react-native', () => {
return Object.setPrototypeOf(
{
Platform: {
OS: 'android',
select: () => {},
},
NativeModules: {
...ReactNative.NativeModules,
RNFBAdMobModule: {},
RNFBAdMobInterstitialModule: {},
RNFBAdMobRewardedModule: {},
RNFBAdsConsentModule: {},
RNFBAppModule: {
NATIVE_FIREBASE_APPS: [
{
appConfig: {
name: '[DEFAULT]',
},
options: {},
},
{
appConfig: {
name: 'secondaryFromNative',
},
options: {},
},
],
addListener: jest.fn(),
eventsAddListener: jest.fn(),
eventsNotifyReady: jest.fn(),
},
RNFBAuthModule: {
APP_LANGUAGE: {
'[DEFAULT]': 'en-US',
},
APP_USER: {
'[DEFAULT]': 'jestUser',
},
addAuthStateListener: jest.fn(),
addIdTokenListener: jest.fn(),
useEmulator: jest.fn(),
},
RNFBCrashlyticsModule: {},
RNFBPerfModule: {},
RNVectorIconsManager: jest.mock(),
},
StyleSheet: {
create: jest.fn(),
},
},
ReactNative,
);
});
^ The above comment broke every test in my project, it's not StyleSheet that needs mocked
Most helpful comment
Any update on this? After updating RN to 0.56.0 I'm still getting this error.