简体   繁体   English

不变违规:元素类型无效(Jest + TSX + StyledComponents)

[英]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.

相关问题 错误:不变违规:元素类型无效 - Error: Invariant Violation: Element type is invalid 当我使用 jest 来测试 React 组件时,它会抛出“不变违规:元素类型无效:需要一个字符串(对于内置组件)” - when I use jest to test react component it throws "Invariant Violation:Element type is invalid: expected a string (for built-in components)" [React-Native]不变违规:元素类型无效 - [React-Native]Invariant Violation: Element type is invalid Jestjs与Expo-cli。 永久违反:元素类型无效 - Jestjs with Expo-cli. Invariant Violation: Element type is invalid 玩笑不变 - Jest Invariant Violation 玩笑测试错误-始终违反:目标容器不是DOM元素 - Jest Testing Error - Invariant Violation: Target container is not a DOM element 不变违规:元素类型无效:使用酶安装测试时需要一个字符串(对于内置组件) - Invariant Violation: Element type is invalid: expected a string (for built-in components) when testing with enzyme mount 不变违规:getNodeFromInstance:参数无效 - Invariant Violation: getNodeFromInstance: Invalid argument 无法在Jest测试中使用webpack别名-不变违规:目标容器不是DOM元素 - Unable to use webpack alias with Jest testing - Invariant Violation: Target container is not a DOM element ReactTestRenderer:不变违例:getNodeFromInstance:参数无效 - ReactTestRenderer: Invariant Violation: getNodeFromInstance: Invalid argument
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM