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

fakeServer requests count always is null #1448

Closed
optimatex opened this issue Jun 6, 2017 · 4 comments
Closed

fakeServer requests count always is null #1448

optimatex opened this issue Jun 6, 2017 · 4 comments

Comments

@optimatex
Copy link

  • 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
@fearphage
Copy link
Member

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

@optimatex
Copy link
Author

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 ?

@fearphage
Copy link
Member

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();
  });
});

@optimatex
Copy link
Author

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

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

2 participants