Skip to content
Permalink
Browse files
fix(local-server): startLocalServer will throw a ConfigurationError i…
…f the server was not started
  • Loading branch information
jan-molak committed Feb 6, 2020
1 parent d5bf71a commit c71f0a16db235c23a41be2186356cdb21dc80221
Showing with 39 additions and 8 deletions.
  1. +30 −3 packages/local-server/spec/reporting.spec.ts
  2. +9 −5 packages/local-server/src/screenplay/abilities/ManageALocalServer.ts
@@ -1,9 +1,10 @@
import 'mocha';

import { EventRecorder, expect, PickEvent } from '@integration/testing-tools';
import { Ensure, equals, startsWith } from '@serenity-js/assertions';
import { Actor, actorCalled, Cast, configure } from '@serenity-js/core';
import { endsWith, Ensure, equals, not, startsWith } from '@serenity-js/assertions';
import { Actor, actorCalled, Cast, ConfigurationError, configure, Log } from '@serenity-js/core';
import { ActivityFinished, ActivityStarts } from '@serenity-js/core/lib/events';
import { Photo } from '@serenity-js/core/lib/model';
import { CallAnApi, GetRequest, LastResponse, Send } from '@serenity-js/rest';
import axios from 'axios';

@@ -42,7 +43,7 @@ describe('@serenity-js/local-server', () => {
* @test {StartLocalServer}
* @test {StopLocalServer}
*/
it('correctly reports actor\'s activities', () => expect(actorCalled('Nadia').attemptsTo(
it(`correctly reports actor's activities`, () => expect(actorCalled('Nadia').attemptsTo(
StartLocalServer.onRandomPort(),
Ensure.that(LocalServer.url(), startsWith('http://127.0.0.1')),
Send.a(GetRequest.to(LocalServer.url())),
@@ -64,6 +65,32 @@ describe('@serenity-js/local-server', () => {
}));
});

describe('when managing a local server', () => {

let Nadia: Actor, Phillip: Actor;

const port = 5000;

beforeEach(() => {
Nadia = actorCalled('Nadia');
Phillip = actorCalled('Phillip');
});

it(`it falls back to a random port if the preferred one is taken`, () =>
expect(Nadia.attemptsTo(
StartLocalServer.onOneOfThePreferredPorts([port]),
Ensure.that(LocalServer.url(), endsWith(`${ port }`)),
).then(() => Phillip.attemptsTo(
StartLocalServer.onOneOfThePreferredPorts([port]),
Ensure.that(LocalServer.url(), not(endsWith(`${ port }`))),
))).to.be.fulfilled);

afterEach(() => Promise.all([
Nadia.attemptsTo(StopLocalServer.ifRunning()),
Phillip.attemptsTo(StopLocalServer.ifRunning()),
]));
});

function hasName(expectedName: string) {
return (e: ActivityStarts | ActivityFinished) => expect(e.value.name.value).equals(expectedName);
}
@@ -1,4 +1,4 @@
import { Ability, UsesAbilities } from '@serenity-js/core';
import { Ability, ConfigurationError, UsesAbilities } from '@serenity-js/core';
import getPort = require('get-port');
import * as http from 'http';
import withShutdownSupport = require('http-shutdown');
@@ -112,12 +112,16 @@ export class ManageALocalServer implements Ability {
*/
listen(preferredPorts: number[]): Promise<void> {
return getPort({ port: preferredPorts }).then(port => new Promise<void>((resolve, reject) => {
this.server.listen(port, '127.0.0.1', (error: Error) => {
if (!! error) {
return reject(error);
this.server.on('error', (error: Error & {code: string}) => {
if (error.code === 'EADDRINUSE') {
return reject(new ConfigurationError(`Server address is in use. Is there another server running on port ${ port }?`, error));
}

return resolve();
return reject(error);
});

this.server.listen(port, '127.0.0.1', () => {
resolve();
});
}));
}

0 comments on commit c71f0a1

Please sign in to comment.