Make wait only resolve currently queued requests

This issue has been tracked since 2016-09-14.

The setTimeout implementation of wait() causes requests that get queued as a result of a prior axios requests to ALSO be fulfilled.

Consider the following script which gets executed.

axios.get('https://www.google.com').then(() => {
   axios.get('https://www.yahoo.com')
})

Our test file looks like so:

it('calls appropriate requests', function(done){
  moxios.wait(() => {
    expect(moxios.requests.mostRecent().config.url).to.equal('https://www.google.com')
    moxios.wait(() => {
       expect(moxios.requests.mostRecent().config.url).to.equal('https://www.yahoo.com')
       done()
    })
  })
})

But this doesn't work, since yahoo.com becomes the last request after the first wait().

We probably need two methods, one for resolving already queued requests at the time wait() is invoked, and one for waiting until all moxios requests (including ones triggered in moxios promise handlers) are finished - maybe named moxios.waitAll()?

I'm no guru of testing frameworks, but this behavior seems desirable and typical since it gives developers much more control over what requests actually get initiated, and lets them inspect the application state at every step of the process rather than only at the very end.

mzabriskie wrote this answer on 2016-09-23

mostRecent() is a convenience for accessing the last request. In your case yahoo.com would be the most recent as it was requested last. In cases where you have multiple requests, as you described, you can also access the requests by index: moxios.requests.at(0) would be google.com in your example and moxios.requests.at(1) would be yahoo.com.

scottmas wrote this answer on 2017-01-27

This is accurate, but I don't think it should be that way. Here's how I think of it:

The script gets executed. axios.get('https://www.google.com') gets added to the queue of pending promises.

The queue is cleared by some mechanism (possibly moxios.wait()) and the promise is resolved and removed from the queue.

As it gets removed, the handlers attached to it are invoked, causing the request to yahoo to trigger, thus adding another promise to the recently cleared queue.

The queue is flushed again by another call to moxios.wait() and finally the last promise gets resolved.

Does this make sense?

More Details About Repo
Owner Name axios
Repo Name moxios
Full Name axios/moxios
Language JavaScript
Created Date 2016-05-25
Updated Date 2022-12-05
Star Count 1386
Watcher Count 20
Fork Count 85
Issue Count 19

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date