npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cypress-signalr-mock

v1.5.0

Published

An easy way to mock a SignalR server which can send and receive messages during Cypress tests.

Downloads

3,712

Readme

Cypress-SignalR-Mock

Easy way to publish messages from and to your SignalR hubs in Cypress E2E tests.

npm@latest npm semantic-release: angular

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!
  • Also works for Vitest unit testing!

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

// 01. Import the plugin to where your signalR connections are created.
import {useCypressSignalRMock} from 'cypress-signalr-mock';

// 02. Call "useCypressSignalRMock" to create a mock for the SignalR hub connection, make sure to give it an unique hub name. 
// It will return null when Cypress is not running, and thus create the real SignalR connection.
const hubConnection = useCypressSignalRMock('testHub') ??
    new HubConnectionBuilder().withUrl(`http://localhost:3000/testhub`).build();

// 03. Activate the plugin in your cypress/support/index.[js/ts] file.
import 'cypress-signalr-mock';

// 04. Use 'hubPublish()' in your E2E tests to publish messages as if it's the server.
cy.hubPublish(
    'testHub', // The name of the hub
    'hello-world', // The name of the message type
    {
        message: 'Hello World!', // The message payload
    },
);

// 05. The listener will receive the message as normal.
hubConnection.on('hello-world', (data) => {
    console.log(data); // { message: 'Hello World!' }
});

useCypressSignalRMock() options

  • debug - Enable debug logging. Default: false
useCypressSignalRMock("testHub", {
    debug: true,
});
  • enableForVitest - Enable the plugin to also work for vitest unit testing. Default: false
useCypressSignalRMock("testHub", {
    enableForVitest: true,
});

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

/**
 * Simulates a message sent from the Server => Client
 */
cy.hubPublish("hubName", "messageType", {
    value: "messagePayload"
});

// The listener will receive the message as normal.
progressHubConnection.on('hello-world', (data) => {
    console.log(data); // { value: "messagePayload" }
});

From Client to Server

/**
 * This command verifies that a specific messageType has been invoked (Client => Server):
 */
cy.hubVerifyInvokes(hubName, messageType, (invokes) => {
    // Do something
    expect(invokes.length).to.equal(5, `${action} not invoked`);
});

Limitations

  • Multiple listeners for the same hub name are not supported
// This will NOT work
const progressHubConnection1 = useCypressSignalRMock('progress') ??
    new HubConnectionBuilder().withUrl(`http://localhost:3000/progress1`).build();

const progressHubConnection2 = useCypressSignalRMock('progress') ??
    new HubConnectionBuilder().withUrl(`http://localhost:3000/progress2`).build();
// This will work
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.