1.0.4 • Published 3 years ago

@jmredfern/websocket-with-promises v1.0.4

Weekly downloads
-
License
-
Repository
-
Last release
3 years ago

websocket-with-promises

This is a utility that wraps the ws WebSocket library with promises. This is useful for creating component tests for applications that use WebSockets.


Example Usage

import { getWebSocket, WebSocket } from 'websocket-with-promises';

describe('component test', () => {
	const websocket = getWebSocket({ URL: 'ws://localhost:8000' });

	beforeAll(async () => {
		await websocket.connect();
	});

	afterAll(async () => {
		await websocket.disconnect();
	});

	it('should create a game', async () => {
		await websocket.sendMessageAsJSON({ type: 'CREATE_GAME' });
		const serverMessage = await websocket.getMessageAsObject();
		expect(serverMessage).toEqual({ type: 'GAME_CREATED' });
	});
});

To run the test suite:

npm run test


License

MIT © Jim Redfern.