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.

react-native-web jest error

See original GitHub issue

“react”: “^16.8.4”, “react-native”: “0.55.4”, “react-native-web”: “^0.9.x”, /// NOTE: using RNW “react-native-svg”: “6.0.0”,

react-native-svg seems to be working fine for Android and Web so far but when running my Jest test I am getting the following error

● Test suite failed to run

TypeError: (0 , _reactNativeWeb.requireNativeComponent) is not a function

  1 | import React from 'react';
  2 |
> 3 | import Svg, { Path, Rect } from 'react-native-svg';
    | ^
  4 |
  5 | import { numPropType, stringPropType } from '../../prop-types/propTypes';
  6 |

  at Object.<anonymous> (node_modules/react-native-svg/elements/Svg.js:99:23)
  at Object.<anonymous> (node_modules/react-native-svg/index.js:7:1)
  at Object.<anonymous> (src/common/assets/edit-log-icons/clone-icon.js:3:1)

I tried to mock react-native-svg but it doesn’t seem to be working. I tried react-native-svg-mock to no avail and I also tried several different variations online. The most current of which is

jest.mock('react-native-svg', () => {
    const React = require('react');
    const ReactNativeSvg = jest.genMockFromModule('react-native-svg');

    const svgElementMockGenerator = (name, propTypes) => {
        function SvgMock() {
            return (
                React.createElement(name, null, null)
            )
        }

        SvgMock.displayName = name
        SvgMock.propTypes = propTypes

        return SvgMock
    };

    const Svg = svgElementMockGenerator('Svg', ReactNativeSvg.Svg.propTypes);

    Svg.Rect = svgElementMockGenerator('Rect', ReactNativeSvg.Rect.propTypes);
    Svg.LinearGradient = svgElementMockGenerator(
        'LinearGradient',
        ReactNativeSvg.LinearGradient.propTypes
    );
    Svg.Path = svgElementMockGenerator('Path', ReactNativeSvg.Path.propTypes);
    Svg.Symbol = svgElementMockGenerator('Symbol', ReactNativeSvg.Symbol.propTypes);
    Svg.Use = svgElementMockGenerator('Use', ReactNativeSvg.Use.propTypes);
    Svg.Stop = svgElementMockGenerator('Stop', ReactNativeSvg.Stop.propTypes);
    Svg.Defs = svgElementMockGenerator('Defs', ReactNativeSvg.Defs.propTypes);

    return Svg;
});

using this mock seems to make the error SLIGHTLY different, in that it doesn’t point to the import.

● Test suite failed to run

TypeError: (0 , _reactNativeWeb.requireNativeComponent) is not a function

  at Object.<anonymous> (node_modules/react-native-svg/elements/Svg.js:146:64)
  at Object.<anonymous> (node_modules/react-native-svg/index.js:142:35)
  at Object.user:/home/thorakmedichi/Development/Projects/myProject/node_modules/react-native-svg/index.js: (config/initTest.js:16:29)

Any help would be appreciated.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8

github_iconTop GitHub Comments

4reactions
MillerGregorcommented, Aug 10, 2020

as @Sliverb suggested, bakerface/react-native-svg-web fixed my jest tests… but jest.mock() didn’t work for me.

I had to make two additions to my jest config:

{
  moduleNameMapper: {
    'react-native-svg': 'react-native-svg-web',
  },
  transformIgnorePatterns: [
    'node_modules/(?!(react-native|react-native-svg-web|react-native-something-else)/)'
  ]
}
1reaction
merrywhethercommented, Aug 2, 2022

This isn’t covered in the docs, but you need to tell Jest to prefer .web.js files over .js files so that it will behave properly when it encounters the dynamic entry point

  moduleNameMapper: {
    '^react-native$': 'react-native-web',
  },
  moduleFileExtensions: ['...', 'web.js', 'js', '...'],
Read more comments on GitHub >

github_iconTop Results From Across the Web

jest-expo broken on react-native-web@0.18.x #18090 - GitHub
Summary react-native-web no longer bundles "react-native-web/jest-preset" and jest-expo expects it. It looks like they now recommend a ...
Read more >
React Native Web testing jest error: ReferenceError: __DEV__ ...
1 Answer 1 ... It seems you have DEV instead of __DEV__ in your package.json : // ... "jest": { // ... "globals":...
Read more >
Testing with Jest - Expo Documentation
In this guide, you'll learn how to set up Jest in your project, write a unit test, write a snapshot test, and common...
Read more >
Using TypeScript - React Native
If the above command is failing, you may have an old version of react-native or react-native-cli installed globally on your system. To fix...
Read more >
How To Test a React App with Jest and React Testing Library
js file. Now, when you look at your tests in the terminal, you will receive the following output: Output. console.error Warning: ...
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