Skip to content

Socket.IO Mocks for the Client and Server. Typescript support, Vite, & Vitest.

License

Notifications You must be signed in to change notification settings

opp100/socket.io-mock-ts

 
 

Repository files navigation

Socket.IO-Mock-TS (TypeScript)

npm version

A mock to test the Socket.IO library implementation.

  • Written with Typescript.
  • Bundled with vite.
  • Tested with vitest.

Installation

npm install @opp100/socket.io-mock-ts -D

Usage

Create a new socket mock with:

import { SocketServerMock } from '@opp100/socket.io-mock-ts';

const socket = new SocketServerMock();

const client = socket.clientMock;

And use the socket as if it was a normal Socket.IO socket.

For example:

import { SocketServerMock } from '@opp100/socket.io-mock-ts';
import { expect, test } from 'vitest';

test('Sockets should be able to talk to each other without a server', () => {
  const socket = new SocketServerMock();

  socket.on('message', (message: string) => {
    expect(message).toBe('Hello World!');
  });

  socket.clientMock.emit('message', 'Hello World!');
});

Or with using promises in unit tests, for example:

import { SocketServerMock } from '@opp100/socket.io-mock-ts';
import { expect, test } from 'vitest';

test('Sockets should be able to talk to each other without a server', () => {
  const socket = new SocketServerMock();

  const data = await new Promise((resolve) => {
    socket.on('message', (message: string) => {
      resolve(message);
    });

    socket.clientMock.emit('message', 'Hello World!');
  });

  expect(data).toBe('Hello World!');
});

About

Socket.IO Mocks for the Client and Server. Typescript support, Vite, & Vitest.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 91.8%
  • JavaScript 8.2%