Skip to content

Commit

Permalink
feat: output summary from the plugin (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov authored Feb 10, 2021
1 parent 3031efc commit d137c17
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ Name | Description

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`

**Warning:** be careful with verbose logging, since it can print all environment variables passed to the plugin, including tokens, API keys, and other secrets.

## Common problems

<details>
Expand Down
6 changes: 6 additions & 0 deletions cypress/integration/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ it('loads page', () => {
cy.visit('/')
cy.contains('Placeholder').should('be.visible')
})

it.skip('skips this test on purpose', () => {
expect(false).to.be.true
})

it('has not test yet')
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
],
"license": "ISC",
"dependencies": {
"common-tags": "1.8.0",
"debug": "4.1.1",
"got": "10.7.0",
"local-web-server": "^4.2.1",
Expand Down
55 changes: 51 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// @ts-check
const { stripIndent } = require('common-tags')
const debug = require('debug')('netlify-plugin-cypress')
const debugVerbose = require('debug')('netlify-plugin-cypress:verbose')
const { ping, getBrowserPath, serveFolder } = require('./utils')

const PLUGIN_NAME = 'netlify-plugin-cypress'
const DEFAULT_BROWSER = 'electron'

function startServerMaybe(run, options = {}) {
Expand Down Expand Up @@ -153,13 +155,28 @@ async function cypressInfo(arg) {
}
}

const processCypressResults = (results, errorCallback) => {
/**
* Reports the number of successful and failed tests.
* If there are failed tests, uses the `errorCallback` to
* fail the build step.
* @param {*} results
* @param {function} errorCallback
* @param {function} summaryCallback
*/
const processCypressResults = (results, errorCallback, summaryCallback) => {
if (typeof errorCallback !== 'function') {
debug('Typeof of error callback %s', errorCallback)
throw new Error(
`Expected error callback to be a function, it was ${typeof errorCallback}`,
)
}
if (typeof summaryCallback !== 'function') {
debug('Typeof of summary callback %s', summaryCallback)
throw new Error(
`Expected summary callback to be a function, it was ${typeof summaryCallback}`,
)
}

if (results.failures) {
// Cypress failed without even running the tests
console.error('Problem running Cypress')
Expand All @@ -177,6 +194,27 @@ const processCypressResults = (results, errorCallback) => {
}
})

let text = stripIndent`
✅ Passed tests: ${results.totalPassed}
🔥 Failed tests: ${results.totalFailed}
⭕️ Pending tests: ${results.totalPending}
🚫 Skipped tests: ${results.totalSkipped}
`
if (results.runUrl) {
text += `\n🔗 Dashboard url: ${results.runUrl}`
}
summaryCallback({
title: PLUGIN_NAME,
summary: [
'tests:',
`✅ ${results.totalPassed}`,
`🔥 ${results.totalFailed}`,
`⭕️ ${results.totalPending}`,
`🚫 ${results.totalSkipped}`,
].join(' '),
text,
})

// results.totalFailed gives total number of failed tests
if (results.totalFailed) {
return errorCallback('Failed Cypress tests', {
Expand All @@ -194,6 +232,7 @@ async function postBuild({
spa,
browser,
errorCallback,
summaryCallback,
}) {
const port = 8080
let server
Expand Down Expand Up @@ -228,7 +267,7 @@ async function postBuild({
})
})

processCypressResults(results, errorCallback)
processCypressResults(results, errorCallback, summaryCallback)
}

const hasRecordKey = () => typeof process.env.CYPRESS_RECORD_KEY === 'string'
Expand Down Expand Up @@ -281,8 +320,9 @@ module.exports = {
}

const errorCallback = arg.utils.build.failBuild.bind(arg.utils.build)
const summaryCallback = arg.utils.status.show.bind(arg.utils.status)

processCypressResults(results, errorCallback)
processCypressResults(results, errorCallback, summaryCallback)
},

onPostBuild: async (arg) => {
Expand Down Expand Up @@ -319,6 +359,7 @@ module.exports = {
const spa = arg.inputs.spa

const errorCallback = arg.utils.build.failBuild.bind(arg.utils.build)
const summaryCallback = arg.utils.status.show.bind(arg.utils.status)

await postBuild({
fullPublishFolder,
Expand All @@ -329,9 +370,14 @@ module.exports = {
spa,
browser,
errorCallback,
summaryCallback,
})
},

/**
* Executes after successful Netlify deployment.
* @param {any} arg
*/
onSuccess: async (arg) => {
debugVerbose('onSuccess arg %o', arg)

Expand Down Expand Up @@ -363,6 +409,7 @@ module.exports = {
debug('onSuccessInputs %s %o', typeof onSuccessInputs, onSuccessInputs)

const errorCallback = utils.build.failPlugin.bind(utils.build)
const summaryCallback = utils.status.show.bind(utils.status)

if (!deployPrimeUrl) {
return errorCallback('Missing DEPLOY_PRIME_URL')
Expand Down Expand Up @@ -404,6 +451,6 @@ module.exports = {
tag,
browser,
)
processCypressResults(results, errorCallback)
processCypressResults(results, errorCallback, summaryCallback)
},
}

0 comments on commit d137c17

Please sign in to comment.