-
Notifications
You must be signed in to change notification settings - Fork 273
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
Warning An update to ForwardRef inside a test was not wrapped in act(...) #1668
Comments
Our of curiosity, why are you externally triggering state update instead of triggering by a button press? That would be a recommended way in RNTL as it would resemble a real user interaction. |
you need the button to be enabled in order to press it. the state update is to enable or disable the button. and it is triggered by a componentDidUpdate. |
could you put it in a repro repo so I could reproduce it too? |
let me know if you have any access issue. |
I tried my hand at solving this problem, but I only have a workaround. Short answer: it's caused by the Button going from disabled=false to disabled=true, causing an animation cycle. I don't know why RNTL does not like that. To prove that it's the Button: To rule out the suspicion that it's caused by the manual call to Here, I mocked TouchableOpacity and the warning message disappears.
The same solution can be applied to the reproduction code without changing to a real redux store: // Mock TouchableOpacity so that Button doesn't animate when changing disabled state
jest.mock(
"react-native/Libraries/Components/Touchable/TouchableOpacity",
() => {
const React = require("react");
const { View } = require("react-native");
return (props) => <View {...props}>{props.children}</View>;
}
); Bonus, by mocking the TouchableOpacity, the original code doesn't even have to wrap the rerender in act anymore: it("should update the internal state when the Redux count changes without wrapping in act", async () => {
const { getByTestId, rerender } = render(
<Provider store={store}>
<ConnectedMyPureComponent />
</Provider>
);
// Initial state should be 0
expect(getByTestId("internal-count").props.children).toBe(0);
// Update Redux state and rerender the component
store = mockStore({ count: 5 });
rerender(
<Provider store={store}>
<ConnectedMyPureComponent />
</Provider>
);
// Now the internal state should be updated to 5
await waitFor(() =>
expect(getByTestId("internal-count").props.children).toBe(5)
);
}); |
It seems like the cause you are seeing the error message is that the animation triggered by the Button/TouchableOpacity is finishing after the If you want to solve it, consider using fake timers and experimenting with something like BTW the error does not come from RNTL, but rather underlying React Test Renderer. |
Closing as stale. |
For anyone who faces this mdjastrzebski's comment really helped. Our issue was animation running on render of a component and completing after the assertion, even those the assertions were in an act. We created a helper function to mock animated views for this instances. export const mockAnimatedView = () => {
jest.mock('react-native', () => {
const rn = jest.requireActual('react-native');
const spy = jest.spyOn(rn.Animated, 'View', 'get');
spy.mockImplementation(() => jest.fn(({ children }) => children));
return rn;
});
jest.useFakeTimers();
}; |
Describe the bug
How do I get rid of this warning an update to ForwardRef inside a test was not wrapped in act?
I am already using act in my test but keep getting this warning even though test passes. see repro step.
Expected behavior
Steps to Reproduce
Here is an example component and the output.
Screenshots
Versions
12.7.2
The text was updated successfully, but these errors were encountered: