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.

Would love to have a workaround for this timeout

See original GitHub issue

In Angular testing for a service that implements $http, we’re using to doing this:

$httpBackend.flush();

What this does is it takes any outstanding requests and processes them so that you can then begin the assertion process. See its docs for more.

Anyway, I’m bringing this up as an example because it would be really nice to be able to have this for the axios mocker. Instead of this flush() method or something similar, we’re forced to create a timeout with a zero millisecond duration and then nest all of our expectations within that timeout. For an example of this, see here:

import { expect } from 'chai';
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import { fetchPost } from './lib'

describe('Lib', () => {
  it('Should return data from response', (done) => {
    let mockAdapter = new MockAdapter(axios);

     mockAdapter.onGet('http://awesome.com/posts').reply(200, {
       data: {
         posts: ['Intro to git']
       }
     });

     let response = fetchPost();

     setTimeout(() => {
        expect(response.posts[0]).to.be.equal('Intro to git');
        done();
     }, 0)
  });
});

I’ve confirmed in my own local code that without the timeout, you indeed cannot expect response.posts[0] to be resolved, but you can inside the timeout. I don’t like this syntax though because it forces all of the code to be nested another level, it’s not as linear, and it assumes some things about how your axios promiser is implemented (e.g. it breaks on an otherwise perfectly functioning custom wrapper that I wrote for fun).

Would it be possible to have some sort of $httpBackend.flush(); equivalent for this axios mocker?

Thanks!

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:4
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
martynchamberlincommented, Feb 6, 2017

If I understand correctly, you’re saying that the code above should be reformatted to the following:

describe('Lib', () => {
  it('Should return data from response', (done) => {
    let mockAdapter = new MockAdapter(axios);

     mockAdapter.onGet('http://awesome.com/posts').reply(200, {
       data: {
         posts: ['Intro to git']
       }
     });

     return fetchPost().then((response) => { 
        expect(response.posts[0]).to.be.equal('Intro to git');
        done();
     });

  });
});

Correct?

Assuming this is the case, that would work in situations where the function that generates the HTTP request returns a promise, but the function might not do that. Imagine if this were how fetchPost were defined:

import http from 'axios';
import eventHub from './eventhub.js';

export function fetchPost() {
 http
    .get('http://awesome.com/posts')
    .then((response) => {
        eventHub.$emit('posts', response.data);
    });
}

In other words, instead of using promises to pass the data around, we’re using a simple pub/sub model. In this situation we’re required to use a timeout. Would love for that to not have to be the case.

0reactions
danielcardoso5commented, Nov 6, 2018

The best way I found to modify the async action creator to return the http.get promise and then make the test an async function and set the action dispatcher with an await call. You can then make assertions after the action dispatch.

//async action creator using redux-thunk
updateContacts: () => {
      return http.get("/api/chat").then((response) => {
        dispatch(chatActions.getContacts());
      }).catch(reason => {
      });
    };
  }

//action test
it("should update the user contacts", async () => {
    chatActions.getContacts = jest.fn();

    mock
      .onGet("/api/chat").replyOnce(200);

    await ChatStore.dispatch(actions.updateContacts());

    expect(actions.getContacts).toHaveBeenCalled();
  });
Read more comments on GitHub >

github_iconTop Results From Across the Web

HACK to FIX "Device Timeout -18" Error on the Cricut - YouTube
Follow these simple and easy steps to get your Circut to connect to your computer so you can cut your projects - this...
Read more >
Please fix Bitbucket timeout - Atlassian Community
This means that you'll be logged out of Bitbucket Cloud when you have no activity on your browser within 24 hours.
Read more >
Timeout for SCORM package - Moodle.org
I'd love to find a way to simply shut down the SCORM package at a timeout value, and then they can reenter and...
Read more >
Fix: Timeout Errors in WordPress - Meow Apps
That's what you should absolutely do. Make sure you use the latest version of PHP (or at least PHP 7.3+). Then, check the...
Read more >
The Easy Guide To Fix 504 Gateway Timeout Error In ... - Nestify
Errors are as undesirable as a speeding ticket. Both can get paid for, but you will regret what they cost you. When it...
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