Javascript Ezyme/Jest:如何测试是否调用了模拟函数

Javascript Ezyme/Jest:如何测试是否调用了模拟函数,javascript,reactjs,unit-testing,jestjs,enzyme,Javascript,Reactjs,Unit Testing,Jestjs,Enzyme,我正在尝试使用jest/enzyme测试changeIt()函数内部的fetch()调用。 但很明显我做错了什么: example.js import fetch from 'node-fetch' export default class Example extends Component { changeIt (id, value) { fetch('http://localhost/set-status?id=' + id + '&value=' + value)

我正在尝试使用jest/enzyme测试
changeIt()
函数内部的
fetch()
调用。 但很明显我做错了什么:

example.js

import fetch from 'node-fetch'

export default class Example extends Component {
  changeIt (id, value) {
    fetch('http://localhost/set-status?id=' + id + '&value=' + value)
  }

  render () {
    return (
      <div>something </div>
    )
  }
}
jest.mock('node-fetch')
test('should call fetch()', () => {
  const id = 1
  const value = 50
  const fetch = jest.fn() // <- This is wrong
  const wrapper = shallow(<Example />)
  wrapper.instance().changeIt(id, value)
  expect(fetch).toHaveBeenCalled() // <- This is wrong
})
从“节点提取”导入提取
导出默认类示例扩展组件{
更改它(id、值){
取('http://localhost/set-status?id=“+id+”&值=”+值)
}
渲染(){
返回(
某物
)
}
}
example.test.js

import fetch from 'node-fetch'

export default class Example extends Component {
  changeIt (id, value) {
    fetch('http://localhost/set-status?id=' + id + '&value=' + value)
  }

  render () {
    return (
      <div>something </div>
    )
  }
}
jest.mock('node-fetch')
test('should call fetch()', () => {
  const id = 1
  const value = 50
  const fetch = jest.fn() // <- This is wrong
  const wrapper = shallow(<Example />)
  wrapper.instance().changeIt(id, value)
  expect(fetch).toHaveBeenCalled() // <- This is wrong
})
jest.mock('node-fetch'))
测试('should call fetch()',()=>{
常数id=1
常数值=50

const fetch=jest.fn()/您需要正确地模拟
节点获取
模块。因为它位于
节点获取模块
中,您需要将
节点获取
放在
节点获取
文件夹中,与
节点获取模块
处于同一级别,如:

├── node_modules/
│   ├── node-fetch/
├── __mocks__/
│   ├── node-fetch.js
内部
node fetch.js
put:

导出默认jest.fn();
最后在测试文件中导入
fetch
,并如下模拟:

import Example from './Bla';
import { shallow } from 'enzyme';
import React from 'react';
import fetch from 'node-fetch';
/**
 * Important! Import the mocked function.
 * Start the mocking with jest.mock('node-fetch').
 * Stop the mocking with jest.unmock('node-fetch').
 */    
jest.mock('node-fetch');

test('should call fetch()', () => {
  const id = 1
  const value = 50
  const wrapper = shallow(<Example />)
  wrapper.instance().changeIt(id, value)
  expect(fetch).toHaveBeenCalled() // now it works
})
从“/Bla”导入示例;
从“酶”导入{shall};
从“React”导入React;
从“节点获取”导入获取;
/**
*重要提示!导入模拟函数。
*用jest.mock('node-fetch')开始模拟。
*使用jest.unmack('node-fetch')停止模拟。
*/    
mock('node-fetch');
测试('should call fetch()',()=>{
常数id=1
常数值=50
常量包装器=浅()
wrapper.instance().changeIt(id,值)
expect(fetch).toHaveBeenCalled()//现在它可以工作了
})

您需要正确模拟
节点获取
模块。因为它位于
节点单元
中,所以您需要将
节点获取
放在与
节点单元
相同级别的
节点获取
文件夹中,如:

├── node_modules/
│   ├── node-fetch/
├── __mocks__/
│   ├── node-fetch.js
内部
node fetch.js
put:

导出默认jest.fn();
最后在测试文件中导入
fetch
,并如下模拟:

import Example from './Bla';
import { shallow } from 'enzyme';
import React from 'react';
import fetch from 'node-fetch';
/**
 * Important! Import the mocked function.
 * Start the mocking with jest.mock('node-fetch').
 * Stop the mocking with jest.unmock('node-fetch').
 */    
jest.mock('node-fetch');

test('should call fetch()', () => {
  const id = 1
  const value = 50
  const wrapper = shallow(<Example />)
  wrapper.instance().changeIt(id, value)
  expect(fetch).toHaveBeenCalled() // now it works
})
从“/Bla”导入示例;
从“酶”导入{shall};
从“React”导入React;
从“节点获取”导入获取;
/**
*重要提示!导入模拟函数。
*用jest.mock('node-fetch')开始模拟。
*使用jest.unmack('node-fetch')停止模拟。
*/    
mock('node-fetch');
测试('should call fetch()',()=>{
常数id=1
常数值=50
常量包装器=浅()
wrapper.instance().changeIt(id,值)
expect(fetch).toHaveBeenCalled()//现在它可以工作了
})

是否模拟了“节点提取”?@axm_uuuu是的,如测试代码所示,但随后在测试函数内创建了一个与节点提取无关的新本地模拟函数。再次查看是否模拟了“节点提取”?@axm_uuuu是的,如测试代码所示,但随后在测试函数内创建了一个与节点提取无关的新本地模拟函数在上。再看一次unmock,它正在工作。然后我不需要我不太清楚的
jest.mock('node-fetch')
,我会编辑它。你仍然需要这样做,因为每次测试你都可以mock/unmock.unmock,它正在工作。然后我就不需要
jest.mock('node-fetch'))
我有点不清楚,我会编辑它。你仍然需要这样做,因为每次测试你都可以模拟/取消模拟。