question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Expect is failing but test is passing in cy.on confirm & alert message

See original GitHub issue

I wanted to test my content of the confirmation message but it seems that even if my test fails my case passes

it('Should Get Confirm Message', () => {
        cy.get('.button')
            .click();
        cy.on('window:confirm', str => {
            expect(2 + 2).to.equal(5);
        })
    })

Error

The issue seems to be similar to https://github.com/cypress-io/cypress/issues/3497 and https://github.com/cypress-io/cypress/issues/2413

But their solution does not work for me. The issue persists for window:alert

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
dwellecommented, Jan 3, 2020

Here’s my take without using state:

cy.wrap(new Promise((resolve, reject) => {
  cy.on('window:confirm', msg => {
    try {
      // expect(msg).to.eq(confirmMessage); // optional
    } catch ( err ) {
      return reject(err);
    }
    resolve();
  });
  // set a timeout to ensure we don't wait forever
  setTimeout(() => {
    reject(new Error('window.confirm wasn\'t called within 4s'));
  }, 4000);
}), { log: false });
1reaction
bahmutovcommented, Jan 2, 2020

Here is an example waiting for the confirm to have been called before finishing the test (the code calls window.confirm 1 second after the click), see https://github.com/cypress-io/cypress-test-tiny/tree/confirm-after-test

Without waiting

/// <reference types="cypress" />
it('confirms', () => {
  cy.visit('index.html')

  cy.on('window:confirm', (text) => {
    // make the assertion fail on purpose
    // but the test does NOT wait for this assertion to pass
    // By the time the assertion fails, the test has passed
    expect(false).to.be.true
  })

  cy.get('#click').click()
})

after-finish

With waiting

/// <reference types="cypress" />
it('confirms', () => {
  cy.visit('index.html')

  let called
  cy.on('window:confirm', (text) => {
    // make the assertion fail on purpose
    // but the test does NOT wait for this assertion to pass
    // By the time the assertion fails, the test has passed
    expect(false).to.be.true
    called = true
  })

  cy.get('#click').click()
  cy.should(() => {
    expect(called).to.be.true
  })
})

When the test passes (comment out the false assertion, or change it to pass), then all is good

passes

When the assertion is false, the test correctly fails (even if the error message claims the assertion was thrown from the application code)

fails

I think all assertions that pass or fail AFTER the test has finished should have a warning icon ⚠️ next to them in the Command Log. On click, the DevTools should print a message in the console, explaining

This assertion failed / passed AFTER the test has completed.
Please ensure the test waits for every assertion before completion.
See `https://on.cypress.io/assertion-after-test-finished`
Read more comments on GitHub >

github_iconTop Results From Across the Web

Expect is failing but test is passing in cy.on confirm & alert ...
What this means is that the window:confirm may be invoked after the test has already finished (which seems to be your case ---...
Read more >
Testing Browser Alerts, Confirmations, and Prompts with ...
Confirmation : triggers a message dialogue but provides two actions allowing the user to accept or cancel the dialogue, both closing after an ......
Read more >
When Can The Test Stop? - Cypress
A test should not stop until all assertions have passed. We can modify our test to "wait" for the expect to execute, and...
Read more >
Cypress Metaprogramming | Better world by better software
I suggest encoding that the test is expected to fail and the the expected error message in the test title. We have access...
Read more >
How to handle web Alerts in Cypress? How to ... - YouTube
How Cypress handles Alerts in web apps?+ How to verify text message in alert box?+How to automate Confirm pop up through Cypress?
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found