Skip to content

Cypress runs commands twice when no baseUrl defined #2777

@rafael-anachoreta

Description

@rafael-anachoreta

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    E2EIssue related to end-to-end testingTriagedIssue has been routed to backlog. This is not a commitment to have it prioritized by the team.existing workaroundprevent-stalemark an issue so it is ignored by stale[bot]type: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions