3

I am working through a udemy course and it's on the testing portion. In it, the instructor uses moxios to stub requests. However, it is not using the stubbed response. The stubbed response should only be returning 2 items, where as the actual API call returns 500. So my test is hitting the actual api and not the stubbed request. I have tried the call to mock the response in both the test and the beforeEach functions.

Any advice on what I'm doing incorrectly?

beforeEach(() => {
  moxios.install()
  moxios.stubRequest(
    'http://jsonplaceholder.typicode.com/comments',
    {
      status: 200,
      response: [{ name: 'Fetched #1' }, { name: 'Fetched #2' }]
    }
  )
}) 

it('should fetch list of comments and display them', (done) => {
  const component = mount(
    <Root>
      <App />
    </Root>
  )


  component.find('#fetch-comments').simulate('click')

  moxios.wait(() => {
    component.update()
    expect(component.find('li').length).toEqual(2)

    done()
    component.unmount()
  }, 500)
});

0 Answers0