I
I
Ismail2020-04-26 11:48:22
Unit testing
Ismail, 2020-04-26 11:48:22

Testing axios request in enzyme + jest?

Good afternoon. I want to test a POST request to the server using axios. For some reason, the function is not called thenafter sending the request:

Test

it('should have ok response', () => {
    const data = [];
    const createBoard = () => {
      axios.post(`${API_ROOT}/boards`).then(response => {
        console.log('data:', response);
        return data.push(response.data);
      })
    }
    const component = shallow(<CreateBoardButton onClick={createBoard} text="CREATE BOARD" />);
    component.find('#create_board_button').simulate('click');
  })



When this happens, the function is called. Something is wrong here?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
grinat, 2020-04-26
@grinat

So you don't need to test, you need to pass jest.fn () there instead of a real call.
And to see it, if you still want to make http requests (which is fundamentally wrong), do something like:
await new Promise(resolve => setTimeout(resolve, 2000))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question