@@ -25,7 +25,6 @@ import { Server } from "@pact-foundation/pact-node/src/server";
2525 * @param {PactOptions } opts
2626 * @return {@link PactProvider }
2727 */
28- // TODO: move this to its own module
2928export class Pact {
3029 public static defaults = {
3130 consumer : "" ,
@@ -35,7 +34,6 @@ export class Pact {
3534 log : path . resolve ( process . cwd ( ) , "logs" , "pact.log" ) ,
3635 logLevel : "info" ,
3736 pactfileWriteMode : "overwrite" ,
38- port : 1234 ,
3937 provider : "" ,
4038 spec : 2 ,
4139 ssl : false ,
@@ -70,29 +68,26 @@ export class Pact {
7068 host : this . opts . host ,
7169 log : this . opts . log ,
7270 pactFileWriteMode : this . opts . pactfileWriteMode ,
73- port : this . opts . port ,
71+ port : config . port , // allow to be undefined
7472 provider : this . opts . provider ,
7573 spec : this . opts . spec ,
7674 ssl : this . opts . ssl ,
7775 sslcert : this . opts . sslcert ,
7876 sslkey : this . opts . sslkey ,
7977 } ) ;
80-
81- logger . info ( `Setting up Pact with Consumer "${ this . opts . consumer } " and Provider "${ this . opts . provider } "
82- using mock service on Port: "${ this . opts . port } "` ) ;
83-
84- this . mockService = new MockService ( undefined , undefined , this . opts . port , this . opts . host ,
85- this . opts . ssl , this . opts . pactfileWriteMode ) ;
8678 }
8779
8880 /**
8981 * Start the Mock Server.
9082 * @returns {Promise }
9183 */
92- public setup ( ) : Promise < void > {
93- return isPortAvailable ( this . opts . port , this . opts . host )
94- // Need to wrap it this way until we remove q.Promise from pact-node
95- . then ( ( ) => new Promise < void > ( ( resolve , reject ) => this . server . start ( ) . then ( ( ) => resolve ( ) , ( e : any ) => reject ( e ) ) ) )
84+ public setup ( ) : Promise < PactOptionsComplete > {
85+ return this . checkPort ( )
86+ . then ( ( ) => this . startServer ( ) )
87+ . then ( ( opts ) => {
88+ this . setupMockService ( )
89+ return Promise . resolve ( opts )
90+ } )
9691 }
9792
9893 /**
@@ -188,6 +183,33 @@ export class Pact {
188183 public removeInteractions ( ) : Promise < string > {
189184 return this . mockService . removeInteractions ( ) ;
190185 }
186+
187+ private checkPort ( ) : Promise < void > {
188+ if ( this . server && this . server . options . port ) {
189+ return isPortAvailable ( this . server . options . port , this . opts . host )
190+ }
191+ return Promise . resolve ( )
192+ }
193+
194+ private setupMockService ( ) : void {
195+ logger . info ( `Setting up Pact with Consumer "${ this . opts . consumer } " and Provider "${ this . opts . provider } "
196+ using mock service on Port: "${ this . opts . port } "` ) ;
197+
198+ this . mockService = new MockService ( undefined , undefined , this . opts . port , this . opts . host ,
199+ this . opts . ssl , this . opts . pactfileWriteMode ) ;
200+ }
201+
202+ private startServer ( ) : Promise < PactOptionsComplete > {
203+ return new Promise < PactOptionsComplete > (
204+ ( resolve , reject ) =>
205+ this . server . start ( )
206+ . then (
207+ ( ) => {
208+ this . opts . port = this . server . options . port || this . opts . port
209+ resolve ( this . opts )
210+ } ,
211+ ( e : any ) => reject ( e ) ) )
212+ }
191213}
192214
193215export * from "./messageConsumerPact" ;
0 commit comments