-
-
Notifications
You must be signed in to change notification settings - Fork 428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Try to test component with React generated form svgr #83
Comments
It looks like a |
I did handle with this by added following conf to the package.json:
But now, I have a following error when I try to render a React component inside test: it('test', () => {
const minimize = sinon.spy();
const toJSON = renderer.create(<CloseBubble minimize={minimize}/>).toJSON();
console.log(toJSON);
}); Produce following error:
|
@piotrooo I believe this is due to not having an async/await transformer in the pipeline. |
@piotrooo how did you fix this issue? I ran into the same thing. Adding
was already helpful, but now I am stuck with the same problem. It seems like
is undefined for the test environment (here Jest). PS.: Why is it so hard to simply run Jest with Webpack :( |
Minimal application showcasing the problem: https://github.com/rwieruch/svgr-async-bug |
@rwieruch it is impossible to use the webpack loader in Jest, you can't do that. I don't know what is the best option to do it but the use-case is not relative to SVGR, you should look for using webpack + Jest together. Maybe build a bundle with webpack, then using this build in Jest. |
That's too bad in my opinion. Not related to this library, but to not being able to combine Jest and Webpack. That's one of the most often used testing setups I have seen out there. It works standalone with its custom setup stuff, but now not being able to add a third-party to the combination is bad for the ecosystem, because I am just not able to test the components using SVG anymore. I try to find a solution for the Jest + Webpack problem. Thanks for your help! Maybe keeping the issue open helps others as well. Maybe we can find a solution together! Would be curious whether @piotrooo found a solution for it. |
Yeah I agree, but you have this problem with all Webpack loaders + Jest. |
I didn't handle it at all... Workaround which works for me, generating icons while bundling project. npm script:
Disclaimer |
We created a simple mock for the svgr loader and mapped to it in the jest config:
In your
Your snapshots will include all properties on the icon components, so they will be tested. |
I need to try this! Thank you @marco-streng 💯 |
It worked for me! 🎉 🎉 🎉 |
I use svgr like this:
Then I changed
Tests pass. |
Thanks @leoendless, I updated the readme. |
@neoziro Whereabouts is this info in the readme? I found this thread first. |
I think the section has disappeared. We should add a section "Testing" on the website. |
Hey 👋 ! I was having issue with both solution on React 16.8.3, React warns about casing. console.error node_modules/react-dom/cjs/react-dom.development.js:506
Warning: <IconMock /> is using incorrect casing. Use PascalCase for React components, or lowercase for HTML elements.
in IconMock
in div
in Unknown (created by WrapperComponent)
in WrapperComponent because, tests were render something like Finally i was able to make it work with: module.exports = { ReactComponent: 'icon-mock' } and now everything is ok |
Thanks man, that worked like a charm, thanks a lot. |
It worked for me with the solution provided by @marco-streng Thank you! Since everything is mocked now, I assume SVG icons will be excluded from visual regression testing now. Would be great to find a solution to still render the SVGs for these kind of tests, but I haven't found one. |
For what it's worth, |
@isaachinman |
If you need to support both Then your import React from 'react';
export default 'SvgrURL';
const SvgrMock = React.forwardRef((props, ref) => <span ref={ref} {...props} />);
export const ReactComponent = SvgrMock; |
Maybe we should add a section in the documentation to document how to test with SVGR. Does someone want to create it? |
Thanks @bobysf12 .
but instead i should be
|
In case this helps anyone, I got this to work with Typescript like so: import React, { SVGProps } from 'react';
const SvgrMock = React.forwardRef<SVGSVGElement, SVGProps<SVGSVGElement>>((props, ref) => (
<svg ref={ref} {...props} />
));
SvgrMock.displayName = 'SvgrMock';
export const ReactComponent = SvgrMock;
export default SvgrMock; I had to also change the mock file's extension to Note: The |
Mine still not solved it's throwing the following error even after mocking import { ReactComponent as ScreenShare } from '@/assets/icons/screenShare.svg'; Error: When using directly svgrMock.js import React from 'react';
const SvgrMock = React.forwardRef((props, ref) => <span ref={ref} {...props} />);
export const ReactComponent = SvgrMock;
export default SvgrMock; jest.config.js moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1',
'\\.(css|less)$': 'identity-obj-proxy',
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': '<rootDir>/src/__mocks__/fileMock.js',
'\\.svg$': '<rootDir>/src/__mocks__/svgrMock.js',
}, |
This was the only thing that worked for me. I am using Next.js + SVGR + typescript thanks mate. |
With solutions above, I can't really test that the import of my svg file is correct. What can I do if I want my test to fail when path to my svg is wrong ? |
tnk you so much |
Hi there, I am using NextJS 12.1.4 with Typescript, React Testing Library and SVGR. So I can import SVGs like this: Here is what I have in
Here is my
and
When running a test of a component including an SVG, I get the following error.
I spent hours trying to solve this, but I couldn't find a solution that worked yet.
Thanks for your help :) |
Hey @denisbarriere, Try require.resolve to moduleNameMapper
|
Having exactly the same issue, same tech stack. Any help would be much appreciated! |
Funny thing:
As you can see I have two svg options point to the same config file; one for svgs with So I console logged the outcome of -> For
-> For
Finally here is my
This suggests that the regular svgs (without query) do not pass through the |
@serhanguney I haven't migrated to Next v12 yet so I'm not talking from experience and this is just a guess. In v12.1 Next added built-in support for Jest, I'm assuming that means they added some additional transformations to handle images including SVGs among other things which is why your plain |
This works for me (instead of // jest.config.js
moduleNameMapper: {
'^.+\\.(svg)$': '<rootDir>/__mocks__/svg.js',
}
moduleDirectories: ['node_modules', '<rootDir>/'] |
see: gregberge/svgr#83 for more reference
WOW It worked for me, Thank you |
1 similar comment
WOW It worked for me, Thank you |
While this transformer works fine in our app, it needs to be mocked in jest otherwise tests that use svg's fail. Documentation https://react-svgr.com/docs/jest/ Helpful debugging threads vercel/next.js#35634 (comment) ihttps://github.com/gregberge/svgr/issues/83#issuecomment-785996587
Thank you, it works for me |
This is awesome. Fixed for me with Next 13 + Jest 29 + svgr 6.5 |
This is working for me but svg component doesn't have any child components in it. It is just imported as
It's kind of blank svg. Please any suggestions would be appreciated. |
For me, Jest complained about "SvgrMock" not being a valid URL (
solved that. Now I get the following error instead...
|
Hi,
I have a little problem with Jest tests runner.
My simple component:
test class:
And after I run this test I got following error:
What I've do wrong?
The text was updated successfully, but these errors were encountered: