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.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
jennifer-shehanecommented, Apr 28, 2020

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.

2reactions
jennifer-shehanecommented, Jun 19, 2018

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rerun unit tests affected by change [closed]
When I am done, I simply remove the "only" part and run a full set of tests. Always run the full set of...
Read more >
Error Messages | Cypress Documentation
We found an error preparing your test file. This message means that Cypress encountered an error when compiling and/or bundling your test file....
Read more >
rouch/Cypress: Fast, easy and reliable testing for anything that runs ...
Cypress - Fast, easy and reliable testing for anything that runs in a browser.
Read more >
Is there a way to make cucumber try a scenario again before ...
Then, after, I'll rerun only the failed tests with the following command ... and that it clutters up test metrics if you have...
Read more >
How NOT to test custom React hooks (and how to test them)
Codesandbox: https://codesandbox.io/s/lp057z0vxzIt can be tempting to try and call your custom hook manually and test it that way, ...
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