Cypress: Cypress does not fail when cy.route throws new error

Created on 18 Dec 2018  路  4Comments  路  Source: cypress-io/cypress

Current behavior:

Sometimes we want to validate that a certain API request has not been fired, for this scenarios we're adding to the relevant cy.route obj:

onRequest: () => throw new Error('This BE call should have not been triggered!')

When we first introduced it (don't remember which cypress version and chrome version), it failed the test if requested as desired.

However, testing it now with cypress 3.1.0 and chrome 71 does not.

Desired behavior:

Throwing an error in cy.route should fail the cypress test

Steps to reproduce: (app code and test code)

Just add any the above onRequest to a cy.route and see in the console that the error is indeed thrown but that the cypress test doesn't fail.

Versions

Cypress 3.1.0, Chrome 71

pkdriver ready for work unexpected behavior

Most helpful comment

I'm able to reproduce this behavior. When adding a throw into onRequest, the route never resolves.

Example test code

it('throw onRequest', () => {
  cy.visit('https://example.cypress.io/commands/network-requests')
  cy.server()
  cy.route({
    method: 'GET',
    url: 'comments/*',
    onRequest: () => {
      throw new Error('This BE call should have not been triggered!')
    },
  }).as('getComment')
})

screen shot 2019-02-21 at 10 31 35 pm

onRequest function is called here in code:

https://github.com/cypress-io/cypress/blob/develop/packages/driver/src/cy/commands/xhr.coffee#L189

All 4 comments

Any updates on this? I see that it still occurs on 3.1.4

I'm able to reproduce this behavior. When adding a throw into onRequest, the route never resolves.

Example test code

it('throw onRequest', () => {
  cy.visit('https://example.cypress.io/commands/network-requests')
  cy.server()
  cy.route({
    method: 'GET',
    url: 'comments/*',
    onRequest: () => {
      throw new Error('This BE call should have not been triggered!')
    },
  }).as('getComment')
})

screen shot 2019-02-21 at 10 31 35 pm

onRequest function is called here in code:

https://github.com/cypress-io/cypress/blob/develop/packages/driver/src/cy/commands/xhr.coffee#L189

I'm running into this issue too when trying to make sure that some security-sensitive data is never passed around in plaintext.

I've tested this with onResponse too, and it has the same issue.

Are there any other ways of grabbing the request of an XHR that do allow test failure?

Edit: Yes there is. .wait('@route') yields the xhr object and you can fail a test from there. Sadly this is not a great solution for me as it only handles the first response.

cy.wait('@route')
  .then((xhr) => {
    throw new Error('I succesfully fail the test');
  });

BTW, for us the desired result is for the test NOT to fail when throwing in onResponse, as we are testing that our app handles TypeError's graciously...

Was this page helpful?
0 / 5 - 0 ratings