首页 > 解决方案 > TypeError:fetch.mockResponse 不是函数

问题描述

遇到以下错误:TypeError: fetch.mockResponse is not a function

尝试测试以下内容:

getRequests = () => {
  let headersObj = {
    'Content-Type': 'application/json',
    'Ocp-Apim-Subscription-Key':  this.props.services.Dashboard.subscription_key,
   }
  fetch(`${this.props.services.Dashboard.URL}/REQUEST`, {
    headers: headersObj,
    method: 'GET',
    credentials: 'include',
  }).then(res => res.json())
  .then(json => {
    if(json.value) {
      this.setState({Requests: json.value, allowNext: true})
   }
 })
}

到目前为止,这是我所拥有的:不确定我是否走在正确测试的正确道路上,但我将不胜感激,谢谢

beforeEach(() => wrapper = mount(<MemoryRouter keyLength={0}><FileName {...baseProps} /></MemoryRouter>)


it('Test getRequests function ',() => {
  wrapper.find('FileName').setState({
    Requests: [],
    allowNext: true
    });

 fetch.mockResponse(JSON.stringify([]));
 wrapper.update();
 expect(wrapper.find('FileName').instance().getRequests('test')).toEqual()
 expect(wrapper.find('FileName').state('allowNext')).toBeTruthy();

标签: reactjsjestjsfetchenzyme

解决方案


mockResponse不是 Fetch API 中的内置函数 - 看起来您已经遵循了一个使用jest-mock-fetch库的示例,该库添加了各种模拟辅助函数,包括mockResponse


推荐阅读