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.

[BUG] Enzyme mount() throws a "No `ref` found." error

See original GitHub issue

Description

Testing mounted motion elements using enzyme mount() throws a ref error that pertains to forwardingRefs:

Error: Uncaught [Error: No 'ref' found. Ensure components created with motion.custom forward refs using 'React.forwardRef']

CodeSandbox: https://codesandbox.io/s/mounting-motion-elements-dz7oh

Steps to reproduce

Steps to reproduce the behavior:

  1. Open codesandbox link.
  2. Run the tests.
  3. Check the console.
  4. The test that mounts the motion component will throw an exeption, while the test that shallows works as expected.

Expected behavior Enzyme should be able to successfully mount the component without throwing an exception.

Additional Information In addition to the codesandbox I have also tested the issue using the following dependency versions in my own codebase using the following:

  • “react”: “16.8.4”,
  • “react-dom”: “16.8.4”,
  • “react-test-renderer”: “16.8.4”,
  • “framer-motion”: “1.16.15”,
  • “mocha”: “5.2.0”,
  • “chai”: “4.1.2”,
  • “enzyme”: “3.10.0”,
  • “enzyme-adapter-react-16”: “1.14.0”

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:8
  • Comments:10

github_iconTop GitHub Comments

8reactions
gc-dancommented, Dec 15, 2019

I’m having the same issue when using react-test-renderer’s create() method directly:

CodeSandbox: https://codesandbox.io/s/mounting-motion-elements-n2elg

Any ideas?

2reactions
emilobergcommented, Jul 31, 2020

@andrefox333 Just a super simple mock where motion.div becomes a simple div, etc.

// __mocks__/framer-motion.tsx
import React from 'react';

export const AnimatePresence = ({ children }: { children: JSX.Element }) => children;

export const motion = {
  div: ({ children, ...rest }: { children: JSX.Element }) => <div {...rest}>{children}</div>,
  img: (props: any) => <img {...props} />,
};
//something.test.tsx
jest.mock('framer-motion');

it(() => { ... })

I just mocked div and img, as those are the only two “elements” we’re currently using. If you’re using a lot of motion elements (e.g. motion.ul, motion.li, motion.whatever) then you could return a proxy with a generic get method that returns an object of the same type as requested, e.g. motion.ul > ul

Read more comments on GitHub >

github_iconTop Results From Across the Web

Change Log · Enzyme - GitHub Pages
mount : extract MountedTraversal.hasClassName from MountedTraversal.instHasClassName, which allows ReactWrapper. · withDom : Display a useful error when withDom ...
Read more >
Continuous integration for React applications using Jest and ...
Learn how to implement continuous integration for React applications using Jest and Enzyme.
Read more >
Jest test fails : TypeError: window.matchMedia is not a function
In this project, I'm using Jest snapshot testing and got an error TypeError: window.matchMedia is not a function inside my component. I go ......
Read more >
Fix the "not wrapped in act(...)" warning - Kent C. Dodds
setState({status: 'rejected', error: e}) } } render() { const {error, ... So while the warning would probably help people find and fix bugs...
Read more >
React Testing with Airbnb's Enzyme, Jest and Babel
If it does not throw an error and it passed then your good to go. What's the difference between shallow rendering and full...
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