Skip to content
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

Public API for start/end of all tests run #818

Closed
amitzur opened this issue Aug 5, 2018 · 10 comments
Closed

Public API for start/end of all tests run #818

amitzur opened this issue Aug 5, 2018 · 10 comments

Comments

@amitzur
Copy link

amitzur commented Aug 5, 2018

Not sure this is a documentation issue, but the feature does exist in Cypress.
There's a need for listening to an event that fires when tests start running. I see 3 cases where this can happen when running cypress open:

  1. Launch browser
  2. Click on "run all tests" button
  3. Reload the browser tab

When running cypress run this should happen only once, of course.

From inspecting the events that Cypress emits, I see run:start and run:end answer exactly this need. So the question is, can these be used as a public API for this hook? If so, I was right to think it's a documentation issue.

@brian-mann
Copy link
Member

If you want to do things in the browser you can have root level before and after hooks which come from mocha and already provide this.

For plugin related events you have before:browser:launch, but there is no "end" events. In interactive mode we will not provide these because interactive mode cypress open never really "ends".

In run mode cypress run you can use the module API and when all the tests finish it will yield you a results object with everything. So today you already have several different options for programmatically tapping into before / after tests run in Cypress.

@amitzur
Copy link
Author

amitzur commented Aug 7, 2018

I essentially wanted to be sure my code runs before any test-related code, but I can go with your suggestion to use root level before and after.

I'd argue that in interactive mode there is an end, and that is the finish point of all the tests that currently run. It's true that this is fluid when open during development.

My issue now is that in order to use root level after I need it to be able to fail the whole test in case there's a failure there. That doesn't happen today (as opposed to mocha), as I described here:
cypress-io/cypress#2296

@amitzur amitzur closed this as completed Aug 7, 2018
@amitzur
Copy link
Author

amitzur commented Oct 16, 2018

I'm reopening this since root level before and after hook doesn't answer my need. I'm not sure if Cypress changed behavior between v2 and v3. I suspect parallelism might have affected it.
When running cypress run, every spec file is being run separately and root level before and after hooks that were defined in the supportsFile happen once for each file.

There is a need to have a way to run a hook one time at the beginning and one time at the end of the whole run of all the files

@amitzur amitzur reopened this Oct 16, 2018
@jennifer-shehane
Copy link
Member

jennifer-shehane commented Oct 17, 2018

@amitzur Yes, this behavior did change in the 3.0 release - that before and after run for each spec file.

Perhaps our Module API would suit your needs better. You can run any code before the cypress.run() and any code after in the resolved Promise.

@amitzur
Copy link
Author

amitzur commented Oct 28, 2018

I cannot use the module API since we run Cypress in a straight forward way and I wouldn't want to complicate that.
I do think this is an important hook, and can be achieved by providing plugins with the following API:

module.exports = (on, config) => {
  on('run:start', () => {...});
  on('run:end', () => {...});
}

By running localStorage.debug=* I noticed the events run:start and run:end provide this functionality. So I think the road to providing this functionality is short.

@jennifer-shehane
Copy link
Member

Hey @amitzur, we agree. In fact the work for adding this hook has mostly been done and will be part of a future release. You can see those changes in these doc updates here: #890

@amitzur
Copy link
Author

amitzur commented Oct 29, 2018

Thanks, that's great to know!
I only wonder if run:before and run:after are going to be available also in interactive mode. I believe there's no reason for them not to.

@jennifer-shehane
Copy link
Member

@amitzur No, those would not be available in interactive mode. The run technically never 'ends' when in interactive mode since the browser stays open.

@gilgold
Copy link

gilgold commented Apr 10, 2020

Is there any update on this issue? I could really use those events right about now.. (all other solutions do not work / not relevant to my needs)

@jennifer-shehane
Copy link
Member

@gilgold This issue is about documentation, not the Cypress product itself. See this issue for any actual events feature: cypress-io/cypress#2840

I'm going to close this documentation issue - as the behavior of before and `after has been documented correctly now. The rest of the comments seem related to actually wanting new features in Cypress covered in cypress-io/cypress#2840

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants