Closed karneaud closed 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:
until()
is a big hack to test it out. It'll wait, polling, until the called
array has some item OR the timeout is finished, whichever happens first.await time(timeToExit)
is again another hack, since client.disconnect()
does not happen synchronously. Might switch to another until()
, since at least that is more empirical.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.
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
});
@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
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
.
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)