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.

Add FAQ for why Cypress passes locally and not in CI.

See original GitHub issue

I’m submitting a…

[ ] Bug report
[x] Content update
[ ] Process update (build, deployment, ... )

Type of bug / changes

It is pretty common question / complain from users and deserves an answer.

Some saved responses in the past:

1

There are a lot of reasons tests could fail in CI and pass locally. On top of that - running in CI is a bit of a black box. So, the combination of these 2 facts make it especially frustrating to debug failing tests that solely fail in CI.

Some reasons this could happen:

  1. There is a problem isolated to the Chrome version of our Electron browser. We’re working on updating it every day. https://github.com/cypress-io/cypress/pull/4720 Installing a later Chrome version in Travis would highlight if this is your problem.
  2. If there is a build process in between running tests locally and running in CI - sometimes Cypress test failures are highlighting a bug in the build process.
  3. Variability in timings when running your application in CI. If you have a test that will only pass when a network request finishes in 10ms (which happens every time on your local machine) and then that request takes 20ms on your CI machine (for whatever reason from that machine - location, CPU, etc slows it down) - this can cause some unexpected failures. You’ll want to make sure to write assertions around the important steps that need to be reached before moving on to the next action in your tests. for example - ensuring a network request has finished before looking for the DOM element that relies on the data from that network request. I would suggest reviewing the video of the test failures, looking at the timings of commands and actions in the Command Log, and comparing them to your local run’s Command Log to highlight if this is the issue.

2

Tests run in CI run within the Electron browser. So, the first suggestion is to run the tests locally within the Electron browser with DevTools open to see if there are any issues there.

Since you already mentioned running the tests locally with other browser combinations, next I would make sure that video recording and/or screenshots are turned on during the CI run. Review the video to see if anything can be detected there that is causing the failure.

If it’s still not obvious, compare the Command Log from within the video to the Command Log of the same test passing locally when run in Cypress. Take a screenshot of each at the same moment and compare each command side by side. Seeing differences of the execution time or missing commands, etc can sometimes illuminate differences in the timings of requests or other methods that may have caused an issue.

3 (more about flaky tests)

Most often in cases of flaky tests, we see that there is not enough assertions surrounding the actions or XHR requests/responses necessary before moving on to the next assertion. So if there is any variation in the speed of the XHR requests response in interactive mode versus run mode, then there will start to be failures in one over the other - or flaky tests.

So, for example if you had a test to check that when you click a ‘Save’ button that the users page refreshes with the correct content, you may write a test like so:

cy.get(‘.name-input’).type(‘Jennifer’) cy.get(‘form’).contains(‘Save’).click() cy.get('#user- name).should(‘contain’, ‘Jennifer’)

Each command by default will wait 4 seconds to meet it’s requirements, but if the POST for the form request sometimes takeds longer to respond than it takes for the “cy.get('#user- name).should(‘contain’, ‘Jennifer’)” to run, then it will fail sometimes. Because of this, we recommend asserting on as many required steps as possible - this also helps later to isolate where the exact failure is when debugging on an actual bug.

So, the example above would be less flaky written like this:

cy.server() cy.route(‘POST’, ‘users/*’).as(‘updateUser’) cy.route(‘GET’, ‘users’).as(‘getUsers’)

cy.get(‘.name-input’).type(‘Jennifer’) cy.get(‘form’).contains(‘Save’).click() cy.wait(‘@updateUser’).then((xhr) => { expect(xhr.requestBody).to.have.property(‘name’, ‘Jennifer’) } cy.url(‘contains’, ‘users/123’) cy.wait(‘@getUsers’) cy.get('#user- name).should(‘contain’, ‘Jennifer’)

Now when the last line runs, we don’t have to worry about the POST or that the POST sent the right information, that the url changed and the GET’s or whatever requests needed to respond - we know it’s in the right state to find the content here.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:11
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

32reactions
RabinMallickcommented, Apr 22, 2022

Facing same issue in 2022. Tests are passing locally but fails in Bitbucket pipeline. Any promising solution?

4reactions
daryamatsenkocommented, Oct 26, 2022

Experiencing the same issue: tests always passed locally but failing in circleci. Did anybody find a solution for this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Test passing locally but not in CI - cypress - Stack Overflow
I'm observing something very similar in my tests and I just cannot find a way to get it working. – Robert Strauch. Nov...
Read more >
Using Cypress - Cypress Documentation
Why do my Cypress tests pass locally but not in CI? There are many reasons why tests may fail in CI but pass...
Read more >
Introduction | Cypress Documentation
Typically you will need to boot a local server prior to running Cypress. When you boot your web server, it runs as a...
Read more >
General Questions | Cypress Documentation
Is Cypress free and open source? Cypress is a free, downloadable and open source (MIT license) application. This is always free to use....
Read more >
Troubleshooting | Cypress Documentation
Tip: use the cypress info command to see all locally detected browsers. ... If this does not succeed, it will fall back to...
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