diff --git a/.gitignore b/.gitignore index b947077..5b3ce88 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules/ dist/ +cypress/screenshots diff --git a/cypress/plugins/index.js b/cypress/plugins/index.js index 6d70bd9..48a69fa 100644 --- a/cypress/plugins/index.js +++ b/cypress/plugins/index.js @@ -10,6 +10,7 @@ // 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 @@ -17,10 +18,11 @@ module.exports = (on, 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); +}; diff --git a/lib/index.ts b/lib/index.ts index 49a7732..bd6dabb 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -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 + } + }) }) - }) + } }) }) diff --git a/lib/task.ts b/lib/task.ts new file mode 100644 index 0000000..3cc5c07 --- /dev/null +++ b/lib/task.ts @@ -0,0 +1,8 @@ +import Axios from 'axios' + +export function percyHealthCheck() { + return Axios + .get('http://localhost:5338/percy/healthcheck') + .then(() => true) + .catch(() => false) +} diff --git a/package.json b/package.json index 3bbbd5c..7a57576 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,8 @@ "typescript": "^3.0.3" }, "dependencies": { - "@percy/agent": "~0" + "@percy/agent": "~0", + "axios": "^0.18.1" }, "release": { "plugins": [