Jest Snapshot: Warning: Native component for "RNSVGSvgView" does not exist
See original GitHub issueHello guys,
So I’m testing my RN 0.33.1 with Jest, as follows:
import 'react-native';
import React from 'react';
import svgElementMockGenerator from '../__mocks__/svgElementMockGenerator';
import ReactTestRenderer from 'react-test-renderer';
const Headline = require('../Headline').default;
const Heart = require('../Heart').default;
// eslint-disable-next-line no-undef
jest.mock('react-native-svg', () => {
// eslint-disable-next-line no-undef
const ReactNativeSvg = jest.genMockFromModule('react-native-svg');
return {
Svg : svgElementMockGenerator('Svg', ReactNativeSvg.Svg.propType),
LinearGradient : svgElementMockGenerator('LinearGradient', ReactNativeSvg.LinearGradient.propType),
Path : svgElementMockGenerator('Path', ReactNativeSvg.Path.propType),
Symbol : svgElementMockGenerator('Symbol', ReactNativeSvg.Symbol.propType),
Use : svgElementMockGenerator('Use', ReactNativeSvg.Use.propType),
Stop : svgElementMockGenerator('Stop', ReactNativeSvg.Stop.propType),
Defs : svgElementMockGenerator('Defs', ReactNativeSvg.Defs.propType),
};
});
// eslint-disable-next-line no-undef
test('Heart', () => {
const tree = ReactTestRenderer.create(
<Heart {...{
width : 50,
gradient : ['#EC6D57', '#FCC747'],
rangeValue : 'sport',
}} />
).toJSON();
// eslint-disable-next-line no-undef
expect(tree).toMatchSnapshot();
});
import React from 'react';
export default function svgElementMockGenerator (name, propTypes) {
class SvgMock extends React.Component {
static displayName = name;
static propTypes = propTypes;
render () {
return React.createElement(name, this.props, this.props.children);
}
}
return SvgMock;
}
This is passing, but I’m getting this warning:
PASS src/components/common/UI/__tests__/Components.test.js
✓ Heart (24ms)
✓ Headline (24ms)
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 2 passed, 2 total
Time: 1.601s
Ran all test suites.
console.error node_modules/react-native/Libraries/JavaScriptAppEngine/Initialization/ExceptionsManager.js:78
Warning: Native component for "RNSVGSvgView" does not exist
Any ideas on how to get rid of that one?
Thanks!
Issue Analytics
- State:
- Created 7 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Jest Snapshot: Warning: Native component for ... - GitHub
Hello guys, So I'm testing my RN 0.33.1 with Jest, as follows: ... Jest Snapshot: Warning: Native component for "RNSVGSvgView" does not ......
Read more >Jest Snapshot: Warning: Native component for "RNSVGSvgView ...
Hello guys,. So I'm testing my RN 0.33.1 with Jest, as follows: import 'react-native'; import React from 'react'; import svgElementMockGenerator from '.
Read more >Invariant Violation: Native component for "RNSVGSvgView ...
The problem is that we just upgraded to react-native 0.50.3 and don't want to go back to an old version. Is there an...
Read more >What's wrong with snapshot tests. Since snapshot ... - Medium
When Jest announced snapshot tests in 2016, I was very excited, but since then ... Snapshots only verify that the component renders (meaning...
Read more >[Solved]-Using require for SVG files in React Native-React ...
[Solved]-Using require for SVG files in React Native-React Native ... SVG - (iOS) Invariant Violation: Native component for "RNSVGSvgView" does not exist ......
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
Carrying on from the code posted here (thanks for doing the bulk of the work!). I’ve made it a little more generic where it’ll automatically mock out anything react-native-svg exports so it exports everything, and would support changes in the future.
I put this file into my project root
/__mocks__/react-native-svg.js
I added back the props and children since I wanted my snapshots to include the full Svg output where without it, you’d just get
<Svg />
in the snapshot. If you want that, just change the mock to passnull
for the children instead.Thank you @jakecraige and @isnifer, you guys are heroes! Your solution solved the following error that I got when running Jest:
TypeError: inst.toJSON is not a function