Skip to main content

ManageALocalServer

An Ability that enables an Actor to manage a local Node.js server.

Managing a raw Node.js server

import { actorCalled } from '@serenity-js/core'
import { CallAnApi, GetRequest, Send } from '@serenity-js/rest'
import { ManageALocalServer, LocalTestServer, StartLocalTestServer, StopLocalTestServer } from '@serenity-js/local-server'
import { Ensure, equals } from '@serenity-js/assertions'

import * as axios from 'axios'
import * as http from 'http'

const server = http.createServer(function (request, response) {
response.setHeader('Connection', 'close');
response.end('Hello!');
})

await actorCalled('Apisit')
.whoCan(
ManageALocalServer.using(server),
CallAnApi.using(axios.create()),
)
.attemptsTo(
StartLocalTestServer.onRandomPort(),
Send.a(GetRequest.to(LocalServer.url())),
Ensure.that(LastResponse.status(), equals(200)),
Ensure.that(LastResponse.body<string>(), equals('Hello!')),
StopLocalTestServer.ifRunning(),
)

Learn more

Hierarchy

  • Ability
    • ManageALocalServer

Index

Constructors

constructor

Methods

staticrunningAHttpListener

staticrunningAHttpsListener

  • runningAHttpsListener(listener: RequestListener | Server<typeof IncomingMessage, typeof ServerResponse>, options?: ServerOptions<typeof IncomingMessage, typeof ServerResponse>): ManageALocalServer

listen

  • listen(preferredPort?: number, highestPort?: number): Promise<void>
  • Starts the server on the first free port between preferredPort and highestPort, inclusive.


    Parameters

    • optionalpreferredPort: number = 8000

      Lower bound of the preferred port range

    • optionalhighestPort: number = 65535

      highestPort Upper bound of the preferred port range

    Returns Promise<void>

mapInstance