Error with Jest testing
JJ-Stanly opened this issue · 2 comments
I am unable to test snapshot of my component which in turn uses Autolink component. Am getting error
Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object.
I have tried mocking as following:
jest.mock('react-native-autolink', () => {
return {
onPress: jest.fn().mockImplementationOnce((url) => 'url'),
};
});
It still returns same error. Any help?
Hard to diagnose without digging into your actual code and Jest config. My suggestion would be to check your imports and Jest configuration. This package as well as autolinker use import syntax and need to be transformed accordingly, which is not default Jest behavior. Also, perhaps take a look at the Jest configuration repo within this repo as it runs without issue.
You need to add transformIgnorePatterns to jest configuration and ignore autolinker.
"jest": {
"preset": "react-native",
"transformIgnorePatterns": [
"node_modules/(?!(@?react-native.*|autolinker.*)/)"
]
}