Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypress: how to mark a single test as failed but continue to run other tests?

Currently when a single test in it() block fails Cypress halts completely.

I want Cypress to continue running subsequent assertions within the test, regardless if a previous assertion failed or not (but I still want to mark the failed tests so I know which one failed).

I tried to intercept the fail event in beforeEach:

beforeEach(() => {
        Cypress.on('fail', (error, runnable) => {
            cy.log('this single test failed, but continue other tests');
            // don't stop!
            // throw error; // marks test as failed but also makes Cypress stop
        });

But it appears I cannot use any cy commands inside this handler because when I do it returns an error due to Cypress weird internal promise logic:

CypressError: Cypress detected that you returned a promise from a command while also invoking one or more cy commands in that promise.

The command that returned the promise was:

cy.wait()

The cy command you invoked inside the promise was:

cy.log()

Because Cypress commands are already promise-like, you don't need to wrap them or return your own promise.

Cypress will resolve your command with whatever the final Cypress command yields.

The reason this is an error instead of a warning is because Cypress internally queues commands serially whereas Promises execute as soon as they are invoked. Attempting to reconcile this would prevent Cypress from ever resolving.

https://on.cypress.io/returning-promise-and-commands-in-another-command

If I leave the Cypress.on('fail') block empty all tests are going to be marked as passed even if they fail.

If I uncomment throw error Cypress will halt completely on any failed test.

like image 700
van_folmert Avatar asked Jan 18 '26 19:01

van_folmert


1 Answers

My way to ensure subsequent tests are done, and the failed test is marked as failure is - to put every it case in different file, and if needed to group them - I group them in a separate subfolder.

It has improved readability of reports and cypress tests run time, since before that I sometimes had problems with cypress not clearing its state between tests and we had memory leaks.

like image 165
Rosen Mihaylov Avatar answered Jan 21 '26 08:01

Rosen Mihaylov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!