Skip to content

Commit 593039f

Browse files
Fix svgr in storybook & jest (#1428)
* fix svg loader * added Logo component to be able to test storybook * added svg mock for svgr as suggested (still not working) * package cleanup * make svg moduleNameMapper match first * cleanup Co-authored-by: Matthias Kretschmann <m@kretschmann.io>
1 parent a5819ba commit 593039f

File tree

8 files changed

+55
-266
lines changed

8 files changed

+55
-266
lines changed

.jest/__mocks__/svgrMock.tsx

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
SvgrMock.displayName = 'SvgrMock'
8+
9+
export const ReactComponent = SvgrMock
10+
export default SvgrMock

.jest/jest.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const customJestConfig = {
1414
moduleDirectories: ['node_modules', '<rootDir>/src'],
1515
testEnvironment: 'jest-environment-jsdom',
1616
moduleNameMapper: {
17+
'\\.svg': '<rootDir>/.jest/__mocks__/svgrMock.tsx',
1718
// '^@/components/(.*)$': '<rootDir>/components/$1',
1819
'@shared(.*)$': '<rootDir>/src/components/@shared/$1',
1920
'@hooks/(.*)$': '<rootDir>/src/@hooks/$1',

.storybook/main.js

+19-14
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,26 @@ module.exports = {
1414
]
1515

1616
// Mimic next.config.js webpack config
17-
config.module.rules.push(
18-
{
19-
test: /\.svg$/,
20-
issuer: /\.(tsx|ts)$/,
21-
use: [
22-
{ loader: require.resolve('@svgr/webpack'), options: { icon: true } }
23-
]
24-
},
25-
{
26-
test: /\.gif$/,
27-
// yay for webpack 5
28-
// https://webpack.js.org/guides/asset-management/#loading-images
29-
type: 'asset/resource'
30-
}
17+
config.module.rules.push({
18+
test: /\.gif$/,
19+
// yay for webpack 5
20+
// https://webpack.js.org/guides/asset-management/#loading-images
21+
type: 'asset/resource'
22+
})
23+
24+
// Modify storybook's file-loader rule to avoid conflicts with svgr
25+
const fileLoaderRule = config.module.rules.find(
26+
(rule) => rule.test && rule.test.test('.svg')
3127
)
28+
fileLoaderRule.exclude = /\.svg$/
29+
30+
config.module.rules.push({
31+
test: /\.svg$/,
32+
issuer: /\.(tsx|ts)$/,
33+
use: [
34+
{ loader: require.resolve('@svgr/webpack'), options: { icon: true } }
35+
]
36+
})
3237

3338
const fallback = config.resolve.fallback || {}
3439
Object.assign(fallback, {

package-lock.json

-246
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@
118118
"process": "^0.11.10",
119119
"serve": "^13.0.2",
120120
"stream-http": "^3.2.0",
121-
"ts-jest": "^28.0.2",
122121
"tsconfig-paths-webpack-plugin": "^3.5.2",
123122
"typescript": "^4.6.4"
124123
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from 'react'
2+
import { ComponentStory, ComponentMeta } from '@storybook/react'
3+
4+
import Logo, { LogoProps } from '@shared/atoms/Logo'
5+
6+
export default {
7+
title: 'Component/@shared/atoms/Logo',
8+
component: Logo
9+
} as ComponentMeta<typeof Logo>
10+
11+
const Template: ComponentStory<typeof Logo> = (args) => <Logo {...args} />
12+
13+
interface Props {
14+
args: LogoProps
15+
}
16+
17+
export const Primary: Props = Template.bind({})
18+
Primary.args = {
19+
noWordmark: true
20+
}

0 commit comments

Comments
 (0)