Tommy Leong
Tommy Leong

Reputation: 3030

TypeError: (0 , _native.createNavigatorFactory) is not a function

Trying to perform npm test on my React Native application, unit testing with Jest are working fine. However with react-test-renderer it complaining the following issue.

FAIL  __tests__/App-test.js
  ● Test suite failed to run

    TypeError: (0 , _native.createNavigatorFactory) is not a function

      at Object.<anonymous> (node_modules/@react-navigation/material-top- tabs/lib/commonjs/navigators/createMaterialTopTabNavigator.tsx:50:16)
      at Object.<anonymous> (node_modules/@react-navigation/material-top-tabs/lib/commonjs/index.tsx:4:1)

I did not write this test case, it comes default while the project was created. I'm expecting to see a pass unit test from here, what seems to be the issue from the react-test-renderer?

/**
 * @format
 */

import 'react-native';
import React from 'react';
import App from '../App';

// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';

it('renders correctly', () => {
  renderer.create(<App />);
});

Upvotes: 3

Views: 2812

Answers (1)

Sumeet Raina
Sumeet Raina

Reputation: 117

You may have to mock renderer

I have had the similar issue with useHeaderHeight

example I had import { useHeaderHeight } from '@react-navigation/stack'; in my screen file. So when I was running the test I was getting the same error

 FAIL  src/screens/expressCheckout/ExpressCheckoutScreen.test.tsx
  ● Test suite failed to run

    TypeError: (0 , _native.createNavigatorFactory) is not a function

      at Object.<anonymous> (node_modules/@react-navigation/stack/lib/commonjs/navigators/createStackNavigator.tsx:96:16)
      at Object.<anonymous> (node_modules/@react-navigation/stack/lib/commonjs/index.tsx:9:1)

Which I fixed by adding below mentioned jest mock in my test file and then I was able to run my tests

jest.mock('@react-navigation/stack', () => ({
  useHeaderHeight: (): number => 0,
}));

Upvotes: 2

Related Questions