-
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
Cypress runs commands twice when no baseUrl defined #2777
Comments
I found that this happened when you visit url which is different to baseUrl in config. |
I met the same bug. In my case, it's worth noting that |
@dirtyhenry Any code provided within the |
Hey @rafael-anachoreta, thank you for providing this great detail on this issue. I have confirmed that indeed, these I imagine this has to do with how we handle visiting sites when no Workaround Today Provide a |
Hi @jennifer-shehane: I don't really understand your message since none of my code is in the |
@jennifer-shehane same behaviour is when |
@dirtyhenry Sorry for not being clear. If you want code to run only once before each spec file as opposed to before each test, make sure to place the |
Hello everyone, |
@jennifer-shehane Do you know of any workarounds other than setting |
I have the same issue even when baseUrl is set to http://localhost:3000. I'm using this hack to avoid running the commands twice in the cypress.json file I have
and in the in support index.js: beforeEach(function() {
if (Cypress.config("firstRun")) {
cy.exec("your command");
Cypress.config("firstRun", false);
}
});
afterEach(function() {
Cypress.config("firstRun", true);
}); |
Totes looking forward to this fix! 😱🙊 Tried the Cypress.config("firstRun") workaround and baseUrl in config but still executes twice 😭 |
@jamesalexmorgan same here. still an issue for me. any workaround? |
Have the same issue and can't set baseUrl because tests running in several environments. plugins/index.js module.exports = (on, config) => {
const env = config.env.environment;
const url = config.env[env].url;
config.baseUrl = url;
return config
} cypress.env.json "env1": {
"url": "https://url.env1.com"
},
"env2": {
"url": "https://url.env2.com"
} and runing commands npx cypress run --env environment=env1
npx cypress open --env environment=env2 |
Good job, thanks! 👍 |
I'm doing a full-cycle registration test in which a confirmation email is sent, then a task is triggered to open the email and find a confirmation link (with a domain different from the one set in baseUrl) in the inbox. Once the link is found, Really hope to see a feasible workaround for this. |
@jennifer-shehane I see that you have labeled this as the intended workaround. However, have you seen occasions where this workaround does not work? It seems like I am not the only one who is unable to get this duplicate behavior to stop even when setting a baseUrl in the |
Had the same issue and realized it was because my beforeEach was an async function |
@jennifer-shehane have You any idea how this could work? I have this same issue. |
* Set config.baseUrl and visit relative URLs to avoid Cypress bug which runs "before" hooks twice (github.com/cypress-io/cypress/issues/2777) * Remove "after" hook dbResets - make tests responsibile for their own initial state, not that of the subsequent test
Another example of this behavior is with people who set the Having no it('Should only log once', () => {
cy.task('log', 'running!')
Cypress.config('baseUrl', 'https://docs.cypress.io')
cy.visit('/')
}) A more realistic example is someone dynamically generating this url Cypress.config('baseUrl', `https://${getOrigin()}.cypress.io`) This is currently the best workaround in this situation I believe: #2777 (comment) |
* Set config.baseUrl and visit relative URLs to avoid Cypress bug which runs "before" hooks twice (github.com/cypress-io/cypress/issues/2777) * Remove "after" hook dbResets; make tests responsible for their own initial state, not that of the subsequent test
I have the same issue however, I am using a before statement with a request statement to an api with a cypress custom command to create a user then beforeEach test case logs that newly created user in on the website that I'm building scripts on cypress for. So when this is running it creates two new users every time, sometimes it logs in the first user. |
This is expected behavior based on the way we switch the top superdomain. We could forcibly restrict the |
Thanks for getting back to me @brian-mann , Do you have any suggestions for this not to run basically twice? |
I have my baseUrl set in my cypress.json and it still runs twice. |
I have not found a better solution than to make the fake first visit so that cypress understands which domain he needs to visit and so that he does not make visit twice to confirmation URL.
|
Disabling the following setting in the cypress.config.ts helped me: testIsolation: false |
Based on suggestions of using plugin file... Adding the |
In the local development environment, I found that this double execution of a cy.request to a test data endpoint only occured when running Cypress in the CLI. With the GUI it runs just once. I managed to fix this by setting a baseUrl with its value being the same as our test data endpoint. However, the same trick will not work when running Cypress in GitLab. It's still executed twice there. It's kind of crazy that this has been an issue since 2018. |
I'm also encountering this issue. I have baseURL is set in cypress config, and tests are being executed twice in GitHub Actions. |
Also encountering the same issue, commenting to increase visibility |
Currently working on this bug at my company. If we can't find a workaround, we're switching to Playwright. |
* Set config.baseUrl and visit relative URLs to avoid Cypress bug which runs "before" hooks twice (github.com/cypress-io/cypress/issues/2777) * Remove "after" hook dbResets; make tests responsible for their own initial state, not that of the subsequent test
A related issue where the retries states are duplicated in the after:run hook due to this bug also. #30794 |
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:
Note: this isn't exclusive to hooks! The below spec will also print unwanted results:
Results:
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
The text was updated successfully, but these errors were encountered: