@epic-web/test-server

中文文档
0.1.6发布于 2025/06/02MITTypeScriptES Module近 30 天下载 4.4万

Utility for creating HTTP and WebSocket servers for testing

原始使用文档
文档目录10 节
  1. Features
  2. Install
  3. Usage
  4. HTTP server
  5. WebSocket server
  6. API
  7. createTestHttpServer([options])
  8. TestHttpServer
  9. createWebSocketMiddleware(options)
  10. TestWebSocketServer

Test Server

Utility for creating HTTP and WebSocket servers for testing.

Features

  • Compact. Spawn servers on-demand while keeping the test setup to the minimum.
  • Automatically disposable. This utility is build with the using keyword in mind. Any servers you spawn are automatically closed once nothing is using them.
  • Standard-based. Handle requests and responses using the web standards. This library uses Hono to spawn servers.

Install

npm install @epic-web/test-server

Usage

HTTP server

import { createTestHttpServer } from '@epic-web/test-server/http'

it('fetches the list of numbers', async () => {
  // Create a disposable "server" instance.
  await using server = await createTestHttpServer({
    defineRoutes(router) {
      router.get('/numbers', () => {
        return Response.json([1, 2, 3])
      })
    }
  })

  // Construct URLs relatively to the test server.
  const response = await fetch(server.http.url('/numbers'))
  await expect(response.json()).resolves.toEqual([1, 2, 3])
})

WebSocket server

import { createTestHttpServer } from '@epic-web/test-server/http'
import { createWebSocketMiddleware } from '@epic-web/test-server/ws'

it('handles WebSocket communication', async () => {
  await using server = await createTestHttpServer()
  // Attach WebSockets as a middleware to an existing HTTP server.
  await using wss = createWebSocketMiddleware({ server })

  // Handle WebSocket connections.
  wss.ws.on('connect', (socket) => console.log('new connection!'))

  const client = new WebSocket(wss.ws.url())
})

API

createTestHttpServer([options])

Creates an HTTP server instance.

  • options (optional)
    • protocols, (optional) Array<'http' | 'https'> (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.
    • defineRoutes, (optional) (router: Router) => void, a function describing the server's routes.
  • Returns: Promise<TestHttpServer>

TestHttpServer

TestHttpServer.http.url([pathname])

  • pathname (optional, default: /), string, a pathname to resolve against the server's URL.
  • Returns: URL.

Calling the .url() method without any arguments returns this server's URL:

server.http.url() // URL<http://localhost:56783/>

Providing the pathname argument returns a URL for that path:

server.http.url('/resource') // URL<http://localhost:56783/resource>

TestHttpServer.close()

Closes the HTTP server, aborting any pending requests.

[!IMPORTANT] 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.

createWebSocketMiddleware(options)

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.

TestWebSocketServer

TestWebSocketServer.on(type, listener)

Adds a listener for the given event.

wss.ws.on('connection', () => {})
wss.wss.on('connection', () => {})

TestWebSocketServer.once(type, listener)

Adds a one-time listener for the given event.

TestWebSocketServer.off(type, listener)

Removes a listener from the given event.

版本与发布记录

@epic-web/test-server 共发布 7 个版本,首次发布于 2024/08/23;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.6latest2025/06/021.5 年前
0.1.52025/04/281.5 年前
0.1.42025/04/271.5 年前
0.1.32025/04/271.5 年前
0.1.22025/04/271.5 年前
0.1.12025/04/271.5 年前

关于 @epic-web/test-server 的常见问题

@epic-web/test-server 是什么?

Utility for creating HTTP and WebSocket servers for testing

如何安装 @epic-web/test-server?

在项目目录运行 npm install @epic-web/test-server,或使用 pnpm add / yarn add / bun add。

@epic-web/test-server 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@epic-web/test-server 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@epic-web/test-server 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@epic-web/test-server 可以商用吗?

@epic-web/test-server 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@epic-web/test-server 的下载量有多少?

根据 npm 官方下载统计,@epic-web/test-server 在 2026/10/14 – 2026/11/12 这 30 天内被下载 43,746 次,日均约 1,458 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@epic-web/test-server 还在维护吗?

最新版本 0.1.6 发布于 2025/06/02(1.5 年前);至今共发布 7 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/08/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。