-
Notifications
You must be signed in to change notification settings - Fork 24k
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
mock Animated components with versions that skipSetNativeProps #25108
Conversation
|
@grabbou @matthargett this is a change i needed to suppress warnings i was seeing from animated components being rendered in jest in 0.59, it looks like createAnimatedComponent gets mocked too late so setNativeProps gets called on these components and throws a bunch of warnings. |
I ran into this problem myself and I solved it a bit differently. I used jest.spyOn to change the params at runtime: import { Animated } from 'react-native';
const mockedAnimatedTiming = Animated.timing;
jest.spyOn(Animated, 'timing').mockImplementation((value, options) => {
return mockedAnimatedTiming(value, {
...options,
useNativeDriver: false,
});
}); I'd suggest we adopt this for the RN jest/setup.js because it removes the whole need for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you rebase this on master instead? We don't merge things like this into stable branches.
Please check out https://github.com/facebook/react-native/pull/25108/files#diff-606adbd6a8c97d177b17baee5a69cdd9R101 where we are mocking AnimatedImplementation. I think we should make the changes there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code analysis results:
eslint
found some issues. Runyarn lint --fix
to automatically fix problems.
@cpojer i rebased against master as you requested. I wasn't sure about your comment on applying this to the If there is a better way to apply the |
@RubenSandwich I tried your snippet with my tests cases but I still saw the warning. Maybe I didn't apply it correctly? In my case I have I'd still like the animation to run in my test as I have something that tracks the animation frames so I can prove that those values are getting interpolated the way i expect over time. |
So I put that code at the start of my own app's native module mocks. To use it in jest/setup.js ...
.mock('AnimatedImplementation', () => {
const AnimatedImplementation = require.requireActual('AnimatedImplementation');
const oldTiming = AnimatedImplementation.timing;
AnimatedImplementation.timing = function(value, options) {
return oldTiming(value, {
...options,
useNativeDriver: false,
});
};
return AnimatedImplementation;
})
... I ran my own apps tests and that change worked just fine without any changes to the snapshots or console warnings. No idea if it would pass the current tests in the RN repo though. It also still seems to remove the need for __skipSetNativeProps_FOR_TESTS_ONLY in my tests. Another important point is that I'm on RN 52, so the above code would need to be modified for removing the haste style imports. (So full file paths.) |
It looks like this is breaking all of the Animated tests in React Native. Can you fix them up? |
Note that the underlying issue appears to be a regression from sometime between 0.55 and 0.59, and was discovered during an upgrade. cc @kelset |
@rdy could you take a look at the test failures? This version is not mergeable and breaks all the JS tests. |
@cpojer yes, sorry was wrapped up in a few other tasks this week but I’ll look into the failures. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code analysis results:
eslint
found some issues. Runyarn lint --fix
to automatically fix problems.
…OR_TESTS_ONLY in jest
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, that looks good!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cpojer is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
This pull request was successfully merged by @rdy in a4acd88. When will my fix make it into a release? | Upcoming Releases |
…ook#25108) Summary: Using Animated components like View and Image do not get created with __skipSetNativeProps_FOR_TESTS_ONLY = true since they get created before the jest mock can be applied to createAnimatedComponent. For these components mock getters to create versions that properly skip set native props. Jest tests that render these components get warnings the setNativeProps gets called even though using a testRenderer this should not happen. ## Changelog [Javascript] [Fixed] - Define Animated Components for react-native/jest/setup that properly skip setNativeProps Pull Request resolved: facebook#25108 Differential Revision: D15779434 Pulled By: cpojer fbshipit-source-id: f39e21ed8e71c2c155297c791d3bf573909896d6
Summary
Using Animated components like View and Image do not get created with __skipSetNativeProps_FOR_TESTS_ONLY = true since they get created before the jest mock can be applied to createAnimatedComponent. For these components mock getters to create versions that properly skip set native props.
Jest tests that render these components get warnings the setNativeProps gets called even though using a testRenderer this should not happen.
Changelog
[Javascript] [Fixed] - Define Animated Components for react-native/jest/setup that properly skip setNativeProps
Test Plan
Run tests