A minimal but customizable Express server for testing




Typescript based preconfigured Express application intended for quick testing requests and responses, it can can be customized to listen for HTTP and HTTPS traffic and alter the default body parser behavior.
The followingContent-Type
headers will be parsed and exposed viareq.body
:
- JSON (
application/json
) - Text (
text/plain
) - URL-Encoded (
application/x-www-form-urlencoded
) - Buffer (
application/octet-stream
)
npm install --save-dev @b4dnewz/express-test-server
importcreateServerfrom"@b4dnewz/express-test-server"constserver=awaitcreateServer({// server options});// Express route handlerserver.get('/foo',(req,res)=>{res.send('bar');});// Express alternative route handlersserver.get('/bar',()=>'foo');server.get('/baz','foo');
importcreateServerfrom"@b4dnewz/express-test-server"letserver;beforeAll(async()=>{server=awaitcreateServer();});afterAll(async()=>{awaitserver.close();});it("respond to get requests",async()=>{sever.get("/foo","bar")const{body}=awaitgot(`${server.url}/foo`)expect(body).toEqual("bar")})
port (default 0)
Specify a custom port for the HTTP server instance, otherwise it will automatically choose a random free TCP port
awaitcreateServer({port:8888})
sslPort (default443)
Specify a custom port for the HTTPS server instance, otherwise it will try to default ssl port
awaitcreateServer({sslPort:4443})
hostname (defaultlocalhost)
Specify a custom hostname for both HTTP and HTTPS servers, remember that you need a resolvable DNS host name for this to work.
awaitcreateServer({hostname:"0.0.0.0"})awaitcreateServer({hostname:"test.example.com"})
listen (defaulttrue)
If false will prevent the test server to automatically start to listen for requests when instanciated.
constserver=awaitcreateServer({listen:false})// listen later on a desired portawaitserver.listen({port:8888})
MIT