【发布时间】:2020-12-15 20:01:19
【问题描述】:
在为父组件运行测试文件时收到Error: Uncaught [Error: Child(...): Nothing was returned from render。
这些是相关文件
/components/Page/Children/Child.js
import React from "react"
export default function Child() {
return <div>abc</div>
}
/components/Page/Children/index.js
export { default } from "./Child"
/components/Page/Parent.js
import React from "react"
import Child from "./Children"
export default class Parent extends React.Component {
constructor(props) {
super(props)
}
render() {
return <Child />
}
}
/components/Page/_tests_/Parent.spec.js
import Parent from "../Parent"
jest.mock("../Children")
describe("<Parent/>", () => {
let wrapper
let props = ...
describe("render", () => {
it("renders", () => {
wrapper = mount(<Parent props={props} />)
expect(wrapper).toMatchSnapshot()
})
})
将 Child.js 文件更改为 react 类组件(如下更新)可以解决问题,但我不明白为什么会这样。
/components/Page/Children/Child.js
import React from "react"
export default class Child extends React.Component {
render() {
return <div>abc</div>
}
}
【问题讨论】:
-
您是否检查过您是否正确导入了它? stackoverflow.com/questions/37200080/…
标签: javascript reactjs jestjs