{"id":13725170,"url":"https://github.com/thoov/mock-socket","last_synced_at":"2025-05-13T19:13:21.253Z","repository":{"id":23207442,"uuid":"26564282","full_name":"thoov/mock-socket","owner":"thoov","description":"Javascript mocking library for WebSockets and Socket.IO","archived":false,"fork":false,"pushed_at":"2025-02-10T06:42:04.000Z","size":2480,"stargazers_count":802,"open_issues_count":74,"forks_count":121,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-04-25T22:49:53.652Z","etag":null,"topics":["javascript","mocking","testing","websockets"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thoov.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","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,"zenodo":null}},"created_at":"2014-11-13T01:15:36.000Z","updated_at":"2025-03-24T06:05:44.000Z","dependencies_parsed_at":"2025-04-11T14:07:42.507Z","dependency_job_id":"de07837a-57bf-4685-8dd9-0b907aa4f0d2","html_url":"https://github.com/thoov/mock-socket","commit_stats":{"total_commits":436,"total_committers":51,"mean_commits":8.549019607843137,"dds":0.3325688073394495,"last_synced_commit":"47eab99d42b4f17abe8b7cc6ab1466cca6768750"},"previous_names":[],"tags_count":50,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thoov%2Fmock-socket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thoov%2Fmock-socket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thoov%2Fmock-socket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thoov%2Fmock-socket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thoov","download_url":"https://codeload.github.com/thoov/mock-socket/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250907688,"owners_count":21506069,"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":["javascript","mocking","testing","websockets"],"created_at":"2024-08-03T01:02:14.911Z","updated_at":"2025-04-25T22:49:58.426Z","avatar_url":"https://github.com/thoov.png","language":"JavaScript","readme":"\u003cp align=\"center\"\u003e\n  \u003cimg width=600 src=\"http://imgur.com/Xt9X83M.png\"\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\nJavascript mocking library for \u003ca href=\"https://developer.mozilla.org/en-US/docs/WebSockets\"\u003ewebsockets\u003c/a\u003e and \u003ca href=\"http://socket.io/\"\u003esocket.io\u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://github.com/thoov/mock-socket/actions\"\u003e\n    \u003cimg src=\"https://github.com/thoov/mock-socket/workflows/CI/badge.svg\" alt=\"Build Status\"\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n## Contents\n\n- [Installation](#installation)\n- [Basic Usage](#usage)\n- [Advanced Usage](#advanced-usage)\n- [Typescript Support](#typescript-support)\n- [Socket.IO](#socket-io)\n- [Contributing](#contributing)\n- [Feedback](#feedback)\n\n## Installation\n\n```shell\nnpm install mock-socket\n```\n\n```js\nimport { WebSocket, Server } from 'mock-socket';\n```\n\n## Usage\n\n```js\nimport test from 'ava';\nimport { Server } from 'mock-socket';\n\nclass ChatApp {\n  constructor(url) {\n    this.messages = [];\n    this.connection = new WebSocket(url);\n\n    this.connection.onmessage = event =\u003e {\n      this.messages.push(event.data);\n    };\n  }\n\n  sendMessage(message) {\n    this.connection.send(message);\n  }\n}\n\ntest.cb('that chat app can be mocked', t =\u003e {\n  const fakeURL = 'ws://localhost:8080';\n  const mockServer = new Server(fakeURL);\n\n  mockServer.on('connection', socket =\u003e {\n    socket.on('message', data =\u003e {\n      t.is(data, 'test message from app', 'we have intercepted the message and can assert on it');\n      socket.send('test message from mock server');\n    });\n  });\n\n  const app = new ChatApp(fakeURL);\n  app.sendMessage('test message from app'); // NOTE: this line creates a micro task\n\n  // NOTE: this timeout is for creating another micro task that will happen after the above one\n  setTimeout(() =\u003e {\n    t.is(app.messages.length, 1);\n    t.is(app.messages[0], 'test message from mock server', 'we have stubbed our websocket backend');\n    mockServer.stop(t.done);\n  }, 100);\n});\n```\n\n## Advanced Usage\n\n### Stubbing the \"global\"\n\n```js\nimport { WebSocket, Server } from 'mock-socket';\n\n/*\n * By default the global WebSocket object is stubbed out when \n * a new Server instance is created and is restored when you stop\n * the server.\n * However, you can disable this behavior by passing `mock: false`\n * to the options and manually mock the socket when you need it.\n */\nconst server = new Server('ws://localhost:8080', { mock: false });\n\n/*\n * If you need to stub something else out you can like so:\n */\n\nwindow.WebSocket = WebSocket; // Here we stub out the window object\n```\n\n### Server Methods\n\n```js\nconst mockServer = new Server('ws://localhost:8080');\n\nmockServer.on('connection', socket =\u003e {\n  socket.on('message', () =\u003e {});\n  socket.on('close', () =\u003e {});\n  socket.on('error', () =\u003e {});\n\n  socket.send('message');\n  socket.close();\n});\n\nmockServer.clients(); // array of all connected clients\nmockServer.emit('room', 'message');\nmockServer.stop(optionalCallback);\n```\n\n## Typescript Support\n\nA [declaration file](https://github.com/thoov/mock-socket/blob/master/index.d.ts) is included by default. If you notice any issues with the types please create an issue or a PR!\n\n## Socket IO\n\n[Socket.IO](https://socket.io/) has **limited support**. Below is a similar example to the one above but modified to show off socket.io support.\n\n```js\nimport test from 'ava';\nimport { SocketIO, Server } from 'mock-socket';\n\nclass ChatApp {\n  constructor(url) {\n    this.messages = [];\n    this.connection = new io(url);\n\n    this.connection.on('chat-message', data =\u003e {\n      this.messages.push(event.data);\n    });\n  }\n\n  sendMessage(message) {\n    this.connection.emit('chat-message', message);\n  }\n}\n\ntest.cb('that socket.io works', t =\u003e {\n  const fakeURL = 'ws://localhost:8080';\n  const mockServer = new Server(fakeURL);\n\n  window.io = SocketIO;\n\n  mockServer.on('connection', socket =\u003e {\n    socket.on('chat-message', data =\u003e {\n      t.is(data, 'test message from app', 'we have intercepted the message and can assert on it');\n      socket.emit('chat-message', 'test message from mock server');\n    });\n  });\n\n  const app = new ChatApp(fakeURL);\n  app.sendMessage('test message from app');\n\n  setTimeout(() =\u003e {\n    t.is(app.messages.length, 1);\n    t.is(app.messages[0], 'test message from mock server', 'we have subbed our websocket backend');\n\n    mockServer.stop(t.done);\n  }, 100);\n});\n```\n\n## Contributing\n\nThe easiest way to work on the project is to clone the repo down via:\n\n```shell\ngit clone git@github.com:thoov/mock-socket.git\ncd mock-socket\nyarn install\n```\n\nThen to create a local build via:\n\n```shell\nyarn build\n```\n\nThen create a local npm link via:\n\n```shell\nyarn link\n```\n\nAt this point you can create other projects / apps locally and reference this local build via:\n\n```shell\nyarn link mock-socket\n```\n\nfrom within your other projects folder. Make sure that after any changes you run `yarn build`!\n\n### Tests\n\nThis project uses [ava.js](https://github.com/avajs/ava) as its test framework. Tests are located in /tests. To run tests:\n\n```shell\nyarn test\n```\n\n### Linting\n\nThis project uses eslint and a rules set from [airbnb's javascript style guides](https://github.com/airbnb/javascript). To run linting:\n\n```shell\nyarn lint\n```\n\n### Formatting\n\nThis project uses [prettier](https://github.com/prettier/prettier). To run the formatting:\n\n```shell\nyarn format\n```\n\n### Code Coverage\n\nCode coverage reports are created in /coverage after all of the tests have successfully passed. To run the coverage:\n\n```shell\nyarn test:coverage\n```\n\n## Feedback\n\nIf you have any feedback, encounter any bugs, or just have a question, please feel free to create a [github issue](https://github.com/thoov/mock-socket/issues/new) or send me a tweet at [@thoov](https://twitter.com/thoov).\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthoov%2Fmock-socket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthoov%2Fmock-socket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthoov%2Fmock-socket/lists"}