Sinon: fakeServer requests count always is null

Created on 6 Jun 2017  ·  4Comments  ·  Source: sinonjs/sinon

  • Sinon version : 2.3.2
  • Environment : karma-test-runner
  • Example URL :
  • Other libraries you are using: mocha

What did you expect to happen? I expect that sinon.fakeServer.requests count was more that 1

What actually happens sinon.fakeServer.requests always is null

How to reproduce

describe('--Pictures Selector--', () => {
   let server, store, wrap;
   beforeEach(() => {
      server = sinon.fakeServer.create();
      server.respondWith("GET", /\/api\/users/, [
         200, {"Content-Type":"application/json"}, JSON.stringify(mock_pictures_list_store)
      ]);
      store = new Pictures_List_Store([]);
      wrap = mount(
         <Selectable_Picture_List store={store} />
      );
      axios.get('http://localhost:9157/api/users/5/images/30/0');
      console.log("%c server.requests", "color: #C78B41", server.requests);
   });
   afterEach(() => {
      server.restore();
   });

   it('render drop zone', () => {
      server.respond();
      console.log("%c server.requests", "color: #C78B41", server.requests);
      expect(wrap.find('Dropzone')).to.have.length(1);
   });
});


I call axios.get requests directly in test code and also it is called in componentWillMount method of a react.js component

Most helpful comment

Is there any way to launch HTML testing with fakeServer ?

Yes, there is.

The solution to seeing the requests in your beforeEach above is two-fold:

  1. pass a callback to the beforeEach function
  2. wait a moment to run the console.log

The changes would look like:

beforeEach(done => {
  // ...
  axios.get(...);
  setTimeout(() => {
    console.log("%c server.requests", "color: #C78B41", server.requests);
    done();
  });
});

All 4 comments

axios.get() is asynchronous. This means that when your console.log line runs, the xmlhttprequest has not been created yet.

Thanks for the answer. But in the case when I use server.autoRespond = true; or server.respondImmediately = true; or server.respond(); server.requests is still empty. Is it right that those things don't work?
Is there any way to launch HTML testing with fakeServer ?

Is there any way to launch HTML testing with fakeServer ?

Yes, there is.

The solution to seeing the requests in your beforeEach above is two-fold:

  1. pass a callback to the beforeEach function
  2. wait a moment to run the console.log

The changes would look like:

beforeEach(done => {
  // ...
  axios.get(...);
  setTimeout(() => {
    console.log("%c server.requests", "color: #C78B41", server.requests);
    done();
  });
});

Wow. That's works! Thanks. Finally, I got the way to build required tests.

Was this page helpful?
0 / 5 - 0 ratings