-
Notifications
You must be signed in to change notification settings - Fork 27.5k
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
feat(next/jest): mock static imports of svg, image and others differently #36907
Conversation
By the way, I want to test the updated The above test code is importing (Above test code should be update too. As far as I know, a statically imported svg file cannot be the |
405f61f
to
65264ef
Compare
To run tests with your changes, you need to build Next.js locally. In development, you should be able to run More about testing here: https://github.com/vercel/next.js/blob/canary/test/readme.md |
So if I run I've read the testing docs for sure but I still don't get which binary will be used when I run a test. I'm asking this because even though I run |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But the thing is, in real world scenario, svg file doesn't get imported in a way that jpg or other image files do. Svg files gets imported by @svgr/webpack so that the import results of svg file and other images end up being different.
This isn't the case in Next.js by default. You likely made customizations to webpack to add another svg loader that turns imported svg files into React components, so this change can't be landed in Next.js as it's particular to your application.
wow.. I have always thought it must be a default settings because I haven't noticed my teammate had set up @svgr/webpack quite a long time ago. I just realized that, I'm sorry. I'll close the PR for now and may open it later if there is a better workaround. thank you! |
Related to #36230 , #35028 . (and PR #34350)
For now, every statically imported file gets mocked by a single
fileMock.js
.As a result, In jest environment, every statically imported file will be mocked as a mock jpg file (to be specific, it is a
StaticImageData
-typed object). But the thing is, in real world scenario, svg file doesn't get imported in a way that jpg or other image files do. Svg files gets imported by@svgr/webpack
so that the import results of svg file and other images end up being different. You can observe this by simply doconsole.log
both imported files.So to make both svg file and other image files get imported properly under jest environment,
fileMock.js
should be split into each dedicated mock files as below.In
svgMock.js
, I tried to mimic the way how@svgr/webpack
treats a svg file. If a svg file gets mocked in such way, now svg-as-component will work well in jest environment too. (ex.<SvgFile width={10} height={20} />
).Bug
fixes #number
contributing.md
Feature
fixes #number
contributing.md
Documentation / Examples
yarn lint