Console.Error When Trying To Test NavigationContainer
See original GitHub issueDescribe the bug
When trying to test NavigationContainer, I get a console.error log:
console.error
Warning: An update to ForwardRef(NavigationContainer) inside a test was not wrapped in act(...).
When testing, code that causes React state updates should be wrapped into act(...):
act(() => {
/* fire events that update state */
});
/* assert on the output */
This ensures that you're testing the behavior the user would see in the browser. Learn more at https://fb.me/react-wrap-tests-with-act
in ForwardRef(NavigationContainer)
Expected behavior
No console.error log.
Steps to Reproduce
import React from 'react';
import { NavigationContainer } from "@react-navigation/native";
import { render, fireEvent } from 'react-native-testing-library';
// Silence the warning https://github.com/facebook/react-native/issues/11094#issuecomment-263240420
jest.mock('react-native/Libraries/Animated/src/NativeAnimatedHelper');
describe('<NavigationContainer />', () => {
it('should match snapshot', () => {
const result = render(<NavigationContainer>
</NavigationContainer>);
expect(result).toMatchSnapshot();
})
})
Versions
npmPackages: react: ~16.9.0 => 16.9.0 react-native: ~0.61.5 => 0.61.5 react-native-testing-library: ^2.0.1 => 2.0.1 react-test-renderer: ^16.13.1 => 16.13.1
Issue Analytics
- State:
- Created 3 years ago
- Comments:15 (1 by maintainers)
Top Results From Across the Web
Async testing React Navigation 5 in Jest: NavigationContainer ...
My recommendation after calling render from react-native-testing-library for components that do async work, useEffect (acting as ...
Read more >NavigationContainer | React Navigation
The NavigationContainer is responsible for managing your app state and linking your top-level navigator to the app environment.
Read more >[Solved]-Async testing React Navigation 5 in Jest
Coding example for the question Async testing React Navigation 5 in Jest: NavigationContainer causes console error-Reactjs.
Read more >React Testing Library and the “not wrapped in act” Errors
Why do I get this error? In test, the code to render and update React components need to be included in React's call...
Read more >Setup Jest Tests with React Navigation
So you're using React Navigation and you want to write tests for your ... import { NavigationContainer } from '@react-navigation/native'; ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I utilized async, await, and act to make it work without any warnings or errors.
Perhaps this will help someone…
It’s because your component wrapped in navigation perform some state updates that you don’t await for. Instead of snapshotting the whole screen, it would be better to find certain elements in it and assert if they rendered or not. Using findBy queries the library would automatically wait for the elements to appear.
If you don’t care about further updates to this component, you can call unmount on it straight away after the assertion.
Anyway it’s not a bug in the library. Learn about the act warning for your tests safety.