question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Console.Error When Trying To Test NavigationContainer

See original GitHub issue

Describe 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:closed
  • Created 3 years ago
  • Comments:15 (1 by maintainers)

github_iconTop GitHub Comments

26reactions
jonmchancommented, Jun 23, 2020

I utilized async, await, and act to make it work without any warnings or errors.

import React from 'react';
import { NavigationContainer } from "@react-navigation/native";
import { act } from 'react-test-renderer';
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', async () => {
    const result = render(<NavigationContainer>
    </NavigationContainer>);
    await act(async () => { expect(result).toMatchSnapshot(); })
  });
});

Perhaps this will help someone…

12reactions
thymikeecommented, Jun 23, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found