Add sockets.js tests and minor fixes#50
Conversation
|
Nice work @vvmnnnkv! I noticed that the tests on TravisCI never actually finish. See the above screenshot. I believe this is from the socket never being cleaned up and thus, it continues to "ping". The screenshot doesn't show it, but the Travis test kept going for 40 minutes with that same message every 20 seconds until I shut it down. Can you clean that up and I'll take another look? |
# Conflicts: # src/sockets.js
Codecov Report
@@ Coverage Diff @@
## grid-syft #50 +/- ##
============================================
Coverage ? 30.76%
============================================
Files ? 2
Lines ? 143
Branches ? 13
============================================
Hits ? 44
Misses ? 89
Partials ? 10
Continue to review full report at Codecov.
|
src/sockets.js
Outdated
| return new Promise((resolve, reject) => { | ||
| data.instanceId = this.instanceId; | ||
| // shallow copy + instanceId | ||
| let _data = Object.assign( |
There was a problem hiding this comment.
Why do it like this @vvmnnnkv? What was wrong with the old way this was being assigned?
There was a problem hiding this comment.
It had side effect of adding instanceId property on the data object passed into .send().
Another way could be sending instanceId outside of data?
{type, instanceId, data}?
test/sockets.test.js
Outdated
| const onOpen = event => console.log('NEW OPEN', event); | ||
| const onClose = event => console.log('NEW CLOSE', event); | ||
| const onError = event => console.log('NEW ERROR', event); | ||
| /** |
There was a problem hiding this comment.
We're not using JSDoc, feel free to throw these comments out for now.
test/sockets.test.js
Outdated
| * @param event Event name | ||
| * @returns {Promise} | ||
| */ | ||
| function makeEventPromise(emitter, event) { |
There was a problem hiding this comment.
Always declare function in ES6 fashion:
const makeEventPromise = (emitter, event) { ... };
test/sockets.test.js
Outdated
| * @param event Event name | ||
| * @returns {Promise} | ||
| */ | ||
| function makeEventPromise(emitter, event) { |
There was a problem hiding this comment.
Another thing: what does this function actually do?
There was a problem hiding this comment.
This is for convenience. It wraps event callback in a promise. The promise is resolved when the event is triggered.
E.g. when we want to await for established connection on the mock server:
const socket = await makeEventPromise(mockServer, 'connection');
test/sockets.test.js
Outdated
| */ | ||
| function makeEventPromise(emitter, event) { | ||
| let resolver; | ||
| let promise = new Promise(resolve => resolver = resolve); |
There was a problem hiding this comment.
Always use const if you can. If you cannot, use let. In this case, you should prefer const.
test/sockets.test.js
Outdated
| } | ||
|
|
||
| describe('Sockets', () => { | ||
| // common test objects |
There was a problem hiding this comment.
Comments should always start capitalized. They should also be fairly descriptive. To me, I don't think you need any comments in this file at all. It's a test file - so it should be "strictly business". Go ahead and remove them unless they're absolutely critical to the understanding of something complex. :)
There was a problem hiding this comment.
OK, removed some comments
test/sockets.test.js
Outdated
|
|
||
| test('can communicate with a socket server', async () => { | ||
| const message = { message: 'This is a test' }; | ||
| test('keep-alive messages', async() => { |
There was a problem hiding this comment.
Test messages ('keep-alive messages') should read as a full sentence with the description. Our description (written above) is 'Sockets', so a full sentence should be 'Sockets can have a custom keep-alive timeout' with 'Sockets' being the description message (from the describe() function) and 'can have a custom keep-alive timeout' being the test message (from the test() function).
test/sockets.test.js
Outdated
| onError: event => onError(event) | ||
| keepAliveTimeout | ||
| }); | ||
| let serverSocket = await mockServer.connected; |
There was a problem hiding this comment.
Again... use const, everywhere. This will be the last comment I write about in my review, but please change all references that can be changed.
test/sockets.test.js
Outdated
| // one message is sent right after establishing the connection hence +1 | ||
| // (do we really need that?) | ||
| expect(messages).toHaveLength(expectedMessagesCount + 1); | ||
| let expectedTypes =[]; |
There was a problem hiding this comment.
This spacing is incorrect. Is prettier running for you? Normally Prettier will fix things like this.
There was a problem hiding this comment.
I thought prettier would run automatically :) Fixed
src/sockets.js
Outdated
| onOpen, | ||
| onClose, | ||
| onMessage, | ||
| keepAliveTimeout |
There was a problem hiding this comment.
Actually, it would be better if we did:
onClose,
onMessage,
keepAliveTimeout = 20000You can assign defaults to your arguments this way, rather than doing it below (on line 18, which you can now remove).
src/sockets.js
Outdated
|
|
||
| const keepAlive = () => { | ||
| const timeout = 20000; | ||
| const timeout = keepAliveTimeout || 20000; |
There was a problem hiding this comment.
You can remove this, see above comment.

Fixes #47