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.

Flakey AJAX requests when using Cypress.on('fail') and the test runner

See original GitHub issue

Current behavior:

When using the Cypress.on('fail', function(err, runnable) {}); method I am posting an AJAX request to our application like so:

    Cypress.on('fail', function(err, runnable) {
      // we now have access to the err instance
      // and runnable this failed on
        setTimeout(function() {
          axios.post('https://site.com', {
            error_message: runnable.err.message,
          })
          .then(function (res) {
            console.log(res);
          })
          .catch(function (error) {
            console.log(error);
          });
        }, 0);
        throw err;
    });

Not sure why but runnable won’t work unless I wrap it in a function or timeout, but that is another issue so don’t get confused by it.

When using npx cypress open and then selecting my spec the AJAX request sends every time without any issues.

However if I run my tests using npx cypress run on my CircleCI job the AJAX request is very flakey. Sometimes it sends and sometimes it doesn’t. I’m guessing this is because the test runner is killing the spec before the AJAX request can be sent.

Desired behavior:

I’d like to know how we can stop the spec from being killed until the request has been successfully sent. I’ve already tried using cy.wait but this doesn’t work.

Is there a way that I can manually fail a test in the promise on my AJAX request?

Steps to reproduce:

Run an AJAX request inside of Cypress.on('fail') and then use the npx cypress run command. Alternatively any logic that can take a bit of time within here such as a setTimeout can’t be ran.

Versions (2.1.0, OSX, Chrome)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
nickclickscocommented, Apr 24, 2018

By the way… are we free to use the API that is used by the Cypress dashboard? CORS is enabled so I’m guessing so. Are there any limitations on the amount of times I can ping the endpoint?

I find an API that returns test failures far more valuable than the actual dashboard in my case.

0reactions
jennifer-shehanecommented, Jul 23, 2019

I’d recommend using the Cypress Module API to achieve this today. You can access all failures after the test run and handle them however you want.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Flaky Cypress Tests and Network Requests - Kishan Gajera
There can be many potential causes for flaky Cypress tests and this can be a cause of frustration when it comes to testing....
Read more >
Network Requests - Cypress Documentation
Tests are more robust with much less flake. Failure messages are much more precise. You can assert about the underlying request object. Let's...
Read more >
Error Messages | Cypress Documentation
We found an error preparing your test file. This message means that Cypress encountered an error when compiling and/or bundling your test file....
Read more >
Troubleshooting | Cypress Documentation
In some cases the Command Log, responsible for displaying test commands, assertions, and statuses in the Cypress Test Runner, may cause performance issues ......
Read more >
Testing Your App | Cypress Documentation
request() - to make HTTP requests. If you're running node.js on your server, you might add a before or beforeEach hook that executes ......
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