Jest Transformation Issue with SVG Imports in Next.js Project with SWC #52234
Unanswered
behnamazimi
asked this question in
Help
Replies: 1 comment 1 reply
-
Ah, IIRC, you have to do a small bypass in the const nextJest = require("next/jest");
const createJestConfig = nextJest({
dir: "./",
});
/** @type {import('jest').Config} */
const customJestConfig = {
setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"],
moduleNameMapper: {
"^@/(.*)$": "<rootDir>/src/$1",
},
// Since Jest 28, IIRC, this has to be installed separately
testEnvironment: "jest-environment-jsdom",
};
const jestConfigWithOverrides = async (...args) => {
const fn = createJestConfig(customJestConfig);
const res = await fn(...args);
res.moduleNameMapper = {
"\\.svg": "<rootDir>/src/__mocks__/svgrMock.js",
...res.moduleNameMapper,
};
return res;
};
module.exports = jestConfigWithOverrides; For example, something like this, where the svgMock.js is: export const ReactComponent = "div";
// eslint-disable-next-line import/no-anonymous-default-export
const SvgUrl = () => "svgrurl";
export default SvgUrl; It is not exactly what you need I think, but it is along the lines. |
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
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
Jest seems to be unable to transform SVG imports correctly, resulting in an error message that says:
Details:
I am currently migrating a Next.js project from Babel to SWC. However, I encountered an issue when using Jest for testing. Jest is not transforming SVG imports correctly, resulting in an object instead of the expected SVG component. The error message I receive is:
here's an example of the SVG import I am using:
and transform result looks like this when I log "RemoveIcon" after import and run jest:
What I tried
I attempted to address this issue by using the
jest-svg-transformer
and adding a custom transform option forsvg
files in myjest.config.js
file. Neither of these approaches worked.I also tried to mock the "@FortAwesome" module like below, but it does not work either.
If necessary, please let me know if there are any additional details or code samples I can provide to assist in troubleshooting this issue.
Thank you in advance for your attention and support.
Additional information
No response
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions