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.
Cypress should only run once.
//__bug.spec.js
describe('Bug?', ()=>{
before(() =>{
cy.task('message', 'I\'m running!');
});
it('Should only log once', () => {
cy.visit('https://www.google.com');
});
});
//index.js
module.exports = (on) => {
on('task', {
message (args) {
console.log(args);
return null;
}
});
};
====================================================================================================
(Run Starting)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Cypress: 3.1.1 โ
โ Browser: Electron 59 (headless) โ
โ Specs: 1 found (__bug.spec.js) โ
โ Searched: cypress/integration/__bug.spec.js โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Running: __bug.spec.js... (1 of 1)
Bug
I'm running!
I'm running!
โ Should only log once (1159ms)
Note: this isn't exclusive to hooks! The below spec will also print unwanted results:
describe('Bug', ()=>{
it('Should only log once', () => {
cy.task('message', 'I\'m running!');
cy.visit('https://www.google.com');
});
it('Should only log once', () => {
cy.task('message', 'I\'m running!');
cy.visit('https://www.google.com');
});
it('Should only log once', () => {
cy.task('message', 'I\'m running!');
cy.visit('https://www.google.com');
});
it('Should only log once', () => {
cy.task('message', 'I\'m running!');
cy.visit('https://www.google.com');
});
});
Results:
Running: __bug.spec.js... (1 of 1)
Bug
I'm running!
I'm running!
โ Should only log once (1091ms)
I'm running!
โ Should only log once (3876ms)
I'm running!
โ Should only log once (992ms)
I'm running!
โ Should only log once (1449ms)
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?
Electron 59 headless
Cypress 3.1.1
macOS High Sierra 10.13.6
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 before code runs twice only when all specs are run at once (ie click on Run all specs). When run one by one, each spec behaves as expected.
@dirtyhenry Any code provided within the support file will run before each spec file as documented here: https://on.cypress.io/writing-and-organizing-tests#Support-file
Hey @rafael-anachoreta, thank you for providing this great detail on this issue.
I have confirmed that indeed, these cy.task() commands are running twice, which is unintended behavior. I've also confirmed that this bug is only present when baseUrl is not defined.
I imagine this has to do with how we handle visiting sites when no baseUrl is defined. See an explanation of this behavior here.
Workaround Today Provide a baseUrl within your Cypress configuration
Hi @jennifer-shehane: I don't really understand your message since none of my code is in the support file.
@jennifer-shehane same behaviour is when baseUrl is defined but I want to visit other page. Ie. I set baseUrl='https://www.google.com/' and in my test will do cy.visit('https://duckduckgo.com/') then all before commands will run twice.
@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 before hook within your support file.
Hello everyone,
Any update ? In my case I am using cy.task() to load/unload data in database using the before/after hooks, so it is causing unexpected behavior.
@jennifer-shehane Do you know of any workarounds other than setting baseUrl?
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
"firstRun": true
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.
My workaround is setting baseURL in plugin file.
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
Have the same issue and can't set baseUrl because tests running in several environments.
My workaround is setting baseURL in plugin file.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, cy.visit(confirmationLink) results in the test being run twice.
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 cypress.json file
Had the same issue and realized it was because my beforeEach was an async function
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,
cy.visit(confirmationLink)results in the test being run twice.Really hope to see a feasible workaround for this.
@jennifer-shehane have You any idea how this could work? I have this same issue.
And next case - i have this same problem in CI, when tests are running in pipeline.
Most helpful comment
Hey @rafael-anachoreta, thank you for providing this great detail on this issue.
I have confirmed that indeed, these
cy.task()commands are running twice, which is unintended behavior. I've also confirmed that this bug is only present whenbaseUrlis not defined.I imagine this has to do with how we handle visiting sites when no
baseUrlis defined. See an explanation of this behavior here.Workaround Today Provide a
baseUrlwithin your Cypress configuration