test-server
Spawns HTTP/HTTPS/WS/WSS local servers for testing.
Install
npm install @open-draft/test-server
HTTP
import fetch from 'node-fetch'
import { HttpServer } from '@open-draft/test-server/http'
const server = new HttpServer((app) => {
app.get('/numbers', (req, res) => {
return res.status(200).json([1, 2, 3])
})
})
beforeAll(async () => {
await server.listen()
})
afterAll(async () => {
await server.close()
})
test('fetches all the numbers', async () => {
const res = await fetch(server.http.url('/books'))
expect(res.status).toBe(200)
expect(await res.json()).toEqual([1, 2, 3])
})
WebSocket
import { WebSocketServer } from '@open-draft/test-server/ws'
const server = await WebSocketServer()
console.log('WebSocket server running at:', server.ws.address.href)
server.ws.instance.on('connect', (socket) => {
console.log('new connection', socket.id)
})