@open-draft/test-server

0.6.2 • Public • Published

npm

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)
})

Package Sidebar

Install

npm i @open-draft/test-server

Weekly Downloads

381

Version

0.6.2

License

MIT

Unpacked Size

16.1 kB

Total Files

10

Last publish

Collaborators

  • kettanaito