# Clone examplegitclonehttps://github.com/assertedio/graphql-uptime# Enter directory and installcdnode-uptime/npminstall# Run asserted testsnpmruntest:asrtd
const { expect } =require('chai');constio=require('socket.io-client');constutil=require('util');constsinon=require('sinon');constsleep=util.promisify(setTimeout);constmonitoringClient=io('http://localhost:3000', { forceNew:true });describe('socketio api tests', () => {let client;before((done) => {monitoringClient.emit('add user','monitoring');monitoringClient.once('connect', () =>done()); });beforeEach((done) => { client =io('http://localhost:3000', { forceNew:true });client.once('connect', () =>done()); });afterEach(() => {client.disconnect(); });after(() => {monitoringClient.disconnect(); });it('user joined and login',async () => {constlogin=sinon.stub();constjoined=sinon.stub();monitoringClient.once('user joined', joined);client.once('login', login);client.emit('add user','new-user');// Admittedly a bit gross to use sleep here, but just wanted something simple.// The less brittle approach would be to block on a promise until the stubs are called.awaitsleep(100);expect(login.args).to.eql([[{ numUsers:2 }]]);expect(joined.args).to.eql([[{ numUsers:2, username:'new-user' }]]); });it('user sent message',async () => {constmonitoringMessage=sinon.stub();constclientMessage=sinon.stub();monitoringClient.once('new message', monitoringMessage);client.once('new message', clientMessage);client.emit('add user','new-user');awaitsleep(100);client.emit('new message','some-message');awaitsleep(100);expect(monitoringMessage.args).to.eql([[{ message:'some-message', username:'new-user' }]]);expect(clientMessage.args).to.eql([]); });});