-
Notifications
You must be signed in to change notification settings - Fork 55
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
Compatibility of version 4.0 and jest #97
Comments
So I was able to find a workaround with a combination of two things:
|
Thanks I've made it worked. Note that I've used a generic transform in my jest config so that I didn't need to mock each svg. The solution that worked for me:
transform: {
'^.+\\.svg$': `<rootDir>/jest/__mocks__/svg.js`,
}
module.exports = {
process() {
return {
code: 'module.exports = () => null;',
}
},
getCacheKey() {
// The output is always the same.
return 'svgTransformSvgr4'
},
} |
@pcorpet Your solution is much nicer. I did have a bit of a hiccup where my tests were still failing with trying to load the SVG, but it was due to a caching problem. I ran Jest once with |
After some trial and error, I went about making v4 work with jest in a different way that seems to be working. In import type { Config } from '@jest/types';
const config: Config.InitialOptions = {
// ...
moduleNameMapper: {
// ...
'^(.+\\.svg)\\?react$': '<rootDir>/src/__mocks__/svg.tsx',
},
// ...
};
module.exports = config; In import React from 'react';
const SvgrMock = React.forwardRef<HTMLSpanElement>((props, ref) => (
<span ref={ref} {...props} />
));
SvgrMock.displayName = 'SvgrMock';
export const ReactComponent = SvgrMock;
export default SvgrMock; |
I had to add also
|
I've come up with some other idea.
moduleNameMapper: {
'^(.*).svg\\?react$': '$1.svg',
}
transform: {
'^.+\\.svg(\\?react)?$': 'jest-transformer-svg',
}, With this setup svgs are rendered like this:
|
I use jest for my test and it's choking on the new syntax introduced in 4.0 (Resource Queries). Jest themselves are planning on supporting it but the PR is not moving and it has been a while.
Do you have an idea on how I could make the upgrade to v4 and keep my jest tests? Maybe there's a way to make the resource query syntax optional?
The text was updated successfully, but these errors were encountered: