Skip to content

Commit d137c17

Browse files
authored
feat: output summary from the plugin (#123)
1 parent 3031efc commit d137c17

File tree

5 files changed

+61
-6
lines changed

5 files changed

+61
-6
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,8 @@ Name | Description
298298

299299
Set environment variable `DEBUG=netlify-plugin-cypress` to see the debug logs. To see even more information, set `DEBUG=netlify-plugin-cypress,netlify-plugin-cypress:verbose`
300300

301+
**Warning:** be careful with verbose logging, since it can print all environment variables passed to the plugin, including tokens, API keys, and other secrets.
302+
301303
## Common problems
302304

303305
<details>

cypress/integration/spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@ it('loads page', () => {
33
cy.visit('/')
44
cy.contains('Placeholder').should('be.visible')
55
})
6+
7+
it.skip('skips this test on purpose', () => {
8+
expect(false).to.be.true
9+
})
10+
11+
it('has not test yet')

package-lock.json

Lines changed: 1 addition & 2 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
],
2121
"license": "ISC",
2222
"dependencies": {
23+
"common-tags": "1.8.0",
2324
"debug": "4.1.1",
2425
"got": "10.7.0",
2526
"local-web-server": "^4.2.1",

src/index.js

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// @ts-check
2+
const { stripIndent } = require('common-tags')
23
const debug = require('debug')('netlify-plugin-cypress')
34
const debugVerbose = require('debug')('netlify-plugin-cypress:verbose')
45
const { ping, getBrowserPath, serveFolder } = require('./utils')
56

7+
const PLUGIN_NAME = 'netlify-plugin-cypress'
68
const DEFAULT_BROWSER = 'electron'
79

810
function 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

234273
const 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

Comments
 (0)