# cypress-websocket-plugin

> Cypress plugin for dealing with single page apps using websockets

Latest version **0.1.2** (published 2022-11-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install cypress-websocket-plugin
pnpm add cypress-websocket-plugin
yarn add cypress-websocket-plugin
bun add cypress-websocket-plugin
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.2 |
| Published | 2022-11-02 |
| First published | 2022-10-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 8.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Sławomir Zborowski |
| Maintainers | zborro |
| Keywords | cypress, websocket, socket.io |

## Links

- npm: https://www.npmjs.com/package/cypress-websocket-plugin
- Repository: https://github.com/zborro/cypress-websocket-plugin
- Homepage: https://github.com/zborro/cypress-websocket-plugin#readme
- Issues: https://github.com/zborro/cypress-websocket-plugin/issues
- npm.io page: https://npm.io/package/cypress-websocket-plugin

## Dependencies (3)

- [cors](https://npm.io/package/cors.md) ^2.8.5
- [express](https://npm.io/package/express.md) ^4.18.1
- [socket.io](https://npm.io/package/socket.io.md) ^2.2.0

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 0.1.2 (latest) — 2022-11-02
- 0.1.1 — 2022-10-01
- 0.1.0 — 2022-10-01

## README

# cypress-websocket-plugin

> Cypress plugin for mocking websockets (socket.io version 2)

## Requirements

* `Cypress 10.x`

## Installation

```shell
npm i -D cypress-websocket-plugin
```

## Usage

First, integrate plugin with your Cypress setup:

1. Edit file `cypress.config.ts`. Following example will let the websocket server respond
   to `hello` by echoing it back with an argument. It will also send `ping` message every
   5 seconds with value coming from dynamic variable (initially set to 42). Code in the
   test can modify this variable to change behavior in run-time.
```js
import { startFakeWebsocketServer } from "cypress-websocket-plugin";

export default defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      startFakeWebsocketServer({
        options: {
            responses: {
                'hello': ['hello', 'dear'],
            },
            periodicMessages: [
                {"name": "ping", "args": ["$dynamicVariable"], "interval": 5000},
            ]
        },
        variables: {
            "dynamicVariable": 42,
        },
      });
    },
  },
});
```
2. Edit file `cypress/support/commands.ts`. Add:
```js
import { addCommands } from "cypress-websocket-plugin/commands.js";

addCommands();
```

### Caveats

* port is not configurable, it's fixed as `3000`. This is on my list.
* this plugin was tested and works with Socket.IO version 2 only.

### Example usage

Imagine you have application, that after clicking on some button will issue two
messages over WebSocket:

* "hey, I just met you", args: "and this is craAAzy"
* "but here's my number", args: "so call me maybe"

This is how it would be tested with the plugin:

```js
beforeEach(() => {
  // we don't want any messages from previous test runs
  cy.resetWsMessages();
});

describe('empty spec', () => {
  it('passes', () => {
    // cy.visit(someUrl);
    // cy.get("#btn").click();
    // ...

    // actual check  <-------
    cy.expectWsMessages(
        [
            ["hey, I just met you", "and this is craAAzy"],
            ["but here's my number", "so call me maybe"],
        ]
    );
```

### Modyfying dynamic variables - example

TODO (you can control values returned by the server from the test easily)

### Changing behavior of the websocket server / overriding options - example

In the test following code can be used to modify options passed in Cypress configuration file:

```js
  cy.overrideWsOptions({
    responses: {
        'on-msg-1': ['msg-1-reply', 'arg'],
        'on-msg-2': ['msg-2-reply', 'arg'],
        // ...
    },
    periodicMessages: [
        {"name": "periodic", "args": ["heartbeat", 1], "interval": 2500},
        {"name": "periodic", "args": ["heartbeat", 2], "interval": 5000},
        // ...
    ]
  });
```

---
_Source: https://npm.io/package/cypress-websocket-plugin · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
