Skip to content

Commit

Permalink
BREAKING CHANGE: Use cy.task for health check rather than cy.exec
Browse files Browse the repository at this point in the history
This will fix #104 (and hopefully #138).

`cy.task` will always execute within the version of node that is bundled with
Cypress, so we no longer have to worry about `$PATH` issues that `cy.exec`
faces. We're also no longer running a CLI command for the health check, this new
implementation will make a HTTP request from node.

This does mean we need to update the docs to let users know there's an extra
step to configure the SDK properly now.

In an ideal world we would be able to `.catch` on `cy.request` and disable Percy
after the first failed `POST` of the DOM. I think in the future we should look
into working with Cypress to implement this so we can shrink the integration,
make the SDK more reliable, and faster (since we won't make 2 network requests
per-snapshot).
  • Loading branch information
Robdel12 committed Aug 2, 2019
1 parent 19d7b77 commit 83a8591
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
dist/
cypress/screenshots
14 changes: 8 additions & 6 deletions cypress/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@

// This function is called when a project is opened or re-opened (e.g. due to
// the project's config changing)
const percyHealthCheck = require("../../dist/task");

module.exports = (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config

// Make it possible to log things to stdout by calling 'cy.task('log', 'some message to log').
// Useful for development and debugging.
on('task', {
log (message) {
console.log(message)
return null
on("task", {
log(message) {
console.log(message);
return null;
}
})
}
});
on("task", percyHealthCheck);
};
60 changes: 25 additions & 35 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,33 @@ declare const Cypress: any
declare const cy: any

Cypress.Commands.add('percySnapshot', (name: string, options: any = {}) => {
const percyAgentClient = new PercyAgent({
handleAgentCommunication: false,
domTransformation: options.domTransformation
})

// Use cy.exec(...) to check if percy agent is running. Ideally this would be
// done using something like cy.request(...), but that's not currently possible,
// for details, see: https://github.com/cypress-io/cypress/issues/3161
const healthcheckCmd = `percy health-check -p ${percyAgentClient.port}`
cy.exec(healthcheckCmd, { failOnNonZeroExit: false }).then(({ stderr }: any) => {
if (stderr) {
// Percy server not available
cy.log('[percy] Percy agent is not running. Skipping snapshots')
cy.log(stderr)

return
}
cy.task('percyHealthCheck').then((percyIsRunning: boolean) => {
if (percyIsRunning) {
const percyAgentClient = new PercyAgent({
handleAgentCommunication: false,
domTransformation: options.domTransformation
})

name = name || cy.state('runnable').fullTitle()
name = name || cy.state('runnable').fullTitle()

cy.document().then((doc: Document) => {
options.document = doc
const domSnapshot = percyAgentClient.snapshot(name, options)
return cy.request({
method: 'POST',
url: `http://localhost:${percyAgentClient.port}/percy/snapshot`,
failOnStatusCode: false,
body: {
name,
url: doc.URL,
enableJavaScript: options.enableJavaScript,
widths: options.widths,
clientInfo: clientInfo(),
environmentInfo: environmentInfo(),
domSnapshot
}
cy.document().then((doc: Document) => {
options.document = doc
const domSnapshot = percyAgentClient.snapshot(name, options)
return cy.request({
method: 'POST',
url: `http://localhost:${percyAgentClient.port}/percy/snapshot`,
failOnStatusCode: false,
body: {
name,
url: doc.URL,
enableJavaScript: options.enableJavaScript,
widths: options.widths,
clientInfo: clientInfo(),
environmentInfo: environmentInfo(),
domSnapshot
}
})
})
})
}
})
})
8 changes: 8 additions & 0 deletions lib/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Axios from 'axios'

export function percyHealthCheck() {
return Axios
.get('http://localhost:5338/percy/healthcheck')
.then(() => true)
.catch(() => false)
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"typescript": "^3.0.3"
},
"dependencies": {
"@percy/agent": "~0"
"@percy/agent": "~0",
"axios": "^0.18.1"
},
"release": {
"plugins": [
Expand Down

0 comments on commit 83a8591

Please sign in to comment.