New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] Testing AnimatePresence with RTL #285
Comments
Having the same issue |
I did a bit more digging and it happening in the getTranslateFromMatrix function. I'm guessing from the |
So I solved my bug my simply wrapping my variants that changed y in an |
Same issue for me. The @CurtisHumphrey solution doesn't seem to be the proper one, is there any correct solution for this or we need to wait the bug to be solved? |
Any update around how to properly test |
It might not solve your issue, but I could solve mine by replacing |
I can confirm that switching to a numeric value works. However '100%' is radically different from 100. I basically have to calculate the exact amount that represents the height of the container and then apply it. Or I can just pick a big enough value and accept that the animation is going to look a little different. Also I'm not in favor of I also debugged and the point of failure is the |
Ok so I think I found a workaround. I changed Must be a bug in the code where if it sees a numeric for the animate target it treats the initial as a numeric as well. Or something like that. |
This issue unfortunately occurs with any |
@jmathew's workaround worked for us! |
This fails for me with react testing library:
Console complains:
|
Ok so It seems framer-motion only accepts pixels so one is expected to just use numbers only. |
@Aromokeye Motion accepts any value type. Animating between them requires DOM measurements though, which aren't available in jsdom. I believe the solution is to test with a browser-based suite (we have to use Cypress as well as Jest to be able to properly test Motion) or explore mocking |
I'm having the same issue with a similar use case. I have a modal component as well and I am wrapping it using AnimatePresence. When I click the close button the content of my modal should disappear from DOM, but it doesn't happen when I'm using AnimatePresence. Any ideas on how do handle it? My component
My test
|
I am also having this issue. My use case is a little different since I am trying to implement animated notifications. In addition to a close button, my notifications have a settimeout that removes them from the DOM after 30 seconds. Testing for either of these outcomes with There is a workaround which may work in your case since you aren't implementing a timeout. You can wrap your expect assertion in RTLs waitFor (make sure to async your test and await waitFor). That should pass your test above, its worth a try. |
Thanks! |
I don't like this at all but I solved the issue with this jest mock jest.mock('framer-motion', () => {
const actual = require.requireActual('framer-motion')
return {
__esModule: true,
...actual,
AnimatePresence: ({ children }) => (
<div className='mocked-framer-motion-AnimatePresence'>
{ children }
</div>
),
motion: {
...actual.motion,
div: ({ children }) => (
<div className='mocked-framer-motion-div'>
{ children }
</div>
),
},
}
}) Obviously if you're using other motion elements other than |
@TSMMark
|
@chapster11 I haven't used custom components, but it looks like the jest.mock('framer-motion', () => {
const actual = require.requireActual('framer-motion')
return {
__esModule: true,
...actual,
default: (component, options) => /* your mock here */
// ...etc |
Thanks @TSMMark unfortunately that gives the same error result. I wish framer-motion would give some test examples. So far I have to use @testing-library/react waitFor to wait for the animation to complete. This is slowing down tests. I was hoping your method would allow me to mock it in a way where I could not have to wait for the animation to complete before I could see the rendered result. |
@TSMMark Do you know if there is a better solution to test framer motion divs? Your mock solution works great! |
This solution did it for me, thanks a lot! |
Hi!
Describe the bug
I'm using AnimatePresence to animate the enter & exit of a modal based on a boolean prop. The issue comes when I want to test, with React Testing Library, the rendering of the AnimatePresence childrens. I get an error and I'm not able to resolve the issue.
To Reproduce
Additional context
The error only happens when I pass the boolean prop
isOpen
astrue
. Asfalse
doesn't render anything as expected.Any ideas?
The text was updated successfully, but these errors were encountered: