@@ -13,6 +13,26 @@ function serveFolder (folder, port) {
13
13
return http . createServer ( server ) . listen ( port )
14
14
}
15
15
16
+ function startServerMaybe ( options = { } ) {
17
+ const startCommand = options . start
18
+ if ( ! startCommand ) {
19
+ debug ( 'No start command found' )
20
+ return
21
+ }
22
+
23
+ const serverProcess = execa ( startCommand , {
24
+ stdio : 'inherit' ,
25
+ detached : true ,
26
+ shell : true
27
+ } )
28
+
29
+ debug ( 'detached the process and returning stop function' )
30
+ return ( ) => {
31
+ debug ( 'stopping server process' )
32
+ serverProcess . kill ( )
33
+ }
34
+ }
35
+
16
36
async function waitOnMaybe ( options = { } ) {
17
37
const waitOnUrl = options [ 'wait-on' ]
18
38
if ( ! waitOnUrl ) {
@@ -32,6 +52,7 @@ async function waitOnMaybe (options = {}) {
32
52
33
53
try {
34
54
await ping ( waitOnUrl , waitTimeoutMs )
55
+ debug ( 'url %s responds' , waitOnUrl )
35
56
} catch ( err ) {
36
57
debug ( 'pinging %s for %d ms failed' , waitOnUrl , waitTimeoutMs )
37
58
debug ( err )
@@ -44,7 +65,7 @@ async function runCypressTests (baseUrl, record, spec) {
44
65
// https://on.cypress.io/module-api
45
66
const cypress = require ( 'cypress' )
46
67
47
- console . log ( 'running Cypress against url %s recording? ', baseUrl , record )
68
+ debug ( 'run cypress params %o ', { baseUrl, record, spec } )
48
69
49
70
return await cypress . run ( {
50
71
config : {
@@ -64,26 +85,7 @@ async function onInit() {
64
85
}
65
86
}
66
87
67
- async function postBuild ( { fullPublishFolder, record, spec, failBuild } ) {
68
- const port = 8080
69
- const server = serveFolder ( fullPublishFolder , port )
70
- debug ( 'local server listening on port %d' , port )
71
-
72
- const baseUrl = `http://localhost:${ port } `
73
-
74
- const results = await runCypressTests ( baseUrl , record , spec )
75
-
76
- await new Promise ( ( resolve , reject ) => {
77
- server . close ( err => {
78
- if ( err ) {
79
- return reject ( err )
80
- }
81
- debug ( 'closed local server on port %d' , port )
82
- resolve ( )
83
- } )
84
- } )
85
-
86
- // seems Cypress TS definition does not have "failures" and "message" properties
88
+ const processCypressResults = ( results , failBuild ) => {
87
89
if ( results . failures ) {
88
90
// Cypress failed without even running the tests
89
91
console . error ( 'Problem running Cypress' )
@@ -109,6 +111,35 @@ async function postBuild({ fullPublishFolder, record, spec, failBuild }) {
109
111
}
110
112
}
111
113
114
+ async function postBuild ( { fullPublishFolder, record, spec, failBuild } ) {
115
+ const port = 8080
116
+ const server = serveFolder ( fullPublishFolder , port )
117
+ debug ( 'local server listening on port %d' , port )
118
+
119
+ const baseUrl = `http://localhost:${ port } `
120
+
121
+ const results = await runCypressTests ( baseUrl , record , spec )
122
+
123
+ await new Promise ( ( resolve , reject ) => {
124
+ server . close ( err => {
125
+ if ( err ) {
126
+ return reject ( err )
127
+ }
128
+ debug ( 'closed local server on port %d' , port )
129
+ resolve ( )
130
+ } )
131
+ } )
132
+
133
+ processCypressResults ( results , failBuild )
134
+ }
135
+
136
+ const throwAnError = ( message , info ) => {
137
+ console . error ( 'Exit with error: %s' , message )
138
+ throw info . error
139
+ }
140
+
141
+ const hasRecordKey = ( ) => typeof process . env . CYPRESS_RECORD_KEY === 'string'
142
+
112
143
module . exports = function cypressPlugin ( pluginConfig ) {
113
144
debugVerbose ( 'cypressPlugin config %o' , pluginConfig )
114
145
@@ -122,10 +153,26 @@ module.exports = function cypressPlugin (pluginConfig) {
122
153
debug ( 'cypress plugin preBuild inputs %o' , arg . inputs )
123
154
const preBuildInputs = arg . inputs && arg . inputs . preBuild
124
155
if ( ! preBuildInputs ) {
156
+ debug ( 'there are no preBuild inputs' )
125
157
return
126
158
}
127
159
160
+ const closeServer = startServerMaybe ( preBuildInputs )
128
161
await waitOnMaybe ( preBuildInputs )
162
+
163
+ const baseUrl = preBuildInputs [ 'wait-on' ]
164
+ const record = Boolean ( preBuildInputs . record )
165
+ const spec = preBuildInputs . spec
166
+ const results = await runCypressTests ( baseUrl , record , spec )
167
+
168
+ if ( closeServer ) {
169
+ debug ( 'closing server' )
170
+ closeServer ( )
171
+ }
172
+
173
+ const failBuild = arg . utils && arg . utils . build && arg . utils . build . failBuild || throwAnError
174
+
175
+ processCypressResults ( results , failBuild )
129
176
} ,
130
177
131
178
postBuild : async ( arg ) => {
@@ -137,17 +184,11 @@ module.exports = function cypressPlugin (pluginConfig) {
137
184
138
185
// only if the user wants to record the tests and has set the record key
139
186
// then we should attempt recording
140
- const record =
141
- typeof process . env . CYPRESS_RECORD_KEY === 'string' &&
142
- Boolean ( pluginConfig . record )
187
+ const record = hasRecordKey ( ) && Boolean ( pluginConfig . record )
143
188
144
189
const spec = pluginConfig . spec
145
190
146
- const exitWithError = ( message , info ) => {
147
- console . error ( 'Exit with error: %s' , message )
148
- throw info . error
149
- }
150
- const failBuild = arg . utils && arg . utils . build && arg . utils . build . failBuild || exitWithError
191
+ const failBuild = arg . utils && arg . utils . build && arg . utils . build . failBuild || throwAnError
151
192
152
193
return postBuild ( {
153
194
fullPublishFolder,
0 commit comments