javascript - EnzymeJest: How to test if mocked function has been called - Stack Overflow

I'm trying to test for the call of fetch() inside of my changeIt() function using jestenzyme.But

I'm trying to test for the call of fetch() inside of my changeIt() function using jest/enzyme. But obviously I'm doing something wrong:

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

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

I'm trying to test for the call of fetch() inside of my changeIt() function using jest/enzyme. But obviously I'm doing something wrong:

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

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
})
Share Improve this question edited Oct 31, 2018 at 15:12 skyboyer 23.8k7 gold badges62 silver badges71 bronze badges asked Oct 20, 2018 at 8:01 user3142695user3142695 17.4k55 gold badges199 silver badges375 bronze badges 3
  • 1 have you mocked 'node-fetch'? – axm__ Commented Oct 20, 2018 at 8:06
  • @axm__ yes, like shown in the test code – user3142695 Commented Oct 20, 2018 at 8:09
  • But then you create a new local mock function, unrelated to node-fetch, inside the test function. Look again at jestjs.io/docs/en/mock-functions.html#mocking-modules – jonrsharpe Commented Oct 20, 2018 at 8:21
Add a ment  | 

1 Answer 1

Reset to default 5

You need to properly mock the node-fetch module. 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-fetch.js

Inside node-fetch.js put:

export default jest.fn();

Finally import fetch in your test file and mock it like this:

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744943516a4602474.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信