Avoid jest svg failures using NextJS + @svgr/webpack #42535
Unanswered
cam-eo
asked this question in
Show and tell
Replies: 2 comments 5 replies
-
@cam-eo Thanks. This resolved our issue. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Beta Was this translation helpful? Give feedback.
5 replies
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 jest unit 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