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

Possible to respond differently based on order of calls? #39

Closed
mrchief opened this issue Nov 9, 2017 · 6 comments
Closed

Possible to respond differently based on order of calls? #39

mrchief opened this issue Nov 9, 2017 · 6 comments

Comments

@mrchief
Copy link

mrchief commented Nov 9, 2017

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

mrchief commented Nov 9, 2017

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

Kamino cloned this issue to anilanar/moxios

@omrilotan
Copy link

omrilotan commented Mar 12, 2020

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

veryneatperson commented Apr 29, 2020

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

@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

@Peter-Hudson
Copy link

Slightly different take:

// different responses
function* responses() {
        yield { status: 401, response: { description: 'errored' } };
        yield { status: 200, response: { description: 'success' } };
      }

      const gen = responses();
     
      moxios.stubRequest('/route', gen.next().value);

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

6 participants