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.

Snapshot testing issue with react-test-renderer

See original GitHub issue

I am trying to write a snapshot test using jest/react-test-renderer but it appears to be incompatible with react-sortable-hoc. I am using one of your samples:

import React, { Component } from "react";
import {
  SortableContainer,
  SortableElement,
  arrayMove
} from "react-sortable-hoc";

const SortableItem = SortableElement(({ value }) => <li>{value}</li>);

const SortableList = SortableContainer(({ items }) => {
  return (
    <ul>
      {items.map((value, index) => (
        <SortableItem key={`item-${index}`} index={index} value={value} />
      ))}
    </ul>
  );
});

class SortableComponent extends Component {
  state = {
    items: ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6"]
  };
  onSortEnd = ({ oldIndex, newIndex }) => {
    this.setState({
      items: arrayMove(this.state.items, oldIndex, newIndex)
    });
  };
  render() {
    return <SortableList items={this.state.items} onSortEnd={this.onSortEnd} />;
  }
}

export default SortableComponent;

And my test looks like this:

import React from "react";
import App from "./App";
import renderer from "react-test-renderer";

it("renders", () => {
  const component = renderer.create(<App />);
  expect(component).toMatchSnapshot();
});

But I receive the following error:

    Invariant Violation: getNodeFromInstance: Invalid argument.

      at invariant (node_modules\fbjs\lib\invariant.js:44:15)
      at Object.getNodeFromInstance (node_modules\react-dom\lib\ReactDOMComponentTree.js:162:77)
      at findDOMNode (node_modules\react-dom\lib\findDOMNode.js:49:41)
      at _class.setDraggable (node_modules\react-sortable-hoc\dist\commonjs\SortableElement\index.js:99:58)
      at _class.componentDidMount (node_modules\react-sortable-hoc\dist\commonjs\SortableElement\index.js:62:16)
      at node_modules\react-test-renderer\lib\ReactCompositeComponent.js:265:25
      at measureLifeCyclePerf (node_modules\react-test-renderer\lib\ReactCompositeComponent.js:75:12)
      at node_modules\react-test-renderer\lib\ReactCompositeComponent.js:264:11
      at CallbackQueue.notifyAll (node_modules\react-test-renderer\lib\CallbackQueue.js:76:22)
      at ReactTestReconcileTransaction.close (node_modules\react-test-renderer\lib\ReactTestReconcileTransaction.js:36:26)
      at ReactTestReconcileTransaction.closeAll (node_modules\react-test-renderer\lib\Transaction.js:206:25)
      at ReactTestReconcileTransaction.perform (node_modules\react-test-renderer\lib\Transaction.js:153:16)
      at batchedMountComponentIntoNode (node_modules\react-test-renderer\lib\ReactTestMount.js:69:27)
      at ReactDefaultBatchingStrategyTransaction.perform (node_modules\react-test-renderer\lib\Transaction.js:140:20)
      at Object.batchedUpdates (node_modules\react-test-renderer\lib\ReactDefaultBatchingStrategy.js:62:26)

I think this is either a ref issue or perhaps some issue with stateless functional components. Any ideas?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

27reactions
foaly-nr1commented, Jan 24, 2018

As I want to unit test my own component and the HOC is tested separately, I simply mock the HOC:

// /__mocks__/react-sortable-hoc.js

export const SortableContainer = component => component;
export const SortableElement = component => component;
export const SortableHandle = component => component;
0reactions
foaly-nr1commented, Mar 8, 2019

@lmadigan save it in the file /__mocks__/react-sortable-hoc.js and Jest should automatically apply the mock for you. That’s at least what happened a year ago.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Snapshot Testing - Jest
A typical snapshot test case renders a UI component, takes a snapshot, then compares it to a reference snapshot file stored alongside the...
Read more >
Creating snapshots in Jest for testing React applications
A typical snapshot test case renders a UI component, takes a snapshot, then compares it to a reference snapshot file stored alongside the...
Read more >
Test Renderer - React
You can use Jest's snapshot testing feature to automatically save a copy of the JSON tree to a file and check in your...
Read more >
snapshot testing with react-test-renderer - Stack Overflow
I'm trying to render the app which certain state, to ensure the same app is rendered. The test case passes, but I get...
Read more >
How To Write Snapshot Tests For React Components With Jest
When writing snapshot tests for a React component, you first need to have code in a working state. Then, generate a snapshot of...
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