Skip to content
This repository was archived by the owner on Dec 19, 2024. It is now read-only.

Commit 0f17fd5

Browse files
committed
fix(jest): mock @svg/webpack transformer
While this transformer works fine in our app, it needs to be mocked in jest otherwise tests that use svg's fail. Documentation https://react-svgr.com/docs/jest/ Helpful debugging threads vercel/next.js#35634 (comment) ihttps://github.com/gregberge/svgr/issues/83#issuecomment-785996587
1 parent 8f4ccfe commit 0f17fd5

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

frontend/jest.config.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,22 @@ const customJestConfig = {
2525
transform: {
2626
'.+\\.(css|style|less|sass|scss)$': 'jest-css-modules-transform',
2727
},
28-
transformIgnorePatterns: [],
2928
watchPlugins: [
3029
'jest-watch-typeahead/filename',
3130
'jest-watch-typeahead/testname',
3231
],
3332
};
3433

35-
const asyncConfig = createJestConfig(customJestConfig);
36-
3734
module.exports = async () => {
38-
const config = await asyncConfig();
39-
// next/jest ignores node_modules and allows to add more ignore patterns, but we need to override them fully to whitelist some node_modules or leave as an empty array
40-
// https://github.com/vercel/next.js/blob/canary/packages/next/build/jest/jest.ts
41-
config.transformIgnorePatterns = customJestConfig.transformIgnorePatterns;
42-
43-
return config;
35+
const nextJestConfig = await createJestConfig(customJestConfig)();
36+
return {
37+
...nextJestConfig,
38+
moduleNameMapper: {
39+
'\\.svg': '<rootDir>/tests/jest/__mocks__/svg.tsx',
40+
...nextJestConfig.moduleNameMapper,
41+
},
42+
// next/jest ignores node_modules and allows to add more ignore patterns, but we need to override them fully to whitelist some node_modules or leave as an empty array
43+
// https://github.com/vercel/next.js/blob/canary/packages/next/build/jest/jest.ts
44+
transformIgnorePatterns: [],
45+
};
4446
};

frontend/tests/jest/__mocks__/svg.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React, { SVGProps } from 'react';
2+
3+
const SvgrMock = React.forwardRef<SVGSVGElement, SVGProps<SVGSVGElement>>(
4+
(props, ref) => <svg ref={ref} {...props} />
5+
);
6+
7+
export const ReactComponent = SvgrMock;
8+
export default SvgrMock;

0 commit comments

Comments
 (0)