Imagine that we have a Cypress project with end-to-end tests and a support file. The support file has the beforeEach
hooks common to all spec files.
1 | beforeEach(function () { |
Every e2e spec file is affected by the e2e support file, since it loads and runs before every spec. Let's say we have 2 tests in the spec.cy.js
file:
1 | it('runs a normal test', () => { |
We can see the beforeEach
hook execute before both tests.
Here is our challenge: can we somehow skip or modify the beforeEach
hook from the test? Can we skip it? In this blog, I will show how you can skip the hook by using the test config object. You can similarly control all aspects of the test.
🎁 You can find the source code shown in this blog post in the repo bahmutov/control-before-each-example.
The test config
Let's pick how a test can control its behavior. I will use the test config object. Let's say the test can signal that it wants the beforeEach
hook skipped by setting a property:
1 | it( |
How can the code inside the beforeEach
hook "know" about this configuration value skipBeforeEach
? Let's inspect what we have in the test context object.
1 | beforeEach(function () { |
There is a lot stored in the currenTest
object, but the last property _testConfig
looks promising.
Let's expand it. Oh my, what a find!
So the beforeEach
hook code can find the test configuration values inside the this.currentTest._testConfig.unverifiedTestConfig
object. If the test does not have any custom config properties, the object is empty. Let's control the hook's behavior.
1 | beforeEach(function () { |
Tip: you can go hardcore and overwrite the global beforeEach
function to skip it automatically, but I have not found a way.