@@ -13,6 +13,26 @@ function serveFolder (folder, port) {
1313 return http . createServer ( server ) . listen ( port )
1414}
1515
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+
1636async function waitOnMaybe ( options = { } ) {
1737 const waitOnUrl = options [ 'wait-on' ]
1838 if ( ! waitOnUrl ) {
@@ -32,6 +52,7 @@ async function waitOnMaybe (options = {}) {
3252
3353 try {
3454 await ping ( waitOnUrl , waitTimeoutMs )
55+ debug ( 'url %s responds' , waitOnUrl )
3556 } catch ( err ) {
3657 debug ( 'pinging %s for %d ms failed' , waitOnUrl , waitTimeoutMs )
3758 debug ( err )
@@ -44,7 +65,7 @@ async function runCypressTests (baseUrl, record, spec) {
4465 // https://on.cypress.io/module-api
4566 const cypress = require ( 'cypress' )
4667
47- console . log ( 'running Cypress against url %s recording? ', baseUrl , record )
68+ debug ( 'run cypress params %o ', { baseUrl, record, spec } )
4869
4970 return await cypress . run ( {
5071 config : {
@@ -64,26 +85,7 @@ async function onInit() {
6485 }
6586}
6687
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 ) => {
8789 if ( results . failures ) {
8890 // Cypress failed without even running the tests
8991 console . error ( 'Problem running Cypress' )
@@ -109,6 +111,35 @@ async function postBuild({ fullPublishFolder, record, spec, failBuild }) {
109111 }
110112}
111113
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+
112143module . exports = function cypressPlugin ( pluginConfig ) {
113144 debugVerbose ( 'cypressPlugin config %o' , pluginConfig )
114145
@@ -122,10 +153,26 @@ module.exports = function cypressPlugin (pluginConfig) {
122153 debug ( 'cypress plugin preBuild inputs %o' , arg . inputs )
123154 const preBuildInputs = arg . inputs && arg . inputs . preBuild
124155 if ( ! preBuildInputs ) {
156+ debug ( 'there are no preBuild inputs' )
125157 return
126158 }
127159
160+ const closeServer = startServerMaybe ( preBuildInputs )
128161 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 )
129176 } ,
130177
131178 postBuild : async ( arg ) => {
@@ -137,17 +184,11 @@ module.exports = function cypressPlugin (pluginConfig) {
137184
138185 // only if the user wants to record the tests and has set the record key
139186 // 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 )
143188
144189 const spec = pluginConfig . spec
145190
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
151192
152193 return postBuild ( {
153194 fullPublishFolder,
0 commit comments