File tree Expand file tree Collapse file tree 2 files changed +30
-2
lines changed Expand file tree Collapse file tree 2 files changed +30
-2
lines changed Original file line number Diff line number Diff line change @@ -19,12 +19,14 @@ export class EnvParser {
19
19
20
20
private parser = new Parser ( false )
21
21
22
+ constructor ( private envFileName : string = '.env' ) { }
23
+
22
24
/**
23
25
* Parse .env file contents
24
26
*/
25
27
public async parse ( rootDir : string ) {
26
28
try {
27
- this . envContents = this . parser . parse ( await readFile ( join ( rootDir , '.env' ) , 'utf-8' ) )
29
+ this . envContents = this . parser . parse ( await readFile ( join ( rootDir , this . envFileName ) , 'utf-8' ) )
28
30
} catch { }
29
31
}
30
32
Original file line number Diff line number Diff line change @@ -19,6 +19,8 @@ import { TestProcess } from './process'
19
19
import { JapaFlags } from '../Contracts'
20
20
21
21
import { ENV_FILES , TESTS_ENTRY_FILE } from '../../config/paths'
22
+ import { EnvParser } from '../EnvParser'
23
+ import getPort from 'get-port'
22
24
23
25
/**
24
26
* Exposes the API to watch project for compilition changes and
@@ -115,6 +117,30 @@ export class TestsServer {
115
117
process . exit ( )
116
118
}
117
119
120
+ /**
121
+ * Returns the HOST and the PORT environment variables
122
+ * for the HTTP server
123
+ */
124
+ private async getEnvironmentVariables ( ) {
125
+ const envParser = new EnvParser ( '.env.test' )
126
+ await envParser . parse ( this . appRoot )
127
+
128
+ const envOptions = envParser . asEnvObject ( [ 'PORT' , 'TZ' , 'HOST' ] )
129
+ const HOST = process . env . HOST || envOptions . HOST || '0.0.0.0'
130
+ let PORT = Number ( process . env . PORT || envOptions . PORT )
131
+
132
+ /**
133
+ * Use the port defined inside ".env.test" file or use
134
+ * a random port
135
+ */
136
+ PORT = await getPort ( {
137
+ port : ! isNaN ( PORT ) ? [ PORT ] : [ ] ,
138
+ host : HOST ,
139
+ } )
140
+
141
+ return { HOST , PORT : String ( PORT ) }
142
+ }
143
+
118
144
/**
119
145
* Run tests. Use [[watch]] to also watch for file
120
146
* changes
@@ -141,7 +167,7 @@ export class TestsServer {
141
167
filters ,
142
168
this . nodeArgs ,
143
169
this . logger ,
144
- { }
170
+ await this . getEnvironmentVariables ( )
145
171
) . run ( )
146
172
147
173
this . busy = false
You can’t perform that action at this time.
0 commit comments