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

Code in hooks and test are rerun when changing to a different superdomain #1987

Closed
davidzambrana opened this issue Jun 19, 2018 · 11 comments · Fixed by #7035 or #7154
Closed

Code in hooks and test are rerun when changing to a different superdomain #1987

davidzambrana opened this issue Jun 19, 2018 · 11 comments · Fixed by #7035 or #7154
Labels
pkg/server This is due to an issue in the packages/server directory topic: hooks ↪ type: bug

Comments

@davidzambrana
Copy link

Current behavior:

When running my spec file, I noticed that some code in the before hook (not beforeEach) is being run before each of my three test cases, causing my checks to fail. Want to remark that I am using different superdomains for every test, I don't see this when I run all the tests with the same superdomain.

Desired behavior:

Code in the before hook only runs once, before all the tests, no matter if using different superdomains.

Steps to reproduce:

I explain my setup. My spec file consists of:

- before()
- after()
- it() //superdomain 1
- it() //superdomain 2 using an iframe
- it() //superdomain 1

As I am using different superdomains, the only way that I've read to do it is visiting the different domains in different tests (https://docs.cypress.io/guides/guides/web-security.html#One-Superdomain-per-Test)

I've also set "chromeWebSecurity": false

Within the before hook I run several requests to some endpoints as a way to get some data that I will use through the tests; Get it once, and use it across the three it().

I've also set a console.log('This only happens in the beforeHook: ' + var) to see what's the value of my variable, and this is what I see in the run:

  • Running the first it()
    11

  • Second it()
    22

  • Third it()
    33

After every it() the console gets cleared, which is fine, but I don't expect to see the log again during the second and the third tests, as this is supposed to run only once. Notice how the variable gets an increment for each loop, as I would expect had I done it in a beforeEach hook.

In the left side of the window every seems fine as there is no command logging for the before hook when running the second and third tests, but the console.log reveals that.

Versions

  • Cypress version: 3.0.1
  • Browser: Chrome 66
  • OS: Ubuntu Linux 16.04 LTS
@jennifer-shehane jennifer-shehane added the stage: needs investigating Someone from Cypress needs to look at this label Jun 19, 2018
@jennifer-shehane
Copy link
Member

Thank you so much for the thorough bug report! Indeed the behavior does appear to be different when visiting different superdomains.

Cypress version 3.0.1, no configuration, support files, or plugins.

My recreation of the issue:

context('SubDomain Checks', () => {
  let beforeNum = 1
  let beforeEachNum = 1

  before(() => {
    console.warn(`BEFORE ${beforeNum}`)
    beforeNum++
  })

  beforeEach(() => {
    console.warn(`BEFORE EACH ${beforeEachNum}`)
    beforeEachNum++
  })

  it('visit 1 subdomain', () => {
    cy.visit('https://www.cypress.io')
  })

  it('visit 2 subdomain', () => {
    cy.visit('https://docs.cypress.io')
  })

  it('visit 3 subdomain', () => {
    cy.visit('https://example.cypress.io')
  })
})

context('SuperDomain Checks', () => {
  let beforeNum = 1
  let beforeEachNum = 1

  before(() => {
    console.warn(`BEFORE ${beforeNum}`)
    beforeNum++
  })

  beforeEach(() => {
    console.warn(`BEFORE EACH ${beforeEachNum}`)
    beforeEachNum++
  })

  it('visit 1 superdomain', () => {
    cy.visit('https://www.cypress.io')
  })

  it('visit 2 superdomain', () => {
    cy.visit('https://www.wikipedia.org')
  })

  it('visit 3 superdomain', () => {
    cy.visit('https://www.dictionary.org')
  })
})

Preserved console warnings for subdomain tests
screen shot 2018-06-19 at 11 43 04 am

Preserved console warnings for superdomain tests
screen shot 2018-06-19 at 11 43 38 am

@jennifer-shehane jennifer-shehane added type: bug stage: ready for work The issue is reproducible and in scope and removed stage: needs investigating Someone from Cypress needs to look at this labels Jun 19, 2018
@brian-mann
Copy link
Member

brian-mann commented Jun 19, 2018

It's because Cypress must swap to the new superdomain, which causes the specs to rerun.

We internally handle this by rehydrating the state of all the previous tests and ensuring that we skip over them if they previous ran.

However, the test that cause the change to the new superdomain is rerun, and because we internally skip the previous tests it then mocha believes this is the first test to be run, and then will rerun its before hook.

To account for this we need to be sure to skip before hooks when rehydrating the state.

@davidzambrana
Copy link
Author

Thank you very much for your quick answer. Amazing job what you guys are doing 👏

@hackel
Copy link

hackel commented Nov 28, 2018

Is there currently any work-around that might instruct Cypress to switch to the second superdomain as soon as the test starts, thereby avoiding running all of my setup code (seeding DB, etc.) twice?

@WesleySSmith
Copy link

WesleySSmith commented Jan 18, 2019

I've found a similar issue, and I'm not sure if it's fully explained by @brian-mann 's comment above, so adding it here in case this is a different variation.

My spec file:

describe('Part 1', function () {

    before(function() {
        cy.task('log', "In 1-before");
    });

    beforeEach(function() {
        cy.task('log', "In 1-beforeEach");
    });

    specify('A', function() {
        cy.task('log', "In 1-A before visit");
       cy.visit("https://cypress.io");
       cy.task('log', "In 1-A after visit");
    });
});


describe('part 2', function () {

    before(function() {
        cy.task('log', "In 2-before");
    });

    beforeEach(function() {
        cy.task('log', "In 2-beforeEach");
    });

    specify('B', function() {
        cy.task('log', "In 2-B before visit");
        cy.visit("https://example.com");
        cy.task('log', "In 2-B after visit");
    });
});

The output:

In 1-before
In 1-beforeEach
In 1-A before visit
	** visit causes restart, as expected
In 1-before
In 1-beforeEach
In 1-A before visit
In 1-A after visit
	** visit works this time, as expected
	** Moving on to second test
In 2-before
In 2-beforeEach
In 2-B before visit
	** visit causes restart, as expected
In 1-before
	** why are we re-running 1-before???
In 2-before
In 2-beforeEach
In 2-B before visit
In 2-B after visit

Note that after we move on the the "part 2" block and call cy.visit() in the 'B' test, the test runner restarts, (as explained above) but then, for some reason, the beforeEach from the "Part 1" test is rerun, even though it's not associated with the test that visited the new superdomain.

@jennifer-shehane jennifer-shehane added pkg/server This is due to an issue in the packages/server directory difficulty: 2️⃣ labels Jan 31, 2019
@jennifer-shehane jennifer-shehane changed the title Requests within the before hook run before each test when using different superdomains Code in hooks and test are rerun when changing to a different superdomain Aug 26, 2019
@c32hedge
Copy link

I think it's worth mentioning here that my duplicate of this issue (#5319) does not involve visiting multiple superdomains--we encountered the issue when there are tests that don't visit a webpage, followed by a test that does; the first test that does a cy.visit() triggers this behavior.

@kuceb
Copy link
Contributor

kuceb commented Oct 10, 2019

Is there currently any work-around that might instruct Cypress to switch to the second superdomain as soon as the test starts, thereby avoiding running all of my setup code (seeding DB, etc.) twice?

@hackel good point. This could be solved by declarative inline test configuration in PR #5346, since we would know ahead of time to switch domains

@kuceb kuceb mentioned this issue Oct 10, 2019
15 tasks
@cypress-bot cypress-bot bot added stage: needs review The PR code is done & tested, needs review stage: work in progress and removed stage: ready for work The issue is reproducible and in scope stage: needs review The PR code is done & tested, needs review stage: work in progress labels Apr 16, 2020
@cypress-bot cypress-bot bot added stage: pending release and removed stage: needs review The PR code is done & tested, needs review labels Apr 23, 2020
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Apr 23, 2020

The code for this is done in cypress-io/cypress#7035, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

@jennifer-shehane
Copy link
Member

Sorry, we accidentally merged the PR above prematurely. This is still in the review stage. We will close this issue and comment when this issue is merged for the next upcoming release.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented May 28, 2020

The code for this is done in cypress-io/cypress#7154, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Jun 8, 2020

Released in 4.8.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v4.8.0, please open a new issue.

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Jun 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
pkg/server This is due to an issue in the packages/server directory topic: hooks ↪ type: bug
Projects
None yet
7 participants