Cypress-SignalR-Mock
Easy way to publish messages from and to your SignalR hubs in Cypress E2E tests.
Table of Contents
Features
- Provides Cypress commands to publish and verify messages from and to the SignalR hub!
- Full TypeScript support!
- Multiple hubs can be mocked at the same time!
- Provides mock support for all public HubConnection methods!
- Small footprint, easy install and very human!
Compatibility
The below are not hard requirements, but the plugin is tested against these versions. It will most likely work with
older versions.
- Cypress 10.0.0 or higher
- SignalR 6.0.0 or higher
- TypeScript 4.0.0 or higher
- RxJS 6.6.0 or higher
- Node 12.0.0 or higher
Install
Install with npm:
npm install --save-dev cypress-signalr-mock
Install with yarn:
yarn add cypress-signalr-mock --dev
Install with pnpm:
pnpm add -D cypress-signalr-mock
Usage
import {useCypressSignalRMock} from 'cypress-signalr-mock';
const progressHubConnection = useCypressSignalRMock('progress') ??
new HubConnectionBuilder().withUrl(`http://localhost:3000/progress`).build();
import 'cypress-signalr-mock';
cy.hubPublish(
'progress',
'hello-world',
{
message: 'Hello World!',
},
);
progressHubConnection.on('hello-world', (data) => {
console.log(data);
});
How does it work
Normally, a plugin like this would replace the window.WebSocket
object with a mock object.
This is not ideal, as that same window.WebSocket
object is also used by Cypress itself during runtime.
Instead, the HubConnection
class from
the @microsoft/signalr package is mocked with the same signatures,
while replacing the functionality with Subjects
from Rx.js. This mimics the SignalR functionality of sending and receiving messages
from the server, while not interfering in any way with Cypress itself.
Cypress Commands
From Server to Client
cy.hubPublish("hubName", "messageType", {
value: "messagePayload"
});
progressHubConnection.on('hello-world', (data) => {
console.log(data);
});
From Client to Server
cy.hubVerifyInvokes(hubName, messageType, (invokes) => {
expect(invokes.length).to.equal(5, `${action} not invoked`);
});
Limitations
- Multiple listeners for the same hub name are not supported
const progressHubConnection1 = useCypressSignalRMock('progress') ??
new HubConnectionBuilder().withUrl(`http://localhost:3000/progress1`).build();
const progressHubConnection2 = useCypressSignalRMock('progress') ??
new HubConnectionBuilder().withUrl(`http://localhost:3000/progress2`).build();
const cypressMock = useCypressSignalRMock('progress');
const progressHubConnection1 = cypressMock ??
new HubConnectionBuilder().withUrl(`http://localhost:3000/progress1`).build();
const progressHubConnection2 = cypressMock ??
new HubConnectionBuilder().withUrl(`http://localhost:3000/progress2`).build();
- The
hubConnection.on
and hubConnection.stream
cannot have the same message type name.
Contributing
Any contributions, ideas and feedback are very welcome!
Credits
Shout-out to @BassSlagter for his great work on
the cypress-plugin-signalr plugin. His work and method was the
major inspiration for Cypress-SignalR-Mock. His plugin is unfortunately not maintained anymore, so I decided to make a
spiritual successor to it.