Jest React 无法读取 setState 上未定义的属性“createEvent”

Che*_*erd 6 javascript json typeerror reactjs jestjs

当用户登录时,我的组件应该检索位置列表。
我正在使用fetch-mock模拟获取请求并返回组件在安装时将使用的一些数据。

class Comp extends Component {

    componentDidMount() {
        this.getLocations();
    }

    getLocations = () => {
        const url = 'the-url';

        fetch(url)
            .then((response) => {
                return response.json()
            })
            .then(json => {
                // json = {"value": [{"id": 0, "name": "Test Location"}]
                this.setState({
                    locations: json.value
                })
            })
            .catch(err => console.log('getLocations err', err))
    };
}
Run Code Online (Sandbox Code Playgroud)

我已经记录了 json 并且输入是在评论中定义的。

我得到的错误是getLocations err TypeError: Cannot read property 'createEvent' of undefined

调用堆栈底部给出的行是this.setState({locations: json.value});并且日志记录this按预期显示了反应组件的实例。

谁能帮我这个?

技术:

  • 反应 16.2.0
  • 开玩笑 21.2.1
  • 反应测试渲染器 16.2.0
  • 巴别开玩笑 21.2.0