简体   繁体   English

酶/玩笑:如何测试是否调用了模拟功能

[英]Enzyme/Jest: How to test if mocked function has been called

I'm trying to test for the call of fetch() inside of my changeIt() function using jest/enzyme. 我正在尝试使用玩笑/酶测试在changeIt()函数内部对fetch()的调用。 But obviously I'm doing something wrong: 但显然我在做错事:

example.js 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>
    )
  }
}

example.test.js example.test.js

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
})

You need to properly mock the node-fetch module. 您需要正确模拟node-fetch模块。 Because it is in node_modules , you need to put node-fetch inside a __mocks__ folder on the same level as node_modules like: 由于它位于node_modules ,因此您需要将node-fetch放在__mocks__文件夹中,位于与node_modules相同级别的node_modules例如:

├── node_modules/
│   ├── node-fetch/
├── __mocks__/
│   ├── node-fetch.js

Inside node-fetch.js put: node-fetch.js内部放:

export default jest.fn();

Finally import fetch in your test file and mock it like this: 最后,将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
})

Read more about mocking node_modules packages in jest here. 在这里阅读更多关于嘲笑node_modules软件包的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查是否已开玩笑地调用了模拟的内部 function? - How to check if mocked inner function has been called in jest? 如果正确应用了内联样式,如何使用Jest / Enzyme进行测试 - How to test with Jest/Enzyme if an inline styles has been applied correctly Jest嘲笑间谍功能,而不是在测试中调用 - Jest mocked spy function, not being called in test 测试 object 的 function 是否已在另一个 function 中被调用 - Test if function of object has been called inside another function in jest 如何测试从导入的函数调用内部函数? (与Jest.js) - How to test that an inner function has been called from an imported function? (with Jest.js) 错误:预期的模拟函数已被调用-onclick笑话酶 - Error: expected mock function to have been called - onclick Jest enzyme 开玩笑的模拟实现未在测试函数外部调用 - Jest mocked implementation not being called outside of test function 反应 formik,测试是否在点击时调用提交函数 - 开玩笑,酶 - react formik, test if submit function called on click - jest,enzyme 当用条件包装了ReactDOM.render时,如何用Jest进行测试? - How to test with Jest that ReactDOM.render has been called when it has been wrapped in a conditional? 如何测试在React组件内部被称为笑话和酶的功能 - How to test a function in jest and enzyme which is being called inside a React component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM