question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

react-test-renderer does not work with ref methods

See original GitHub issue

Do you want to request a feature or report a bug? Report a bug

What is the current behavior? Ref methods cause an error to be thrown

If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install and yarn test.

import React from 'react'
import renderer from 'react-test-renderer'

it('Test refs', () => {
  class Comp extends React.Component {
    componentDidMount () {
      console.log(this.myRef)
      this.myRef.addEventListener('click', () => console.log('click'))
    }
    render () {
      return (
        <div ref={div => { this.myRef = div }}>foo</div>
      )
    }
  }

  const tree = renderer.create(
    <Comp />
  ).toJSON()

  expect(tree).toMatchSnapshot()
})

Cannot read property 'addEventListener' of null

What is the expected behavior?

For refs to work and have usable mock methods. I did some digging through other issues and it looks like this was fixed here https://github.com/facebook/react/issues/7740 but I’m not sure if ref DOM methods were ever fixed.

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

MacOS react-test-renderer @ 16.2.0 jest @ 21.2.1 react @ 16.1.1 node @ 8.2.1 yarn @ 0.27.5

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
SimenBcommented, Feb 6, 2018

Also see React docs about createNodeMock: https://reactjs.org/docs/test-renderer.html#ideas for an alternative.

Either way, this is not a jest issue

1reaction
SimenBcommented, Feb 6, 2018

You’ve linked to the correct issue in react’s tracker - react-test-renderer does not support ref. You can either use ReactDOM or enzyme to do a real render.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Test Renderer - React
This package provides a React renderer that can be used to render React components to pure JavaScript objects, without depending on the DOM...
Read more >
Refs are null in Jest snapshot tests with react-test-renderer
Looks like ref value that I am getting is null in jsdom. There is and issue here: https://github.com/facebook/react/issues/7371 but looks like ...
Read more >
Testing React Components with React Test Renderer
React Test Renderer is a renderer for testing React Components, which traverses a ReactElement tree and represents as a JSON object.
Read more >
How To Test a React App with Jest and React Testing Library
Because Jest is pre-packaged with Create React App, you do not need to ... Inside the function, there is a render method that...
Read more >
Snapshot Testing - Jest
A similar approach can be taken when it comes to testing your React components. Instead of rendering the graphical UI, which would require ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found