React-native-linear-gradient: Warning about refs when testing my components with Jest

Created on 12 Apr 2017  Â·  3Comments  Â·  Source: react-native-linear-gradient/react-native-linear-gradient

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:

  1. git clone https://github.com/Strayer/react-native-LinearGradientTestWarning.git
  2. cd react-native-LinearGradientTestWarning
  3. yarn install
  4. yarn test

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?

All 3 comments

In __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';
Was this page helpful?
0 / 5 - 0 ratings

Related issues

lucianomlima picture lucianomlima  Â·  5Comments

kgsachinthaudara picture kgsachinthaudara  Â·  4Comments

dsernst picture dsernst  Â·  4Comments

moiri-gamboni picture moiri-gamboni  Â·  4Comments

hamidhadi picture hamidhadi  Â·  4Comments