[BUG] unmounting a component with react-test-renderer causes TypError in animateVisualElement
Hello,
2. Describe the bug
Rendering AnimatePresence with react-test-renderer, then invoking unmount, results in TypeError: The provided value is not of type 'Element'. in animateVisualElement
3. IMPORTANT: Provide a CodeSandbox reproduction of the bug
https://codesandbox.io/s/framer-motion-error-on-unmount-7vgm1
The following test fails. The example is simply taken from https://www.framer.com/api/motion/animate-presence/
import React from "react";
import reactTestRenderer from "react-test-renderer";
import { motion, AnimatePresence } from "framer-motion";
export const MyComponent = ({ isVisible }) => (
<AnimatePresence>
{isVisible && (
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
/>
)}
</AnimatePresence>
);
it("should unmount", () => {
const tree = reactTestRenderer.create(<MyComponent isVisible={true} />);
tree.unmount();
});
Error:
TypeError: The provided value is not of type 'Element'.
at Object.exports.convert (node_modules/jsdom/lib/jsdom/living/generated/Element.js:26:9)
at Window.getComputedStyle (node_modules/jsdom/lib/jsdom/browser/Window.js:644:19)
at HTMLVisualElement.Object.<anonymous>.HTMLVisualElement.getComputedStyle (node_modules/framer-motion/dist/framer-motion.cjs.js:2121:23)
at HTMLVisualElement.Object.<anonymous>.HTMLVisualElement.read (node_modules/framer-motion/dist/framer-motion.cjs.js:2007:34)
at HTMLVisualElement.Object.<anonymous>.HTMLVisualElement.readNativeValue (node_modules/framer-motion/dist/framer-motion.cjs.js:2030:25)
at checkTargetForNewValues (node_modules/framer-motion/dist/framer-motion.cjs.js:1824:95)
at HTMLVisualElement.Object.<anonymous>.HTMLVisualElement.makeTargetAnimatable (node_modules/framer-motion/dist/framer-motion.cjs.js:2061:13)
at animateTarget (node_modules/framer-motion/dist/framer-motion.cjs.js:3493:28)
at animateVisualElement (node_modules/framer-motion/dist/framer-motion.cjs.js:3441:21)
at node_modules/framer-motion/dist/framer-motion.cjs.js:3721:20
React components:
in Unknown (created by ForwardRef(MotionComponent))
in ForwardRef(MotionComponent) (at checkbox.test.js:9)
in PresenceChild (created by AnimatePresence)
in AnimatePresence (at checkbox.test.js:7)
in MyComponent (at checkbox.test.js:19)
No error when setting isVisible={false}.
5. Expected behavior
Unmounting does not causes an error. This issue currently makes it impossible to use storyshots (https://storybook.js.org/docs/react/workflows/snapshot-testing), which unmounts after taking a snapshot. We encountered this issue when using the checkbox component from chakra-ui. https://github.com/chakra-ui/chakra-ui/issues/3203
7. Environment details
framer-motion v3.3.0 react v16.14.0 jest v26.6.2
Edit: created codesandbox with the failing test
Marking this as wontfix because we can't support every test environment. The bug here is IMO in react-test-renderer. Whatever it's passing through to ref isn't an Element. You could try mocking Window.getComputedStyle in your test environment as the implementation in jsdom is throwing the error.
Ok.
I had not noticed jsdom in the stack trace (or I did not understand the implications of that when I logged the ticket). Now you mention it, It makes sense that ending up in jsdom when using react-test-renderer is not going to work well.
I could work around it by using @testing-library/react in my snapshot tests.