All Projects → lensesio → cypress-websocket-testing

lensesio / cypress-websocket-testing

Licence: Apache-2.0 License
Test WebSocket connections with Cypress

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to cypress-websocket-testing

nextjs-starter-kit
NextJS Starter Kit with Testing Frameworks and CI/CD
Stars: ✭ 30 (-53.85%)
Mutual labels:  cypress
eslint-config-adjunct
A reasonable collection of plugins to use alongside your main esLint configuration
Stars: ✭ 39 (-40%)
Mutual labels:  cypress
cypress-example-docker-compose
Example running Cypress tests against Apache server via docker-compose
Stars: ✭ 106 (+63.08%)
Mutual labels:  cypress
cypress-dotenv
Cypress plugin that enables compatability with dotenv
Stars: ✭ 47 (-27.69%)
Mutual labels:  cypress
glific-frontend
Frontend for the Glific platform
Stars: ✭ 18 (-72.31%)
Mutual labels:  cypress
cypress-plugin-stripe-elements
A small Cypress plugin that assists you in filling in Stripe Elements inputs
Stars: ✭ 22 (-66.15%)
Mutual labels:  cypress
cypress-circleci-reporter
Cypress test reporter for CircleCI
Stars: ✭ 37 (-43.08%)
Mutual labels:  cypress
daruma-backend
🎎 Shared Expense Manager (Backend) - NestJS+DDD+CQRS+Event Sourcing 🎎
Stars: ✭ 70 (+7.69%)
Mutual labels:  cypress
example-percy-cypress
Example app demonstrating Percy's Cypress integration.
Stars: ✭ 48 (-26.15%)
Mutual labels:  cypress
metasfresh-webui-frontend-legacy
metasfresh Webui Frontend
Stars: ✭ 57 (-12.31%)
Mutual labels:  cypress
todo-graphql-example
Example Todo app on top of json-graphql-server
Stars: ✭ 20 (-69.23%)
Mutual labels:  cypress
cypress-faq
As perguntas mais frequentes sobre Cypress respondidas em PT-BR
Stars: ✭ 101 (+55.38%)
Mutual labels:  cypress
next-ts-graphql-apollo-starter
An opiniated Next powered starter which include support for Apollo with GraphQL SSR support, codegen, styled component / system, framer motion and Cypress
Stars: ✭ 18 (-72.31%)
Mutual labels:  cypress
Ocelot-Social
Free and open-source social network for active citizenship.
Stars: ✭ 49 (-24.62%)
Mutual labels:  cypress
babel-plugin-remove-test-ids
🐠 Babel plugin to strip `data-test-id` HTML attributes
Stars: ✭ 40 (-38.46%)
Mutual labels:  cypress
silverback
DEPRECATED. Use https://packagist.org/packages/amazeelabs/silverback-cli instead.
Stars: ✭ 13 (-80%)
Mutual labels:  cypress
cypress-xhr-responses-recording
No description or website provided.
Stars: ✭ 19 (-70.77%)
Mutual labels:  cypress
cypress-mochawesome-reporter
Zero config Mochawesome reporter for Cypress with screenshots
Stars: ✭ 48 (-26.15%)
Mutual labels:  cypress
awesome-address-book
This project shows a basic address book built with ReactJS, Redux Toolkit and Typescript 📖
Stars: ✭ 20 (-69.23%)
Mutual labels:  cypress
vue3-realworld-example-app
Explore the charm of Vue composition API! Vite?
Stars: ✭ 364 (+460%)
Mutual labels:  cypress

cypress-websocket-testing

Test your WebSocket endpoints using Cypress.

Build Status
Commitizen friendly semantic-release License: Apache 2 TypeScript

Table of Contents

Background

Cypress comes out of the box with a great set of tools that allow both UI and API integration tests to be written. Unfortunately the cy.request() command is limited to REST endpoints only, so this library is here to help with those cases when WebSockets need to be called/tested as part of more complex integration/E2E tests.

Installation

npm i -D @lensesio/cypress-websocket-testing
# or
yarn add -D @lensesio/cypress-websocket-testing

You also need to add rxjs to the project.

npm i -D rxjs

Usage

JavaScript

@lensesio/cypress-websocket-testing extends Cypress' cy command.

Add this line to your project's cypress/support/commands.js:

import { addStreamCommands } from '@lensesio/cypress-websocket-testing';
addStreamCommands();

Then, in your test, you can use both commands that come with this lib. cy.stream and cy.streamRequest.

// For common cases:
cy.streamRequest(config, options).then(results => {
        expect(results).to.not.be.undefined;
})
// When in need of a bit more flexibility
cy.stream(config).then(subject => {
      subject
        .pipe(
          takeUntil(timer(1000)),
          reduce((acc , val) => acc.concat([val]), [])
        )
        .subscribe({
          next: (results) => {
            expect(results).to.not.be.undefined;
          },
          error: (err) => {},
          complete: done
        });
    });

TypeScript

As the library is written in Typescript, you can pass the type of the message to the command and to the config/options object. ( make sure that you already configured your Cypress tests to work with TS )

First, add @lensesio/cypress-websocket-testing to the cypress/tsconfig.json file

{
  "compilerOptions": {
    "types": [
        "cypress",
        "@lensesio/cypress-websocket-testing"
    ]
  }
}

Then to use in TypeScript tests:

// For full set of config values, check rxjs documentation
const config: WebSocketSubjectConfig<IMessage> = {
  url: "ws://localhost:8080/"
};

let options: Partial<StreamRequestOptions<IMessage>>;

cy.streamRequest<IMessage>(config, options).then((results?: IMessage[]) => {
        expect(results).to.not.be.undefined;
})
cy.stream<IMessage>(config).then(subject => {
      subject
        .pipe(
          takeUntil(timer(1000)),
          reduce((acc: IMessage[], val: IMessage) => acc.concat([val]), [])
        )
        .subscribe({
          next: (results?: IMessage[]) => {
            expect(results).to.not.be.undefined;
          },
          error: (err: any) => {},
          complete: done
        });
    });

Note: There are some type conflicts when extending/adding operators to cy.stream() in tests directly. (due to issues with Cypress including an old rxjs version as a dependency). Best way is to extend cy.stream() by building a custom command with it and use that instead.

Arguments

  • config

A WebSocketSubjectConfig object. See official docs for more information. Is passed as is to the underlying webSocket subject.

  • options:StreamRequestOptions (only for streamRequest command. Although optional, at least the takeWhileFn should be set)

Usage cy.streamRequest(config, options).

Option Default Description
streamTimeout defaultCommandTimeout Time to wait for the stream to complete. (if is greater than Cypress defaultCommandTimeout, you need to use the cy.wrap as a workaround. Investigating alternative ways)
retryDelay 4000 How long to way until a new connection attempt is made.
retryAttempts 5 How many times to retry the connection before completing.
startUpMessage any A message to be sent on connection open.
takeWhileFn ()=>false Function that will tell the stream when to close. If not set, it will close on the first message received in order to avoid having an open connection.
retryUntilFn ()=>true Function that will tell the stream how to check the results, and retry if the result is false.


Running the examples

In order to try this project locally, you need to npm install in both the root and the examples/ folder. After, build the library using npm run build in the root folder, then go to examples/ , start the websocket server npm start and cypress using npm run test:local.

PRs

PRs are welcome. Be sure to add

  • Tests
  • Reason for the PR

When committing, remember to use `npm run commit`, in order to start commitizen.

TODO

  • [] Find a fix for the cy.wrap workaround.
  • [] Improve error handling.
  • [] Add more examples for cy.stream command.

LICENSE

Apache 2.0

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].