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.

Cypress runs commands twice when no baseUrl defined

See original GitHub issue

Current behavior:

Right now, whenever you start Cypress it will kick off some of the commands twice.

This isn’t a problem most of the time as tests should be self contained anyway, but if you are using before hooks (for instance, to login and store cookies, as suggested by the docs) it becomes quite detrimental.

Desired behavior:

Cypress should only run once.

Steps to reproduce:


//__bug.spec.js
describe('Bug?', ()=>{
    before(() =>{
        cy.task('message', 'I\'m running!');
    });

    it('Should only log once', () => {
        cy.visit('https://www.google.com');
    });
});

//index.js
module.exports = (on) => {
    on('task', {
        message (args) {
            console.log(args);

            return null;
        }
    });
};
  • Run the spec
  • Results:
====================================================================================================

  (Run Starting)

  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚ Cypress:    3.1.1                                                                              β”‚
  β”‚ Browser:    Electron 59 (headless)                                                             β”‚
  β”‚ Specs:      1 found (__bug.spec.js)                                                            β”‚
  β”‚ Searched:   cypress/integration/__bug.spec.js                                                  β”‚
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜


────────────────────────────────────────────────────────────────────────────────────────────────────

  Running: __bug.spec.js...                                                                (1 of 1)


  Bug
I'm running!
I'm running!
    βœ“ Should only log once (1159ms)

Note: this isn’t exclusive to hooks! The below spec will also print unwanted results:

describe('Bug', ()=>{

    it('Should only log once', () => {
        cy.task('message', 'I\'m running!');
        cy.visit('https://www.google.com');
    });
    it('Should only log once', () => {
        cy.task('message', 'I\'m running!');
        cy.visit('https://www.google.com');
    });
    it('Should only log once', () => {
        cy.task('message', 'I\'m running!');
        cy.visit('https://www.google.com');
    });
    it('Should only log once', () => {
        cy.task('message', 'I\'m running!');
        cy.visit('https://www.google.com');
    });

});

Results:

Running: __bug.spec.js...                                                                (1 of 1)


  Bug
I'm running!
I'm running!
    βœ“ Should only log once (1091ms)
I'm running!
    βœ“ Should only log once (3876ms)
I'm running!
    βœ“ Should only log once (992ms)
I'm running!
    βœ“ Should only log once (1449ms)

If the commands are inverted (i.e., if you visit() before logging) then the results are fine, so maybe Cypress waits for visit then reboots?

Versions

Electron 59 headless Cypress 3.1.1 macOS High Sierra 10.13.6

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:6
  • Comments:24 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
kfenskecommented, Sep 24, 2020

@jennifer-shehane I see that you have labeled this as the intended workaround. However, have you seen occasions where this workaround does not work? It seems like I am not the only one who is unable to get this duplicate behavior to stop even when setting a baseUrl in the cypress.json file

5reactions
dawidkostyszakcommented, Jan 17, 2019

@jennifer-shehane same behaviour is when baseUrl is defined but I want to visit other page. Ie. I set baseUrl='https://www.google.com/' and in my test will do cy.visit('https://duckduckgo.com/') then all before commands will run twice.

Read more comments on GitHub >

github_iconTop Results From Across the Web

visit - Cypress Documentation
Visit local files​​ Cypress will automatically attempt to serve your files if you don't provide a host and baseUrl is not defined. The...
Read more >
Cypress triggers cy.wait(1000) twice in separate tests
Cypress changes the parent domain to match the baseUrl, in order to avoid issues with navigating on a website that does not match...
Read more >
Run Two Cypress Test Runners At The Same Time
The configuration files # Β· While running two Cypress instances, we are not using any plugins or custom commands, thus we disable the...
Read more >
cypress-io/cypress - Gitter
@eXpLoD96 No you can't. integrationFolder in cypress.json created to define the root folder for all your tests. When you want to run specific...
Read more >
How to Add Screenshot Testing with Cypress to Your Project
In this post, you will learn how to use Cypress to capture parts of pages of a website. ... The baseUrl is the...
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