-
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
Global before() and after() hooks are executed multiple times when running tests with Command Line but only one time when running tests with Test Runner #5160
Comments
The 'Run all specs' doesn't reflect how the tests are run during The intended behavior is to close and reopen the browser in between each spec file, so the |
After a lot of debugging and exploration, I've landed in a fix for the problem where a Cypress run creates multiple happo reports, one per test file. This was first reported last week and to begin with I couldn't repro it. In short, this was the bug: When you have multiple test files, Happo creates one report per file, instead of one combined report. When running with `cypress open`, the `after` hook was only called once per full run. But if you ran `cypress run`, the behavior changed to calling `after` once per test file. There's a bug for this in the Cypress issue tracker: cypress-io/cypress#5160 To mitigate this, I tried hard to find a place/hook or something where Cypress would tell me one of these things: The full suite is done, or These are the specs in the full run Either of those would be sufficient enough for our plugin to conditionally create the Happo report once the `after` hook is called for the last time. But I couldn't find any of this information available from Cypress. So I did what Percy.io is doing -- added a wrapper command that you need to use instead of `cypress run`: happo-cypress -- cypress run This call will set up a server locally, invoke the wrapped command, accept `requestIds` to be POSTed to the server and then finally, once the wrapped command is done, create the full Happo report. It's a little unfortunate that we have to go down this route as there's a lot of complexity involved: - Users will have to make sure to wrap the Cypress command - We have to be careful about choosing a good port (for now this is hard-coded).
After a lot of debugging and exploration, I've landed in a fix for the problem where a Cypress run creates multiple happo reports, one per test file. This was first reported last week and to begin with I couldn't repro it. In short, this was the bug: When you have multiple test files, Happo creates one report per file, instead of one combined report. When running with `cypress open`, the `after` hook was only called once per full run. But if you ran `cypress run`, the behavior changed to calling `after` once per test file. There's a bug for this in the Cypress issue tracker: cypress-io/cypress#5160 To mitigate this, I tried hard to find a place/hook or something where Cypress would tell me one of these things: The full suite is done, or These are the specs in the full run Either of those would be sufficient enough for our plugin to conditionally create the Happo report once the `after` hook is called for the last time. But I couldn't find any of this information available from Cypress. So I did what Percy.io is doing -- added a wrapper command that you need to use instead of `cypress run`: happo-cypress -- cypress run This call will set up a server locally, invoke the wrapped command, accept `requestIds` to be POSTed to the server and then finally, once the wrapped command is done, create the full Happo report. It's a little unfortunate that we have to go down this route as there's a lot of complexity involved: - Users will have to make sure to wrap the Cypress command - We have to be careful about choosing a good port (for now this is hard-coded).
This comment has been minimized.
This comment has been minimized.
@jennifer-shehane I am still trying to find a way to solve workaround this. I opened the feature request #29432 a couple of weeks ago. One thing I can´t completely understand is why does Cypress run what is supposed to be a global before and after hook (from the support file) actually before and after every spec.... Doesn't each spec have its own "before" and "after" hooks for that? I would expect, by logic, that the hooks would work something like this: Support file "before" hook - RUN ONCE AT THE BEGINNING OF THE TEST RUN And this would simplify a lot scenarios where one has to run custom commands to setup data (call apis to populate databases with test data or similar) Do you think this could be possible to implement? Thanks |
@jprealini If you're running specs in parallel machines, would we run the |
@jennifer-shehane hmm... I guess you have a point there. I didn´t think about that. I was following up on some threads about this same feature requested a few years ago, and I see the before:run after:run events where added to solve this. The caveat here is that if we have some logic that needs to run inside the before:run but which has heavy dependency on Cypress, then we can't run it and we need to refactor/redesign our code to NOT use any cypress commands to be able to run it inside the before:run event... Do you think there is a remote possibility that this could be achieved sooner or later? Thanks |
@jprealini It's not currently something that we're looking at unfortunately. |
Current behavior:
If user defines global
before()
orafter()
hook in the support file (support/index.js
) then the hook is executed only one time when running all tests with the Test Runner (npx cypress open
=> Run all specs button).However if we run the same code with CLI (
npx cypress run
) then the global hooks are executed for each .spec file in the suite.Desired behavior:
Global
before()
andafter()
hooks should be run only once per test run.Steps to reproduce: (app code and test code)
support/index.js
npx cypress run
npx cypress open
=> Run all specs buttonVersions
Cypress 3.4.1
The text was updated successfully, but these errors were encountered: