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

Add real Global Before and After hooks #29432

Open
jprealini opened this issue Apr 29, 2024 · 3 comments
Open

Add real Global Before and After hooks #29432

jprealini opened this issue Apr 29, 2024 · 3 comments
Labels
type: feature New feature that does not currently exist

Comments

@jprealini
Copy link

What would you like?

Currently I am facing a scenario where, to setup and teardown test users, I depend on a Cypress plugin developed by our company that uses custom commands to run API requests (using cy.request).

The expectation is that the test users setup will occur ONCE when the test run begins, and the teardown (actually removing the users from db) will happen ONCE after the whole test run has finished

I was convinced that the before and after hooks we write in the support file are executed only ONCE before and after the whole test run, but I learned that they run before and after each spec (which Cypress considers a suite)... I am not sure then about the difference between those hooks and the "before and after" hooks you add in the specs themselves.. wouldn't they do the same?

It would be awesome to provide with real Global Before and Global After hooks for easy test data setup and teardown.

Why is this needed?

It would be easier to run certain code that relies on cypress (cy commands, even custom commands, or cy.request) only ONCE starting the test run and ONCE finishing the test run, instead of having to write "cypress agnostic" code to be called from "before:run" or "after:run" events.

Other

No response

@jprealini jprealini changed the title Add real Global Before hook Add real Global Before and After hooks Apr 29, 2024
@jennifer-shehane
Copy link
Member

There is a before:run and after:run API as part of setupNodeEvents that does this.

I'm not sure if we could hook into this once a spec has begun, so that you could run Cypress tests.... it would require quite a bit of rework anyway.

@jennifer-shehane jennifer-shehane added the type: feature New feature that does not currently exist label Apr 30, 2024
@jprealini
Copy link
Author

Thanks for the input @jennifer-shehane

Yeah, I know I can use those apis. The problem is that the whole setup and cleanup processes in our scenario make use of a lot of cypress commands, which blocks us from being able to call them from there.

It would be awesome if we could have a true global before hook (one that runs just one at the beginning of the test run) and a true global after hook (that runs just once, after the whole test run has completed)... Are there any limitations that would prevent Cypress to provide such functionality?

Thanks again.

@jprealini
Copy link
Author

NOTE: I know that using the after or afterEach hooks to cleanup state or test data is an anti pattern. The problem we face is that we are required by dba's to cleanup every test data we create in the lower environments databases for our tests as soon as the test runs are completed.

We could (and that's what we are doing) delete the users after each spec is completed. The problem with that is that we are creating between 4 and 10 users depending on the run type, and creating that amount of users takes a lot of time... If we have to create them before each spec, with 20 specs we will have more than 40 minutes taken only for test users setup and cleanup.

That's why we decided to create a set of users on startup, use them across all our test run, and clean them up when we are done...

Hope that makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature New feature that does not currently exist
Projects
None yet
Development

No branches or pull requests

2 participants