I included react-native-linear-gradient in one of my components and now receive the following warning when running the jest test suite:
$ jest
PASS __tests__/App.js
App
✓ should render correctly (40ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 1 passed, 1 total
Time: 0.728s, estimated 1s
Ran all test suites.
console.error node_modules/react-native/Libraries/Core/ExceptionsManager.js:71
Warning: Stateless function components cannot be given refs. Attempts to access this ref will fail.
Check the render method of `LinearGradient`.
in Unknown (created by LinearGradient)
in LinearGradient (created by App)
in View (created by View)
in View (created by App)
in App
✨ Done in 1.38s.
I couldn't find out why this is happening, googling also didn't help. The Jest snapshot seems fine, I also don't see any warnings when running the app, so it seems like this is only happening in the test environment…
I created an example app that shows this behaviour and pushed it to GitHub: https://github.com/Strayer/react-native-LinearGradientTestWarning
Steps to reproduce:
git clone https://github.com/Strayer/react-native-LinearGradientTestWarning.gitcd react-native-LinearGradientTestWarningyarn installyarn testIn __mocks__, create react-native-linear-gradient.js, with content:
import { View } from 'react-native';
export default View;
@peterorum mocking this away prevents them from included into my snapshots, which is not desirable. And really, can't we fix the actual issue here? Why is this package referring to a ref on a stateless function?
Instead of exporting a View, export a string and jest uses that as the component name:
export default 'BVLinearGradient';
Most helpful comment
@peterorum mocking this away prevents them from included into my snapshots, which is not desirable. And really, can't we fix the actual issue here? Why is this package referring to a ref on a stateless function?