Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make wait only resolve currently queued requests #10

Closed
scottmas opened this issue Sep 14, 2016 · 2 comments
Closed

Make wait only resolve currently queued requests #10

scottmas opened this issue Sep 14, 2016 · 2 comments

Comments

@scottmas
Copy link

scottmas commented Sep 14, 2016

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.

@scottmas scottmas changed the title Make wait only clear resolve currently queued requests Make wait only resolve currently queued requests Sep 16, 2016
@mzabriskie
Copy link
Member

mzabriskie commented Sep 23, 2016

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
Copy link
Author

scottmas commented Jan 27, 2017

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants