axiosmoxios

Moxios: How to access requests that aren't the most recent in unit tests


A have a page that on render sends out multiple HTTP get requests for data. How do I filter through moxios.requests to get the request with a certain URL to respond to?

The only method I can find online is moxios.requests.mostRecent(), however I'm not certain what the most recent request would be. Even then, is there a another method to pop another request?

There doesn't seem to be any documentation on this on the moxios library here: https://github.com/axios/moxios

 let request = moxios.requests.mostRecent() // How to get 'some/url/' here? This expect is currently failing
      expect(request.url).to.equal('some/url/')
      request.respondWith({
        status: 200,
        response: []
}

Solution

  • You can access requests by index like so: moxios.requests.at(0) for the first request, moxios.requests.at(1) for the next one and so on. I found that out here: https://github.com/axios/moxios/issues/10