From 40d02e24d30dce7e16caa2b50d92ad773010edfa Mon Sep 17 00:00:00 2001 From: Pradum Kumar Date: Thu, 13 Jun 2024 18:47:44 +0530 Subject: [PATCH] adding support for percyThrowErrorOnFailure , by default it will be false (#803) * adding support for throwErrorOnFailure, by default it will be false * update readme and test fux * test fix * test fix * test fix * ignoring the thowing of error * ignoring throwing error from cypress * adding PERCY_THROW_ERROR_ON_FAILURE variable as a global on cypress env * adding PERCY_THROW_ERROR_ON_FAILURE on config level * adding test on failure --- README.md | 8 ++++++++ cypress/e2e/index.cy.js | 29 +++++++++++++++++++++++++++++ index.js | 3 +++ 3 files changed, 40 insertions(+) diff --git a/README.md b/README.md index 42d42e0..4dee693 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,14 @@ $ percy exec -- cypress run - `name` - The snapshot name; must be unique to each snapshot; defaults to the full test title - `options` - [See per-snapshot configuration options](https://docs.percy.io/docs/cli-configuration#per-snapshot-configuration) +## Cypress Config +- `percyThrowErrorOnFailure` - If set to true, it will throw an error when one is encountered. By default, it is set to false, and errors are suppressed. +If you are using uncaught exeception then test test will pass. +``` +cy.on("uncaught:exception", (e, runnable) => {}); + +``` + ## Upgrading ### Automatically with `@percy/migrate` diff --git a/cypress/e2e/index.cy.js b/cypress/e2e/index.cy.js index f7099e3..f5d619c 100644 --- a/cypress/e2e/index.cy.js +++ b/cypress/e2e/index.cy.js @@ -42,6 +42,35 @@ describe('percySnapshot', () => { ]); }); + describe('if percyThrowErrorOnFailure set to true', () => { + let ogPercyThrowErrorOnFailure; + + beforeEach(() => { + ogPercyThrowErrorOnFailure = Cypress.config('percyThrowErrorOnFailure'); + }); + + afterEach(() => { + Cypress.config().percyThrowErrorOnFailure = ogPercyThrowErrorOnFailure; + }); + + it('disables snapshots and fails the test', () => { + cy.on('fail', (_err, runnable) => { + // it only runs when test fails. + // This will supress failure and pass the test. + return false; + }); + + Cypress.config().percyThrowErrorOnFailure = true; + cy.then(() => helpers.test('error', '/percy/snapshot')); + + cy.percySnapshot(); + + cy.then(() => helpers.logger.stderr).should('include.members', [ + '[percy] Could not take DOM snapshot "percySnapshot handles snapshot failures"' + ]); + }); + }); + describe('in interactive mode', () => { let ogInteractive; diff --git a/index.js b/index.js index ef870fb..0652b61 100644 --- a/index.js +++ b/index.js @@ -73,6 +73,9 @@ Cypress.Commands.add('percySnapshot', (name, options) => { // Handle errors log.error(`Could not take DOM snapshot "${name}"`); log.error(error); + if (Cypress.config('percyThrowErrorOnFailure')) { + throw error; + } }); }); });