Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
@epic-web/test-server
Advanced tools
Utility for creating HTTP and WebSocket servers for testing
Utility for creating HTTP and WebSocket servers for testing.
using
keyword in mind. Any servers you spawn are automatically closed once nothing is using them.npm install @epic-web/test-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])
})
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())
})
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.TestHttpServer
>TestHttpServer
TestHttpServer.http.url([pathname])
pathname
(optional, default: /
), string
, a pathname to resolve against the server's URL.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 theserver
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)
options
server
, TestHttpServer
, a reference to the existing test HTTP server object.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
andwss.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.
FAQs
Utility for creating HTTP and WebSocket servers for testing
The npm package @epic-web/test-server receives a total of 199 weekly downloads. As such, @epic-web/test-server popularity was classified as not popular.
We found that @epic-web/test-server demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.