Skip to content

Commit 55a5801

Browse files
authored
fix: improve error handling (#25)
correctly stop the build if the tests fail or there is an error - closes #20
1 parent bad64b6 commit 55a5801

File tree

3 files changed

+16
-46
lines changed

3 files changed

+16
-46
lines changed

package-lock.json

Lines changed: 4 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,9 @@
2020
],
2121
"license": "ISC",
2222
"dependencies": {
23-
"argument-vector": "1.0.2",
24-
"check-more-types": "2.24.0",
2523
"debug": "4.1.1",
2624
"ecstatic": "4.1.2",
27-
"got": "9.6.0",
28-
"lazy-ass": "1.6.0"
25+
"got": "9.6.0"
2926
},
3027
"repository": {
3128
"type": "git",

src/index.js

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ const ecstatic = require('ecstatic')
33
const http = require('http')
44
const debug = require('debug')('netlify-plugin-cypress')
55
const debugVerbose = require('debug')('netlify-plugin-cypress:verbose')
6-
const la = require('lazy-ass')
7-
const is = require('check-more-types')
86
const { ping } = require('./utils')
97

108
function serveFolder (folder, port) {
@@ -33,7 +31,7 @@ function startServerMaybe (run, options = {}) {
3331
}
3432
}
3533

36-
async function waitOnMaybe (failPlugin, options = {}) {
34+
async function waitOnMaybe (buildUtils, options = {}) {
3735
const waitOnUrl = options['wait-on']
3836
if (!waitOnUrl) {
3937
debug('no wait-on defined')
@@ -56,7 +54,7 @@ async function waitOnMaybe (failPlugin, options = {}) {
5654
} catch (err) {
5755
debug('pinging %s for %d ms failed', waitOnUrl, waitTimeoutMs)
5856
debug(err)
59-
failPlugin(`Pinging ${waitOnUrl} for ${waitTimeoutMs} failed`, { error: err })
57+
return buildUtils.failBuild(`Pinging ${waitOnUrl} for ${waitTimeoutMs} failed`, { error: err })
6058
}
6159
}
6260

@@ -95,13 +93,13 @@ async function onInit(arg) {
9593
await arg.utils.run('cypress', ['install'], runOptions)
9694
}
9795

98-
const processCypressResults = (results, failPlugin) => {
96+
const processCypressResults = (results, buildUtils) => {
9997
if (results.failures) {
10098
// Cypress failed without even running the tests
10199
console.error('Problem running Cypress')
102100
console.error(results.message)
103101

104-
return failPlugin('Problem running Cypress', {
102+
return buildUtils.failPlugin('Problem running Cypress', {
105103
error: new Error(results.message)
106104
})
107105
}
@@ -115,13 +113,13 @@ const processCypressResults = (results, failPlugin) => {
115113

116114
// results.totalFailed gives total number of failed tests
117115
if (results.totalFailed) {
118-
return failPlugin('Failed Cypress tests', {
116+
return buildUtils.failBuild('Failed Cypress tests', {
119117
error: new Error(`${results.totalFailed} test(s) failed`)
120118
})
121119
}
122120
}
123121

124-
async function postBuild({ fullPublishFolder, record, spec, group, tag, failPlugin }) {
122+
async function postBuild({ fullPublishFolder, record, spec, group, tag, buildUtils }) {
125123
const port = 8080
126124
const server = serveFolder(fullPublishFolder, port)
127125
debug('local server listening on port %d', port)
@@ -140,7 +138,7 @@ async function postBuild({ fullPublishFolder, record, spec, group, tag, failPlug
140138
})
141139
})
142140

143-
processCypressResults(results, failPlugin)
141+
processCypressResults(results, buildUtils)
144142
}
145143

146144
const hasRecordKey = () => typeof process.env.CYPRESS_RECORD_KEY === 'string'
@@ -155,11 +153,8 @@ module.exports = {
155153
return
156154
}
157155

158-
const failPlugin = arg.utils && arg.utils.build && arg.utils.build.failPlugin
159-
la(is.fn(failPlugin), 'expected failPlugin function inside', arg.utils)
160-
161156
const closeServer = startServerMaybe(arg.utils.run, preBuildInputs)
162-
await waitOnMaybe(failPlugin, preBuildInputs)
157+
await waitOnMaybe(arg.utils.build, preBuildInputs)
163158

164159
const baseUrl = preBuildInputs['wait-on']
165160
const record = hasRecordKey() && Boolean(preBuildInputs.record)
@@ -183,7 +178,7 @@ module.exports = {
183178
closeServer()
184179
}
185180

186-
processCypressResults(results, failPlugin)
181+
processCypressResults(results, arg.utils.build)
187182
},
188183

189184
onPostBuild: async (arg) => {
@@ -210,16 +205,15 @@ module.exports = {
210205
}
211206
}
212207

213-
const failPlugin = arg.utils && arg.utils.build && arg.utils.build.failPlugin
214-
la(is.fn(failPlugin), 'expected failPlugin function inside', arg.utils)
208+
const buildUtils = arg.utils.build
215209

216210
await postBuild({
217211
fullPublishFolder,
218212
record,
219213
spec,
220214
group,
221215
tag,
222-
failPlugin
216+
buildUtils,
223217
})
224218
}
225219
}

0 commit comments

Comments
 (0)