{"id":19237151,"url":"https://github.com/epicweb-dev/test-server","last_synced_at":"2025-04-08T04:17:56.638Z","repository":{"id":254451570,"uuid":"846175421","full_name":"epicweb-dev/test-server","owner":"epicweb-dev","description":"Utility for creating HTTP and WebSocket servers for testing.","archived":false,"fork":false,"pushed_at":"2024-08-23T13:09:18.000Z","size":47,"stargazers_count":108,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-02T22:43:57.502Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://npm.im/@epic-web/test-server","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/epicweb-dev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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":"2024-08-22T17:13:55.000Z","updated_at":"2025-03-21T03:59:46.000Z","dependencies_parsed_at":null,"dependency_job_id":"5af7703f-700d-4fda-b5ed-1255d46be298","html_url":"https://github.com/epicweb-dev/test-server","commit_stats":null,"previous_names":["epicweb-dev/test-server"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/epicweb-dev%2Ftest-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/epicweb-dev%2Ftest-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/epicweb-dev%2Ftest-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/epicweb-dev%2Ftest-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/epicweb-dev","download_url":"https://codeload.github.com/epicweb-dev/test-server/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247773722,"owners_count":20993639,"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-11-09T16:25:00.431Z","updated_at":"2025-04-08T04:17:56.590Z","avatar_url":"https://github.com/epicweb-dev.png","language":"TypeScript","readme":"# Test Server\n\nUtility for creating HTTP and WebSocket servers for testing.\n\n## Features\n\n- **Compact**. Spawn servers on-demand while keeping the test setup to the minimum.\n- **Automatically disposable**. This utility is build with the [`using`](https://www.totaltypescript.com/typescript-5-2-new-keyword-using) keyword in mind. Any servers you spawn are automatically closed once nothing is using them.\n- **Standard-based**. Handle requests and responses using the web standards. This library uses [Hono](https://hono.dev/) to spawn servers.\n\n## Install\n\n```sh\nnpm install @epic-web/test-server\n```\n\n## Usage\n\n### HTTP server\n\n```ts\nimport { createTestHttpServer } from '@epic-web/test-server/http'\n\nit('fetches the list of numbers', async () =\u003e {\n  // Create a disposable \"server\" instance.\n  await using server = await createTestHttpServer({\n    defineRoutes(router) {\n      router.get('/numbers', () =\u003e {\n        return Response.json([1, 2, 3])\n      })\n    }\n  })\n\n  // Construct URLs relatively to the test server.\n  const response = await fetch(server.http.url('/numbers'))\n  await expect(response.json()).resolves.toEqual([1, 2, 3])\n})\n```\n\n### WebSocket server\n\n```ts\nimport { createTestHttpServer } from '@epic-web/test-server/http'\nimport { createWebSocketMiddleware } from '@epic-web/test-server/ws'\n\nit('handles WebSocket communication', async () =\u003e {\n  await using server = await createTestHttpServer()\n  // Attach WebSockets as a middleware to an existing HTTP server.\n  await using wss = createWebSocketMiddleware({ server })\n\n  // Handle WebSocket connections.\n  wss.ws.on('connect', (socket) =\u003e console.log('new connection!'))\n\n  const client = new WebSocket(wss.ws.url())\n})\n```\n\n## API\n\n### `createTestHttpServer([options])`\n\nCreates an HTTP server instance.\n\n- `options` (optional)\n  - `protocols`, (optional) `Array\u003c'http' | 'https'\u003e` (default: `['http']`), the list of protocols to use when spawning the test server. Providing multiple values will spawn multiple servers with the corresponding controls via `server.http` and `server.https`.\n  - `defineRoutes`, (optional) `(router: Router) =\u003e void`, a function describing the server's routes.\n- Returns: [Promise\u003c`TestHttpServer`\u003e](#testhttpserver)\n\n### `TestHttpServer`\n\n#### `TestHttpServer.http.url([pathname])`\n\n- `pathname` (optional, default: `/`), `string`, a pathname to resolve against the server's URL.\n- Returns: `URL`.\n\nCalling the `.url()` method without any arguments returns this server's URL:\n\n```ts\nserver.http.url() // URL\u003chttp://localhost:56783/\u003e\n```\n\nProviding the `pathname` argument returns a URL for that path:\n\n```ts\nserver.http.url('/resource') // URL\u003chttp://localhost:56783/resource\u003e\n```\n\n#### `TestHttpServer.close()`\n\nCloses the HTTP server, aborting any pending requests.\n\n\u003e [!IMPORTANT]\n\u003e The `createTestHttpServer()` is a _disposable_ utility. It means that JavaScript will automatically dispose of the server instance (i.e. close it) when nothing else is referencing the `server` object. _You don't have to manually close the server_. But you _can_ close the server amidst a test, if that's what your test needs.\n\n### `createWebSocketMiddleware(options)`\n\n- `options`\n  - `server`, [`TestHttpServer`](#testhttpserver), a reference to the existing test HTTP server object.\n- Returns: [TestWebSocketServer](#testwebsocketserver)\n\n\u003e Note: The WebSocket middleware will automatically attach itself to all spawned HTTP servers. If you are using multiple servers at once (e.g. HTTP + HTTPS), both `wss.ws` and `wss.wss` APIs will be available for WebSockets respectively.\n\n### `TestWebSocketServer`\n\n#### `TestWebSocketServer.on(type, listener)`\n\nAdds a listener for the given event.\n\n```ts\nwss.ws.on('connection', () =\u003e {})\nwss.wss.on('connection', () =\u003e {})\n```\n\n#### `TestWebSocketServer.once(type, listener)`\n\nAdds a one-time listener for the given event.\n\n#### `TestWebSocketServer.off(type, listener)`\n\nRemoves a listener from the given event.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fepicweb-dev%2Ftest-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fepicweb-dev%2Ftest-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fepicweb-dev%2Ftest-server/lists"}