11// @ts -check
2+ const { stripIndent } = require ( 'common-tags' )
23const debug = require ( 'debug' ) ( 'netlify-plugin-cypress' )
34const debugVerbose = require ( 'debug' ) ( 'netlify-plugin-cypress:verbose' )
45const { ping, getBrowserPath, serveFolder } = require ( './utils' )
56
7+ const PLUGIN_NAME = 'netlify-plugin-cypress'
68const DEFAULT_BROWSER = 'electron'
79
810function startServerMaybe ( run , options = { } ) {
@@ -153,13 +155,28 @@ async function cypressInfo(arg) {
153155 }
154156}
155157
156- const processCypressResults = ( results , errorCallback ) => {
158+ /**
159+ * Reports the number of successful and failed tests.
160+ * If there are failed tests, uses the `errorCallback` to
161+ * fail the build step.
162+ * @param {* } results
163+ * @param {function } errorCallback
164+ * @param {function } summaryCallback
165+ */
166+ const processCypressResults = ( results , errorCallback , summaryCallback ) => {
157167 if ( typeof errorCallback !== 'function' ) {
158168 debug ( 'Typeof of error callback %s' , errorCallback )
159169 throw new Error (
160170 `Expected error callback to be a function, it was ${ typeof errorCallback } ` ,
161171 )
162172 }
173+ if ( typeof summaryCallback !== 'function' ) {
174+ debug ( 'Typeof of summary callback %s' , summaryCallback )
175+ throw new Error (
176+ `Expected summary callback to be a function, it was ${ typeof summaryCallback } ` ,
177+ )
178+ }
179+
163180 if ( results . failures ) {
164181 // Cypress failed without even running the tests
165182 console . error ( 'Problem running Cypress' )
@@ -177,6 +194,27 @@ const processCypressResults = (results, errorCallback) => {
177194 }
178195 } )
179196
197+ let text = stripIndent `
198+ ✅ Passed tests: ${ results . totalPassed }
199+ 🔥 Failed tests: ${ results . totalFailed }
200+ ⭕️ Pending tests: ${ results . totalPending }
201+ 🚫 Skipped tests: ${ results . totalSkipped }
202+ `
203+ if ( results . runUrl ) {
204+ text += `\n🔗 Dashboard url: ${ results . runUrl } `
205+ }
206+ summaryCallback ( {
207+ title : PLUGIN_NAME ,
208+ summary : [
209+ 'tests:' ,
210+ `✅ ${ results . totalPassed } ` ,
211+ `🔥 ${ results . totalFailed } ` ,
212+ `⭕️ ${ results . totalPending } ` ,
213+ `🚫 ${ results . totalSkipped } ` ,
214+ ] . join ( ' ' ) ,
215+ text,
216+ } )
217+
180218 // results.totalFailed gives total number of failed tests
181219 if ( results . totalFailed ) {
182220 return errorCallback ( 'Failed Cypress tests' , {
@@ -194,6 +232,7 @@ async function postBuild({
194232 spa,
195233 browser,
196234 errorCallback,
235+ summaryCallback,
197236} ) {
198237 const port = 8080
199238 let server
@@ -228,7 +267,7 @@ async function postBuild({
228267 } )
229268 } )
230269
231- processCypressResults ( results , errorCallback )
270+ processCypressResults ( results , errorCallback , summaryCallback )
232271}
233272
234273const hasRecordKey = ( ) => typeof process . env . CYPRESS_RECORD_KEY === 'string'
@@ -281,8 +320,9 @@ module.exports = {
281320 }
282321
283322 const errorCallback = arg . utils . build . failBuild . bind ( arg . utils . build )
323+ const summaryCallback = arg . utils . status . show . bind ( arg . utils . status )
284324
285- processCypressResults ( results , errorCallback )
325+ processCypressResults ( results , errorCallback , summaryCallback )
286326 } ,
287327
288328 onPostBuild : async ( arg ) => {
@@ -319,6 +359,7 @@ module.exports = {
319359 const spa = arg . inputs . spa
320360
321361 const errorCallback = arg . utils . build . failBuild . bind ( arg . utils . build )
362+ const summaryCallback = arg . utils . status . show . bind ( arg . utils . status )
322363
323364 await postBuild ( {
324365 fullPublishFolder,
@@ -329,9 +370,14 @@ module.exports = {
329370 spa,
330371 browser,
331372 errorCallback,
373+ summaryCallback,
332374 } )
333375 } ,
334376
377+ /**
378+ * Executes after successful Netlify deployment.
379+ * @param {any } arg
380+ */
335381 onSuccess : async ( arg ) => {
336382 debugVerbose ( 'onSuccess arg %o' , arg )
337383
@@ -363,6 +409,7 @@ module.exports = {
363409 debug ( 'onSuccessInputs %s %o' , typeof onSuccessInputs , onSuccessInputs )
364410
365411 const errorCallback = utils . build . failPlugin . bind ( utils . build )
412+ const summaryCallback = utils . status . show . bind ( utils . status )
366413
367414 if ( ! deployPrimeUrl ) {
368415 return errorCallback ( 'Missing DEPLOY_PRIME_URL' )
@@ -404,6 +451,6 @@ module.exports = {
404451 tag ,
405452 browser ,
406453 )
407- processCypressResults ( results , errorCallback )
454+ processCypressResults ( results , errorCallback , summaryCallback )
408455 } ,
409456}
0 commit comments