Last active
March 13, 2020 08:28
-
-
Save gtindo/9ae9a3a7d936a4650b55a71ca4ed33c8 to your computer and use it in GitHub Desktop.
Integration test with socket.io
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const assert = require('chai').assert; | |
const socket = require('socket.io-client')("http://localhost:3000"); | |
/** | |
* function used to wait response on a channel | |
* | |
* @param {String} channel | |
* @param {Function} callback | |
*/ | |
const waitMessage = async (channel, socket, callback) => { | |
return new Promise((resolve, reject) => { | |
try { | |
socket.on(channel, async (data) => { | |
await callback(data); | |
resolve(data); | |
}); | |
} catch (err) { | |
reject(err) | |
} | |
}); | |
} | |
describe('Test Connection', () => { | |
it("Should connect, send ping and receive pong", () => { | |
return new Promise((resolve, reject) => { | |
socket.emit("pingMsg", "ping"); | |
// wait for response | |
await waitMessage("pongMsg", socket, (data) => { | |
try { | |
assert.equal(data, "pong"); | |
socket.removeAllListeners(["pongMsg"]); // remove socket listener | |
resolve("done"); | |
} catch (e) { | |
reject(e); | |
} | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment