-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Comments
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')
})
}) |
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 To account for this we need to be sure to skip before hooks when rehydrating the state. |
Thank you very much for your quick answer. Amazing job what you guys are doing 👏 |
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? |
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:
Note that after we move on the the "part 2" block and call |
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 |
@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 |
The code for this is done in cypress-io/cypress#7035, but has yet to be released. |
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. |
The code for this is done in cypress-io/cypress#7154, but has yet to be released. |
Released in This comment thread has been locked. If you are still experiencing this issue after upgrading to |
Current behavior:
When running my spec file, I noticed that some code in the
before
hook (notbeforeEach
) 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:
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()
Second it()
Third it()
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 abeforeEach
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 theconsole.log
reveals that.Versions
The text was updated successfully, but these errors were encountered: