Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single test executes the same cy.task() command twice #4146

Closed
ollie-o opened this issue May 6, 2019 · 3 comments
Closed

Single test executes the same cy.task() command twice #4146

ollie-o opened this issue May 6, 2019 · 3 comments
Labels
type: duplicate This issue or pull request already exists

Comments

@ollie-o
Copy link

ollie-o commented May 6, 2019

Note: Here is a fork of cypress-test-tiny that reproduces the bug: https://github.com/ollie-o/cypress-test-tiny

Current behavior:

Given a test like this:

context("Very bland test", () => {
  it("replicates my bug", () => {
    cy.task("log", "This should only be logged in the terminal once!");

    cy.visit("https://google.com");
  });
});

The cy.task() command is executed twice.

Desired behavior:

cy.task() should only be executed once in the above test, but instead it is executed twice. We can tell because it leaves two logs in the Terminal window.

Steps to reproduce: (app code and test code)

Test File

context("Very bland test", () => {
  it("replicates my bug", () => {
    cy.task("log", "This should only be logged in the terminal once!");

    cy.visit("https://google.com");
  });
});

Plugins File

module.exports = (on, config) => {
  // `on` is used to hook into various events Cypress emits
  // `config` is the resolved Cypress config
  on("task", {
    log(message) {
      console.log(message);

      return null;
    }
  });
};

How to run

./node_modules/.bin/cypress run --spec **/test.spec.js*

What you will see

  Running: examples/test.spec.js...                                                        (1 of 1) 


  Very bland test
This should only be logged in the terminal once!
This should only be logged in the terminal once!
    ✓ replicates my bug (908ms)

Versions

Cypress: 3.2.0
Operating System: MacOS Mojave (10.14.4)
Browser: At least Electron. I haven't tested any others.

@ollie-o ollie-o changed the title Single test executes cy.task() command twice Single test executes the same cy.task() command twice May 6, 2019
@bahmutov
Copy link
Contributor

bahmutov commented May 7, 2019 via email

@ollie-o
Copy link
Author

ollie-o commented May 7, 2019

Thanks for the response @bahmutov!

That workaround does cause cy.task() to execute the expected number of times 👍

Another workaround I've been using is to put the cy.task() in a beforeEach() and the cy.visit() in an it() block. That also helps 👍 Seems like that is not actually working for me.

It would still be great if you could resolve the core bug though!

On a personal level it was quite annoying for me to keep searching for the reason my code was executing twice - I kept searching for the bug in my own code, just to discover that it is a bug in the framework we are using. It would be great to save future users of Cypress from that frustration.

@jennifer-shehane
Copy link
Member

Thanks for opening! This is a duplicate of this issue: #2777

@jennifer-shehane jennifer-shehane added the type: duplicate This issue or pull request already exists label May 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants