开玩笑 - `无法为组件创建样式组件:未定义`

jxi*_*o23 9 jestjs babel-jest styled-components

使用测试我的应用程序时遇到以下错误jest

\n
 FAIL  \n  \xe2\x97\x8f Test suite failed to run\n\n    Cannot create styled-component for component: undefined.\n\n      30 |\n      31 | \n    > 32 | export const BackgroundVector = styled(Background)`\n         |                                 ^\n      33 |   position: fixed;\n      34 |   left: 0;\n      35 |   bottom: 0;\n
Run Code Online (Sandbox Code Playgroud)\n

Background是我在此文件顶部导入的 svg,如下所示:

\n
import { ReactComponent as Background } from \'../images/Background.svg\';\n
Run Code Online (Sandbox Code Playgroud)\n

我不太确定如何解决这个错误。在我的中package.json,我已将 SVG 文件映射到fileMock.js一个module.exports = \'test-file-stub\';. 我还应该做些什么来解决这个问题吗?

\n

lou*_*lyl 16

您可以尝试以下操作:

export const BackgroundVector = styled(props => <Background {...props}/>)`
  //Your styles here
`
Run Code Online (Sandbox Code Playgroud)

这解决了我的问题。