@@ -27,6 +27,51 @@ async function runCypressTests (baseUrl, record, spec) {
2727 } )
2828}
2929
30+ async function postBuild ( { fullPublishFolder, record, spec, failBuild } ) {
31+ const port = 8080
32+ const server = serveFolder ( fullPublishFolder , port )
33+ debug ( 'local server listening on port %d' , port )
34+
35+ const baseUrl = `http://localhost:${ port } `
36+
37+ const results = await runCypressTests ( baseUrl , record , spec )
38+
39+ await new Promise ( ( resolve , reject ) => {
40+ server . close ( err => {
41+ if ( err ) {
42+ return reject ( err )
43+ }
44+ debug ( 'closed local server on port %d' , port )
45+ resolve ( )
46+ } )
47+ } )
48+
49+ // seems Cypress TS definition does not have "failures" and "message" properties
50+ if ( results . failures ) {
51+ // Cypress failed without even running the tests
52+ console . error ( 'Problem running Cypress' )
53+ console . error ( results . message )
54+
55+ return failBuild ( 'Problem running Cypress' , {
56+ error : new Error ( results . message )
57+ } )
58+ }
59+
60+ debug ( 'Cypress run results' )
61+ Object . keys ( results ) . forEach ( key => {
62+ if ( key . startsWith ( 'total' ) ) {
63+ debug ( '%s:' , key , results [ key ] )
64+ }
65+ } )
66+
67+ // results.totalFailed gives total number of failed tests
68+ if ( results . totalFailed ) {
69+ return failBuild ( 'Failed Cypress tests' , {
70+ error : new Error ( `${ results . totalFailed } test(s) failed` )
71+ } )
72+ }
73+ }
74+
3075module . exports = function cypressPlugin ( pluginConfig ) {
3176 debugVerbose ( 'cypressPlugin config %o' , pluginConfig )
3277
@@ -38,60 +83,26 @@ module.exports = function cypressPlugin (pluginConfig) {
3883 const fullPublishFolder = arg . netlifyConfig . build . publish
3984 debug ( 'folder to publish is "%s"' , fullPublishFolder )
4085
41- const port = 8080
42- const server = serveFolder ( fullPublishFolder , port )
43- debug ( 'local server listening on port %d' , port )
44-
4586 // only if the user wants to record the tests and has set the record key
4687 // then we should attempt recording
4788 const record =
4889 typeof process . env . CYPRESS_RECORD_KEY === 'string' &&
4990 Boolean ( pluginConfig . record )
50- const baseUrl = `http://localhost:${ port } `
51- const spec = pluginConfig . spec
52-
53- const results = await runCypressTests ( baseUrl , record , spec )
5491
55- await new Promise ( ( resolve , reject ) => {
56- server . close ( err => {
57- if ( err ) {
58- return reject ( err )
59- }
60- debug ( 'closed local server on port %d' , port )
61- resolve ( )
62- } )
63- } )
92+ const spec = pluginConfig . spec
6493
6594 const exitWithError = ( message , info ) => {
6695 console . error ( 'Exit with error: %s' , message )
6796 throw info . error
6897 }
6998 const failBuild = arg . utils && arg . utils . build && arg . utils . build . failBuild || exitWithError
7099
71- // seems Cypress TS definition does not have "failures" and "message" properties
72- if ( results . failures ) {
73- // Cypress failed without even running the tests
74- console . error ( 'Problem running Cypress' )
75- console . error ( results . message )
76-
77- return failBuild ( 'Problem running Cypress' , {
78- error : new Error ( results . message )
79- } )
80- }
81-
82- debug ( 'Cypress run results' )
83- Object . keys ( results ) . forEach ( key => {
84- if ( key . startsWith ( 'total' ) ) {
85- debug ( '%s:' , key , results [ key ] )
86- }
100+ return postBuild ( {
101+ fullPublishFolder,
102+ record,
103+ spec,
104+ failBuild
87105 } )
88-
89- // results.totalFailed gives total number of failed tests
90- if ( results . totalFailed ) {
91- return failBuild ( 'Failed Cypress tests' , {
92- error : new Error ( `${ results . totalFailed } test(s) failed` )
93- } )
94- }
95106 }
96107 }
97108}
0 commit comments