TypeError:fetch.mockResponse不是函数

时间:2019-04-25 13:15:17

标签: reactjs jestjs fetch enzyme

遇到以下错误: TypeError:fetch.mockResponse不是函数

尝试测试以下内容:

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();

1 个答案:

答案 0 :(得分:2)

mockResponse不是Fetch API中的内置函数-您似乎已经遵循了一个使用jest-mock-fetch库的示例,该库添加了各种模拟助手功能,包括{{ 3}}。