[英]Invariant Violation: Element type is invalid (Jest + TSX + StyledComponents)
I have been struggling with running test using Jest when I use styled-components . 当我使用样式组件时,我一直在努力使用Jest运行测试。
I took the example code from styled-components/jest 我从styled-components / jest中获取了示例代码
import * as React from 'react'
import styled from 'styled-components'
import * as TestRenderer from 'react-test-renderer'
import 'jest-styled-components'
const Button = styled.button`
color: red;
`
test('it works', () => {
const tree = TestRenderer.create(<Button />).toJSON()
expect(tree).toMatchSnapshot()
expect(tree).toHaveStyleRule('color', 'red')
})
and I always and up with following error: 我总是和以下错误:
Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
The test file is .tsx. 测试文件是.tsx。 Jest config is following:
笑话配置如下:
module.exports = {
"roots": [
"<rootDir>/src"
],
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
]
}
Well, eventually I ended up using enzyme for testing. 好吧,最终我最终使用酶进行了测试。 Did not really resolve this, but managed to make it working.
并未真正解决此问题,但设法使其正常运行。
Also decided not to use jest-styled-components
since last versions have some troubles on its own. 还决定不使用
jest-styled-components
因为最后一个版本本身就有一些麻烦。 Hopefully I will manage that later. 希望以后能解决。
import * as React from 'react'
import styled from 'styled-components'
const Button = styled.button`
color: red;
`
test('it works', () => {
const tree = shallow(<Button />);
expect(tree).toMatchSnapshot()
})
Needless to say that you must update your jest.config.js
to make shallow
working: 不用说,您必须更新
jest.config.js
才能使工作更shallow
:
"snapshotSerializers": [
"enzyme-to-json/serializer"
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.