Avoid jest svg failures using NextJS + @svgr/webpack #793
cam-eo
started this conversation in
Show and tell
Replies: 1 comment 1 reply
-
I think it could be helpful to post this on Next because you are actually struggling to add a |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I faced this issue a lot personally whenever trying to use new versions of NextJS with Jest. All my unit tests for components that import SVGs break the tests.
For changing colours and sizes and making reusable icons (when there is no good icon library or custom icons) it is just more convenient to use SVG's as React Components
I've seen various methods of using a stub/mock for svgs in the jest config but I had trouble moving from Next 12.1.6 to anything higher.
But after much suffering I came to this solution
jest.config.ts
Basically once you have setup the configuration, you need to setup the mock for svg first in the config
moduleNameMapper
list.The full POC can be seen here
Since I struggled so much I thought I would share to help others. If anyone has more elegant solutions I'm keen to here about it :)
Beta Was this translation helpful? Give feedback.
All reactions