Possible to respond differently based on order of calls?

This issue has been tracked since 2017-11-09.

Trying to test retry logic of a method, so I've it wired up like this:

    moxios.wait(() => {
      const request = moxios.requests.at(0)
      request.respondWith({
        status: 401
        , response: mockApiError
      })

      const nextRequest = moxios.requests.at(1)
      nextRequest.respondWith({
        status: 200
        , response: mockApiResponse
      })
    })

    const mockRequest = {
      headers: {}
      , method: 'get'
      , url: 'to/no/where'
    }

    const response = await tryMakeRequest(mockRequest)

    expect(response.status).toBe(200)
    expect(response.data).toEqual(mockApiResponse)

The test fails with this error:

    Timeout - Async callback was not invoked within timeout specified by
jasmine.DEFAULT_TIMEOUT_INTERVAL.

      at node_modules/jest-jasmine2/build/queue_runner.js:64:21
      at Timeout.callback [as _onTimeout] (node_modules/jsdom/lib/jsdom/browser/Window.js:523:19)
      at ontimeout (timers.js:469:11)
      at tryOnTimeout (timers.js:304:5)
      at Timer.listOnTimeout (timers.js:264:5)

Any ideas what am I missing here?

mrchief wrote this answer on 2017-11-09

For the time being, I'm using axios-mock-adapter and this works:

    mock.onGet('/to/no/where').replyOnce(401, mockApiError)
      .onGet('/to/no/where').replyOnce(200, mockApiResponse)
anilanar wrote this answer on 2018-05-27

Kamino cloned this issue to anilanar/moxios

omrilotan wrote this answer on 2020-03-12

Couldn't find a resolution for this. This is how I addressed this issue, if it helps the next poor soul:

In this example, the same route fails the first time, then succeeds for subsequent requests. This logic can be expanded to check anything from authentication flow to rate limit

// Initial setup
class MockResponse {
  constructor() {
    this.iterations = 0;
  }
  get response() {
    this.iterations++;
    return this.iterations === 1 ? 'Forbidden' : 'Allowed';
  }
  get status() {
    return this.iterations === 1 ? 403 : 200;
  }
}

// Register in beforeEach
moxios.stubRequest('/route', new MockResponse());

// First call fails: gets 403
expect(axios.get('/route')).rejects.toThrow('Request failed with status code 404');

// The next one's okay: gets 200
const { status } = await axios.get(REQUEST_PERIMETERX_BLOCK);
expect(status).toBe(200);
veryneatperson wrote this answer on 2020-04-29

omrilotan, thanks a lot.
Kinda hard to find reasonable solutions for that.
It really works (even though feels kinda unnatural).

By the way, as i understand, moxios.stubRequest first calls get response and then get status.

omrilotan wrote this answer on 2020-04-30

@veryneatperson You're right

moxios/index.js

Lines 268 to 269 in 3785075

this.data = res.responseText || res.response;
this.status = res.status

I'll switch them in the comment

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