Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(local-server): corrected issue where local server could come back…
… on a random port after restart Replaced get-port with portfinder to avoid issue sindresorhus/get-port#43
- Loading branch information
Showing
8 changed files
with
304 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import 'mocha'; | ||
|
||
import { expect } from '@integration/testing-tools'; | ||
import { Ensure, equals } from '@serenity-js/assertions'; | ||
import { Actor, actorCalled, actorInTheSpotlight, Cast, configure, Log } from '@serenity-js/core'; | ||
import { CallAnApi } from '@serenity-js/rest'; | ||
import axios from 'axios'; | ||
import { given } from 'mocha-testdata'; | ||
import { satisfies } from 'semver'; | ||
import { LocalServer, ManageALocalServer, StartLocalServer, StopLocalServer } from '../src'; | ||
import servers = require('./servers'); | ||
|
||
describe('ManageALocalServer', () => { | ||
|
||
/** @test {ManageALocalServer} */ | ||
describe('restarting', () => { | ||
|
||
given(servers). | ||
it('allows the Actor to restart a server on the same port multiple times', function ({ handler, node }) { | ||
if (! satisfies(process.versions.node, node)) { | ||
return this.skip(); | ||
} | ||
|
||
class Actors implements Cast { | ||
prepare(actor: Actor): Actor { | ||
return actor.whoCan( | ||
ManageALocalServer.runningAHttpListener(handler()), | ||
CallAnApi.using(axios.create()), | ||
); | ||
} | ||
} | ||
|
||
configure({ | ||
actors: new Actors(), | ||
}); | ||
|
||
return expect(actorCalled('Nadia').attemptsTo( | ||
StartLocalServer.onPort(30000), | ||
Ensure.that(LocalServer.url(), equals('http://127.0.0.1:30000')), | ||
StopLocalServer.ifRunning(), | ||
StartLocalServer.onPort(30000), | ||
Log.the(LocalServer.url()), | ||
Ensure.that(LocalServer.url(), equals('http://127.0.0.1:30000')), | ||
)).to.be.fulfilled; // tslint:disable-line:no-unused-expression | ||
}); | ||
|
||
afterEach(() => actorInTheSpotlight().attemptsTo( | ||
StopLocalServer.ifRunning(), | ||
)); | ||
}); | ||
}); |
73 changes: 73 additions & 0 deletions
73
packages/local-server/spec/screenplay/interactions/StartLocalServer.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import 'mocha'; | ||
|
||
import { expect } from '@integration/testing-tools'; | ||
import { and, Ensure, equals, isGreaterThan, isLessThan, or } from '@serenity-js/assertions'; | ||
import { Actor, actorCalled, actorInTheSpotlight, Cast, configure } from '@serenity-js/core'; | ||
import { given } from 'mocha-testdata'; | ||
import { Server } from 'net'; | ||
import { satisfies } from 'semver'; | ||
import { RequestListener } from '../../../src/screenplay/abilities'; | ||
|
||
import { LocalServer, ManageALocalServer, StartLocalServer, StopLocalServer } from '../../../src'; | ||
import servers = require('../../servers'); | ||
|
||
/** @test {StartALocalServer} */ | ||
/** @test {LocalServer.url} */ | ||
/** @test {LocalServer.port} */ | ||
describe('StartALocalServer', () => { | ||
|
||
class Actors implements Cast { | ||
constructor(private readonly listener: () => RequestListener | Server) { | ||
} | ||
|
||
prepare(actor: Actor): Actor { | ||
return actor.whoCan(ManageALocalServer.runningAHttpListener(this.listener())); | ||
} | ||
} | ||
|
||
given(servers). | ||
it('allows the Actor to start the server on a preferred port', function ({ handler, node }) { | ||
if (! satisfies(process.versions.node, node)) { | ||
return this.skip(); | ||
} | ||
|
||
configure({ actors: new Actors(handler) }); | ||
|
||
return expect(actorCalled('Nadia').attemptsTo( | ||
StartLocalServer.onPort(30000), | ||
Ensure.that(LocalServer.port(), equals(30000)), | ||
)).to.be.fulfilled; // tslint:disable-line:no-unused-expression | ||
}); | ||
|
||
given(servers). | ||
it('allows the Actor to start the server on a random port', function ({ handler, node }) { | ||
if (! satisfies(process.versions.node, node)) { | ||
return this.skip(); | ||
} | ||
|
||
configure({ actors: new Actors(handler) }); | ||
|
||
return expect(actorCalled('Nadia').attemptsTo( | ||
StartLocalServer.onRandomPort(), | ||
Ensure.that(LocalServer.port(), and(or(equals(8000), isGreaterThan(8000)), or(isLessThan(65535), equals(65535)))), | ||
)).to.be.fulfilled; // tslint:disable-line:no-unused-expression | ||
}); | ||
|
||
given(servers). | ||
it('allows the Actor to start the server on a random port within a range', function ({ handler, node }) { | ||
if (! satisfies(process.versions.node, node)) { | ||
return this.skip(); | ||
} | ||
|
||
configure({ actors: new Actors(handler) }); | ||
|
||
return expect(actorCalled('Nadia').attemptsTo( | ||
StartLocalServer.onRandomPortBetween(8080, 9090), | ||
Ensure.that(LocalServer.port(), and(or(equals(8080), isGreaterThan(8080)), or(isLessThan(9090), equals(9090)))), | ||
)).to.be.fulfilled; // tslint:disable-line:no-unused-expression | ||
}); | ||
|
||
afterEach(() => actorInTheSpotlight().attemptsTo( | ||
StopLocalServer.ifRunning(), | ||
)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.