{"id":17808299,"url":"https://github.com/loaderb0t/playwright-easy-network-stub","last_synced_at":"2025-08-02T06:08:40.364Z","repository":{"id":57688153,"uuid":"475890997","full_name":"LoaderB0T/playwright-easy-network-stub","owner":"LoaderB0T","description":"An easy class to mock a lot of network requests in playwright","archived":false,"fork":false,"pushed_at":"2024-10-03T23:34:55.000Z","size":430,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-05T10:02:37.812Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/LoaderB0T.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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}},"created_at":"2022-03-30T13:27:13.000Z","updated_at":"2024-11-20T08:07:34.000Z","dependencies_parsed_at":"2024-04-15T15:28:23.817Z","dependency_job_id":"d3a1ff5e-65c3-435c-a7d8-7a78437e2a14","html_url":"https://github.com/LoaderB0T/playwright-easy-network-stub","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/LoaderB0T%2Fplaywright-easy-network-stub","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LoaderB0T%2Fplaywright-easy-network-stub/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LoaderB0T%2Fplaywright-easy-network-stub/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LoaderB0T%2Fplaywright-easy-network-stub/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LoaderB0T","download_url":"https://codeload.github.com/LoaderB0T/playwright-easy-network-stub/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243865598,"owners_count":20360470,"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":[],"created_at":"2024-10-27T15:10:19.281Z","updated_at":"2025-08-02T06:08:40.348Z","avatar_url":"https://github.com/LoaderB0T.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![npm](https://img.shields.io/npm/v/playwright-easy-network-stub?color=%2300d26a\u0026style=for-the-badge)](https://www.npmjs.com/package/playwright-easy-network-stub)\n[![CI](https://img.shields.io/github/actions/workflow/status/LoaderB0T/playwright-easy-network-stub/build.yml?branch=main\u0026style=for-the-badge)](https://github.com/LoaderB0T/playwright-easy-network-stub/actions/workflows/build.yml)\n[![Sonar Quality Gate](https://img.shields.io/sonar/quality_gate/LoaderB0T_playwright-easy-network-stub?server=https%3A%2F%2Fsonarcloud.io\u0026style=for-the-badge)](https://sonarcloud.io/summary/new_code?id=LoaderB0T_playwright-easy-network-stub)\n[![bundle size](https://img.shields.io/bundlephobia/minzip/playwright-easy-network-stub?color=%23FF006F\u0026label=Bundle%20Size\u0026style=for-the-badge)](https://bundlephobia.com/package/playwright-easy-network-stub)\n\n# playwright-easy-network-stub\n\nAn easy class to mock a lot of network requests in playwright.\n\n## See also 🔬\n\nThe base class of this package is:\n[https://github.com/LoaderB0T/easy-network-stub](https://github.com/LoaderB0T/easy-network-stub)\n\n## Motivation 💥\n\nWhen running tests in playwright, sometimes you want to mock all network requests. Especially when running tests in a CI environment. This package provides an easy and type safe way to mock network requests for a whole API.\n\n## Features 🔥\n\n✅ Easy to setup and use\n\n✅ Type safe url and query parameters\n\n✅ Add your own parameter matchers\n\n✅ Works asynchronously (Promise based)\n\n✅ Supports failing of stubbed requests\n\n✅ Supports EventSource \u0026 ndjson\n\n✅ Supports WebSockets (beta)\n\n✅ No production dependencies (You need to have playwright installed, of course)\n\n✅ ESM \u0026 CJS exports\n\nThe primary use case for this package is to create a mock server for your tests so that they do not need real network requests.\n\n## Built With 🔧\n\n- [TypeScript](https://www.typescriptlang.org/)\n\n## Usage Example 🚀\n\n```typescript\nconst posts = [0, 1, 2, 3, 4, 5].map(x =\u003e ({ postId: x, text: `test${x}` }));\n\nconst blogStub = new PlaywrightEasyNetworkStub(/MyServer\\/api\\/Blog/);\n\nblogStub.init();\n\nblogStub.stub('GET', 'posts', () =\u003e {\n  return posts;\n});\n\n// Match Example: GET: /MyServer/api/Blog/posts/123\nblogStub.stub('GET', 'posts/{id:number}', ({ params }) =\u003e {\n  return posts.find(x =\u003e x.postId === params.id);\n});\n\n// Match Example: POST: /MyServer/api/post\nblogStub.stub('POST', 'posts', ({ body, params }) =\u003e {\n  posts.push({ postId: body.postId, text: body.text });\n});\n\n// Match Example: POST: /MyServer/api/Blog/test/true?query=myValue\u0026secondQuery=myOtherValue\n// Note: The order of the query parameters is not important\nblogStub.stub('POST', 'test/{something:boolean}?{query:string}\u0026{secondQuery:number}', ({ body, params }) =\u003e {\n  console.log(params.something);\n  console.log(params.query);\n  console.log(params.secondQuery);\n  console.log(body);\n});\n\n// Here we use the stub2\u003c\u003e() method to create a stub with a typed body\nblogStub.stub2\u003cMyRequest\u003e()('POST', 'test', ({ body }) =\u003e {\n  console.log(body.myValue);\n});\n\n// You can mark query params as optional with a '?'\n// Match Example: GET: /MyServer/api/Blog/test\n// Match Example: GET: /MyServer/api/Blog/test?refresh=true\nblogStub.stub('GET', 'test?{refresh?:boolean}', ({ body, params }) =\u003e {\n  if (params.refresh) {\n    console.log('Refreshing');\n  }\n  console.log(body.myValue);\n});\n\n// You can mark query params as arrays with a '[]'\n// Match Example: GET: /MyServer/api/Blog/test?props=1\n// Match Example: GET: /MyServer/api/Blog/test?props=1\u0026props=2\nblogStub.stub('GET', 'test?{props:number[]}', ({ params }) =\u003e {\n  params.props.forEach(x =\u003e console.log(x));\n});\n\n\n// You can remove a stub with its handle or all stubs at once\nconst handle = blogStub.stub('GET', 'test?{props:number[]}', ({ params }) =\u003e {\n  params.props.forEach(x =\u003e console.log(x));\n});\nhandle.unregister(); // remove this stub\n// Or:\n\nblogStub.unregisterAll(); // remove all stubs\n```\n\n## Strongly typed api parameters\n\nYou can add types to parameters and they will be parsed. Out of box 'string', 'number' and 'boolean' are supported. You can add your own types and parsers though.\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://user-images.githubusercontent.com/37637338/162327029-994ce009-d1ab-45cc-ab86-d1e21a0d1a6e.png\"\u003e\n\u003cimg src=\"https://user-images.githubusercontent.com/37637338/162327040-a45381a1-652d-4838-91ae-7dc405bd9ff4.png\"\u003e\n\u003c/p\u003e\n\n## Stub streams\n\n```typescript\nimport { StreamResponseHandler } from 'easy-network-stub/stream';\n\nconst srh = new StreamResponseHandler();\nblogStub.stub('POST', 'test', () =\u003e srh);\n\nsrh.send('Hello');\nsrh.send('World');\nsrh.close();\n```\n\n## Contributing 🧑🏻‍💻\n\nContributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**.\n\nIf you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag \"enhancement\".\nDon't forget to give the project a star! Thanks again!\n\n1. Fork the Project\n2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`)\n3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`)\n4. Push to the Branch (`git push origin feature/AmazingFeature`)\n5. Open a Pull Request\n\n## License 🔑\n\nDistributed under the MIT License. See `LICENSE.txt` for more information.\n\n## Contact 📧\n\nJanik Schumacher - [@LoaderB0T](https://twitter.com/LoaderB0T) - [linkedin](https://www.linkedin.com/in/janikschumacher/)\n\nProject Link: [https://github.com/LoaderB0T/playwright-easy-network-stub](https://github.com/LoaderB0T/playwright-easy-network-stub)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Floaderb0t%2Fplaywright-easy-network-stub","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Floaderb0t%2Fplaywright-easy-network-stub","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Floaderb0t%2Fplaywright-easy-network-stub/lists"}