question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Possible to respond differently based on order of calls?

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5

github_iconTop GitHub Comments

6reactions
omrilotancommented, Apr 30, 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);
2reactions
mrchiefcommented, 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)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Possible to stub method twice within a single test to return ...
"test should stub method differently on consecutive calls": function () { var callback = sinon.stub(); callback.onCall(0).returns(1); callback.
Read more >
How to Answer Customer Service Calls: The Dos and Don'ts
How NOT to answer calls in a call center · Don't Interrupt your customer · Don't belittle the customer's issues · Don't give...
Read more >
How To Answer the Phone Professionally (With Examples)
Try switching out phrases like "I don't know" for proactive alternatives like "Let me see if I can figure that out for you."...
Read more >
Telephone Etiquette - LTS Knowledge Base
Listen actively and listen to others without interrupting. Don't make people dread having to answer their phone or call your department.
Read more >
Stop Trying to Delight Your Customers
Twenty-four percent of the repeat calls in our study stemmed from emotional disconnects between customers and reps—situations in which, for instance, the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found