Coder Social home page Coder Social logo

cypress-socketio-chat's Introduction

cypress-socketio-chat

ci status badges status renovate-app badge cypress version

Example testing real-time Socket.io chat using Cypress.io

The original chat program copied from dkhd/node-group-chat as described in Build A Group-Chat App in 30 Lines Using Node.js blog post.

Chat test

Blog posts

Testing a Socket.io chat application can be done in several ways. Read the following blog posts to see the alternatives.

Title Description
Test a Socket.io Chat App using Cypress Simulates the second user by connecting to the chat server from the plugins file
Run Two Cypress Test Runners At The Same Time Launches two test runners, giving them separate specs to run
Sync Two Cypress Runners via Checkpoints Launches two test runners, which stay in sync by communicating via their own Socket.io server
Code Coverage For Chat App Instruments and measures the fullstack code coverage

Flip through the presentation slides E2E Testing For A Real-Time Chat Web Application

Specs

Name Description
first-spec Tests that the user can post a message and see it
random-name-spec.js Creates a random user name for the test
client-api-spec.js Invokes events as a 2nd user using app actions
socket-spec.js Mimics the 2nd user by connecting to the Socket.io server from the plugin file
socket-from-browser-spec.js Mimics the 2nd user by connecting to the Socket.io server from the spec file
mock-socket-spec.js The test forces the app to use a mock socket object instead of the real connection, see video
disconnect-spec.js Checks if the user disconnects correctly

Running 2 Cypress instances

This repo also shows how to run 2 Cypress instances at the same time to "really" chat with each other.

  • start the server with npm start
  • execute npm run chat:run which starts the two Cypress processes

Look at the package.json file to see the commands we use to run the first and second user specs - they are listed in cy-first-user.json and cy-second-user.json config files.

The test runners wait for each other using a common Socket.io server created in the chat.js script. This is a separate Socket.io server from the application.

Test communication

The first server logs in and reports that it is ready for the second test to start

// cypress/pair/first-user.js
/// <reference types="cypress" />

// this test behaves as the first user to join the chat
it('chats with the second user', () => {
  const name = 'First'
  const secondName = 'Second'

  cy.visit('/', {
    onBeforeLoad(win) {
      cy.stub(win, 'prompt').returns(name)
    },
  })

  // make sure the greeting message is shown
  cy.contains('#messages li i', `${name} join the chat..`).should('be.visible')
  cy.task('checkpoint', 'first user has joined')
})

The second test runner executes its own test. It first waits for the checkpoint before visiting the page

// cypress/pair/second-user.js
/// <reference types="cypress" />

// this test behaves as the second user to join the chat
it('chats with the first user', () => {
  cy.task('waitForCheckpoint', 'first user has joined')

  const name = 'Second'
  // we are chatting with the first user
  const firstName = 'First'
  cy.visit('/', {
    onBeforeLoad(win) {
      cy.stub(win, 'prompt').returns(name)
    },
  })
})

Code coverage

Added instrumenting front-end code using istanbul-lib-instrument module, see index.js file. Read the blog post Code Coverage For Chat App.

Fullstack code coverage

Note: I did not use Istanbul middleware because it seemed to not support ES6 syntax (the middleware module had no releases for a long time).

cypress-socketio-chat's People

Contributors

bahmutov avatar renovate-bot avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

cypress-socketio-chat's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Edited/Blocked

These updates have been manually edited so Renovate will no longer make changes. To discard all commits and start over, click on a checkbox.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/badges.yml
.github/workflows/ci.yml
  • cypress-io/github-action v2
  • cypress-io/github-action v2
npm
package.json
  • ejs 3.1.10
  • socket.io 4.7.5
  • @cypress/code-coverage 3.9.12
  • cypress 9.6.1
  • cypress-recurse 1.35.3
  • socket.io-client 4.7.5
  • socket.io-mock 1.3.2

  • Check this box to trigger a request for Renovate to run again on this repository

Can't reach websocket messages

Hi there!

This is more a question than an issue. I am trying to use your socket solution into my project. My goal is to fetch websocket messages, so I can verify them.

As a PoC, I have used https://websocketstest.com/ site, in order to try to check ws messages. However, I have no luck so far yet.

it.only('test 5', function () {
  cy.visit('https://websocketstest.com/')

  const socket = io.connect('https://websocketstest.com/')
  
  let lastMessage
  socket.on('time', (msg) => (lastMessage = msg))
  // socket.onAny((msg) => (lastMessage = msg));
  
  cy.reload()
  
  cy.wait(4000)
  cy.log(lastMessage)
})

Am I missing something?

Thanks in advance

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.