0

我在使用模拟孩子测试组件时遇到错误。因此,我要测试的组件 A 有一个组件 B 作为子组件,而组件 C 作为子组件。我正在尝试模拟 C 组件。以下是 C 在其源文件中的样子:

// C.js

export class C extends React.Component {
  constructor(props) {
    super(props);
    autoBind(this);
  }
 // ... code ...
}

在我的 A.test.js 中,我有以下内容

jest.mock('<PATH_TO_C.JS>', () => ({
  C: jest.fn(() => '')
})

在运行测试时,我收到指向我的自定义渲染的错误:TypeError: Cannot read property 'prototype' of undefined

4

1 回答 1

0

您应该返回一个模拟 JSX 元素而不是一个笑话函数,

jest.mock('<PATH_TO_C.JS>', () => ({
  C: () => <div>Sample Component Content</div>
})
于 2021-09-17T04:40:59.807 回答