Karim Abdel Hamid
Karim Abdel Hamid

Reputation: 423

How to run before on most cypress tests but not all

I have a login test I want to code for cypress. Currently, I need to login before all tests so i use a before block. But I want to automatically run the login test before all tests except for 2 or 3 files. Is that possible? Or do I need to move the login code into the describe blocks themselves. I'd rather segment the tests because I don't want so much redundancy, writing the same code so many times.

Upvotes: 1

Views: 3366

Answers (1)

Kerry
Kerry

Reputation: 431

The official Cypress documentation on hooks has a great example showing where before and beforeEach hooks are placed in the describe and it blocks, as well as outside of the describe block.

One thing they mention is to be wary of root level hooks, because of its unpredictable order of execution when running your specs together.

As far as I can see, you have a couple options here. 1) You can have your hooks explicitly stated in each of your spec files, so instead of having a root level login, you call that for each spec file that needs to run in. 2) You create a sort of base file where you can toggle whether the spec file will execute the login functionality.

But again, #2 may lead to some unpredictable behavior. My suggestion is to keep it simple and clean, and have your hooks specified directly in your spec files.

Upvotes: 3

Related Questions