thoov / mock-socket

Javascript mocking library for WebSockets and Socket.IO
MIT License
797 stars 118 forks source link

Mock-socket in typescript is causing error #179

Open Umayalmuthupalaniappan opened 6 years ago

Umayalmuthupalaniappan commented 6 years ago

Hi ,

I am using the mock-socket library to test the socket communication in my typescript app. I use karma for testing. I have the following test case where I mock the socket server

import { SocketIO, Server } from 'mock-socket'; it('should get message', done => { let mockServer = new Server('http://localhost:8000'); mockServer.on('connection', socket => { socket.emit('message', true) }) (window as any).io = SocketIO; expect(myService.getMessage()).toEqual(true); done(); })

And the service looks like this import * as ioclient from 'socket.io-client';

constructor() { this.Socket = ioclient.connect('http://localhost:8000'); this.portSocket.on('message', (msg: any) => { console.log('Message :', msg); this.message= msg; }); } getMessage(){ return this.message; }

When I run the test case through karma I am getting an error stating that mockServer.on(...)is not a function. I have added the mock-socket files in karma.conf.js.Looking for help in solving this

Thanks in advance

DemianTinkiel commented 5 years ago

I had the same problem, suppressing the TS error with // @ts-ignore above the .on part allows it to run (and works fine) e.g

mockDM.on('connection', socket => {
      // @ts-ignore
      socket.on('message', request => {

Obviously this is a workaround and not a solution. Perhaps the types incorrectly defined?