Skip to content

Commit

Permalink
adding support for percyThrowErrorOnFailure , by default it will be f…
Browse files Browse the repository at this point in the history
…alse (#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
  • Loading branch information
prklm10 authored Jun 13, 2024
1 parent 546e7c1 commit 40d02e2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
29 changes: 29 additions & 0 deletions cypress/e2e/index.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});
});
});
Expand Down

0 comments on commit 40d02e2

Please sign in to comment.