@@ -9,11 +9,13 @@ import {
99 replaceInFile ,
1010 writeFile ,
1111} from '../../utils/fs' ;
12+ import { findFreePort } from '../../utils/network' ;
1213import { installPackage } from '../../utils/packages' ;
1314import { ng } from '../../utils/process' ;
1415import { updateJsonFile } from '../../utils/project' ;
15- import { expectToFail } from '../../utils/utils' ;
1616import { readNgVersion } from '../../utils/version' ;
17+ import { Server } from 'http' ;
18+ import { AddressInfo } from 'net' ;
1719
1820// Configurations for each locale.
1921export const baseDir = 'dist/test-project' ;
@@ -67,13 +69,33 @@ export const langTranslations = [
6769] ;
6870export const sourceLocale = langTranslations [ 0 ] . lang ;
6971
70- export const externalServer = ( outputPath : string , baseUrl = '/' ) => {
72+ export interface ExternalServer {
73+ readonly server : Server ;
74+ readonly port : number ;
75+ readonly url : string ;
76+ }
77+
78+ /**
79+ * Create an `express` `http.Server` listening on a random port.
80+ *
81+ * Call .close() on the server return value to close the server.
82+ */
83+ export async function externalServer ( outputPath : string , baseUrl = '/' ) : Promise < ExternalServer > {
7184 const app = express ( ) ;
7285 app . use ( baseUrl , express . static ( resolve ( outputPath ) ) ) ;
7386
74- // call .close() on the return value to close the server.
75- return app . listen ( 4200 , 'localhost' ) ;
76- } ;
87+ return new Promise ( ( resolve ) => {
88+ const server = app . listen ( 0 , 'localhost' , ( ) => {
89+ const { port } = server . address ( ) as AddressInfo ;
90+
91+ resolve ( {
92+ server,
93+ port,
94+ url : `http://localhost:${ port } ${ baseUrl } ` ,
95+ } ) ;
96+ } ) ;
97+ } ) ;
98+ }
7799
78100export const formats = {
79101 'xlf' : {
0 commit comments