{"id":15552689,"url":"https://github.com/bahmutov/cypress-socketio-chat","last_synced_at":"2025-09-28T23:31:24.880Z","repository":{"id":37649002,"uuid":"375349592","full_name":"bahmutov/cypress-socketio-chat","owner":"bahmutov","description":"Example testing real-time Socket.io chat using Cypress.io","archived":false,"fork":false,"pushed_at":"2024-12-18T05:39:51.000Z","size":999,"stargazers_count":9,"open_issues_count":6,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-12-27T20:08:19.865Z","etag":null,"topics":["cypress-example"],"latest_commit_sha":null,"homepage":"https://slides.com/bahmutov/e2e-for-chat","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bahmutov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-06-09T12:36:41.000Z","updated_at":"2024-10-26T02:04:24.000Z","dependencies_parsed_at":"2024-01-17T07:43:45.513Z","dependency_job_id":"d76c4af3-95aa-4d6e-9499-303e545164a4","html_url":"https://github.com/bahmutov/cypress-socketio-chat","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Fcypress-socketio-chat","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Fcypress-socketio-chat/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Fcypress-socketio-chat/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Fcypress-socketio-chat/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bahmutov","download_url":"https://codeload.github.com/bahmutov/cypress-socketio-chat/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234570370,"owners_count":18854156,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cypress-example"],"created_at":"2024-10-02T14:21:42.135Z","updated_at":"2025-09-28T23:31:24.366Z","avatar_url":"https://github.com/bahmutov.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# cypress-socketio-chat\n[![ci status][ci image]][ci url] [![badges status][badges image]][badges url] [![renovate-app badge][renovate-badge]][renovate-app] ![cypress version](https://img.shields.io/badge/cypress-9.6.1-brightgreen)\n\u003e Example testing real-time [Socket.io](https://socket.io/) chat using [Cypress.io](https://www.cypress.io/)\n\nThe original chat program copied from [dkhd/node-group-chat](https://github.com/dkhd/node-group-chat) as described in [Build A Group-Chat App in 30 Lines Using Node.js](https://itnext.io/build-a-group-chat-app-in-30-lines-using-node-js-15bfe7a2417b) blog post.\n\n![Chat test](images/chat.png)\n\n## Blog posts\n\nTesting a Socket.io chat application can be done in several ways. Read the following blog posts to see the alternatives.\n\nTitle | Description\n---|---\n[Test a Socket.io Chat App using Cypress](https://glebbahmutov.com/blog/test-socketio-chat-using-cypress/) | Simulates the second user by connecting to the chat server from the plugins file\n[Run Two Cypress Test Runners At The Same Time](https://glebbahmutov.com/blog/run-two-cypress-runners/) | Launches two test runners, giving them separate specs to run\n[Sync Two Cypress Runners via Checkpoints](https://glebbahmutov.com/blog/sync-two-cypress-runners/) | Launches two test runners, which stay in sync by communicating via their own Socket.io server\n[Code Coverage For Chat App](https://glebbahmutov.com/blog/code-coverage-for-chat-tests/) | Instruments and measures the fullstack code coverage\n\nFlip through the presentation slides [E2E Testing For A Real-Time Chat Web Application](https://slides.com/bahmutov/e2e-for-chat)\n\n## Specs\n\nName | Description\n---|---\n[first-spec](./cypress/integration/first-spec.js) | Tests that the user can post a message and see it\n[random-name-spec.js](./cypress/integration/random-name-spec.js) | Creates a random user name for the test\n[client-api-spec.js](./cypress/integration/client-api-spec.js) | Invokes events as a 2nd user using app actions\n[socket-spec.js](./cypress/integration/socket-spec.js) | Mimics the 2nd user by connecting to the Socket.io server from the plugin file\n[socket-from-browser-spec.js](./cypress/integration/socket-from-browser-spec.js) | Mimics the 2nd user by connecting to the Socket.io server from the spec file\n[mock-socket-spec.js](./cypress/integration/mock-socket-spec.js) | The test forces the app to use a mock socket object instead of the real connection, see [video](https://youtu.be/soNyOqpi_gQ)\n[disconnect-spec.js](./cypress/integration/disconnect-spec.js) | Checks if the user disconnects correctly\n\n## Running 2 Cypress instances\n\nThis repo also shows how to run 2 Cypress instances at the same time to \"really\" chat with each other.\n\n- start the server with `npm start`\n- execute `npm run chat:run` which starts the two Cypress processes\n\nLook at the [package.json](./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](./cy-first-user.json) and [cy-second-user.json](./cy-second-user.json) config files.\n\nThe test runners wait for each other using a common Socket.io server created in the [chat.js](./cypress/pair/chat.js) script. This is a separate Socket.io server from the application.\n\n![Test communication](./images/chat-server.png)\n\nThe first server logs in and reports that it is ready for the second test to start\n\n```js\n// cypress/pair/first-user.js\n/// \u003creference types=\"cypress\" /\u003e\n\n// this test behaves as the first user to join the chat\nit('chats with the second user', () =\u003e {\n  const name = 'First'\n  const secondName = 'Second'\n\n  cy.visit('/', {\n    onBeforeLoad(win) {\n      cy.stub(win, 'prompt').returns(name)\n    },\n  })\n\n  // make sure the greeting message is shown\n  cy.contains('#messages li i', `${name} join the chat..`).should('be.visible')\n  cy.task('checkpoint', 'first user has joined')\n})\n```\n\nThe second test runner executes its own test. It first waits for the checkpoint before visiting the page\n\n```js\n// cypress/pair/second-user.js\n/// \u003creference types=\"cypress\" /\u003e\n\n// this test behaves as the second user to join the chat\nit('chats with the first user', () =\u003e {\n  cy.task('waitForCheckpoint', 'first user has joined')\n\n  const name = 'Second'\n  // we are chatting with the first user\n  const firstName = 'First'\n  cy.visit('/', {\n    onBeforeLoad(win) {\n      cy.stub(win, 'prompt').returns(name)\n    },\n  })\n})\n```\n\n## Code coverage\n\nAdded instrumenting front-end code using `istanbul-lib-instrument` module, see [index.js](./index.js) file. Read the blog post [Code Coverage For Chat App](https://glebbahmutov.com/blog/code-coverage-for-chat-tests/).\n\n![Fullstack code coverage](./images/second-coverage.png)\n\nNote: I did not use [Istanbul middleware](https://github.com/gotwarlost/istanbul-middleware#readme) because it seemed to not support ES6 syntax (the middleware module had no releases for a long time).\n\n[ci image]: https://github.com/bahmutov/cypress-socketio-chat/workflows/ci/badge.svg?branch=main\n[ci url]: https://github.com/bahmutov/cypress-socketio-chat/actions\n[badges image]: https://github.com/bahmutov/cypress-socketio-chat/workflows/badges/badge.svg?branch=main\n[badges url]: https://github.com/bahmutov/cypress-socketio-chat/actions\n[renovate-badge]: https://img.shields.io/badge/renovate-app-blue.svg\n[renovate-app]: https://renovateapp.com/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbahmutov%2Fcypress-socketio-chat","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbahmutov%2Fcypress-socketio-chat","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbahmutov%2Fcypress-socketio-chat/lists"}