franciscop / server

:desktop_computer: Simple and powerful server for Node.js
https://serverjs.io/
MIT License
3.56k stars 171 forks source link

Testing socket with run.js #94

Closed karneaud closed 6 years ago

karneaud commented 6 years ago

What are you trying to do? I'm trying to run tests with socket() and run.js

Ask your question Most of your test involve using standard http routes I'm trying to figure out how to use it with run. Do I use socket.io to emit or does run have utilities for it?

Environment (optional):

Additional context (optional)

var routes = [], spy, io
    it('is it installed?', ()=> {
      expect(ctlr).to.not.be.null
    })

    before(() => {
      spy = sinon.spy(ctlr, 'console')
      routes.push(socket('console', ctlr.console ))
      run({port: 5058 }, routes)
      io = client('http://localhost:5058');
    })

    it('should create game ', () => {
      io.emit('console', { state: 'create' })
    })
franciscop commented 6 years ago

It is not so easy; for the http methods, the workflow is request => response. However for websockets it's bidirectional communication, so any side could start it.

Have a look at socket.io tests for the alpha version: https://github.com/franciscop/server/blob/plugins/plugins/socket/integration.test.js:

// Example of socket.io test:
it('can listen to a simple call', async () => {
  // Store here all the calls received from the middleware socket()
  let called = [];
  const middle = socket('message', () => {
    called.push('message');
  });
  await test(middle).run(async api => {
    const client = io(`http://localhost:${api.ctx.options.port}/`);
    client.emit('message', { hello: 'world' });
    await until(10000, () => called.length);
    client.disconnect();
    await time(timeToExit);
  });
  expect(called).toEqual(['message']);
});

Things to note:

If I was to write many more tests (or if you are), I'd try to improve these testing bits since they are quite hacky or at least make a wrapper to write tests more easily. Good news, they are cautiously hacky, so if they pass the test is actually correct, but the tests will fail randomly so I have to re-run them.

Let me know if you have any other question.

PS, you said 1.1.0. I answered this assuming you mean 1.1.0-alpha.X and not 1.0.x, which uses different testing methods.

franciscop commented 6 years ago

If you are using the alphas, please make sure to update; there hasn't been any run.js for a while, now it's this workflow:

test(/* server argumets to test */).run(async api => {
  // Testing websockets
});
karneaud commented 6 years ago

@franciscop I tried updating. Now realise there is no run.js. Is it easier with the new versions? Kinda want to stick to these earlier version until you are more stable

franciscop commented 6 years ago

Sorry for not answering before; both run.js and test.js are not part of the stable releases. Only the documented code should be considered stable. I am trying to get it somewhere stable where you can test it, but so far there is no official testing suite. You can always run the server and launch requests to it, or test your individual handlers, but there is no official solution for the exact case of run.js.