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 'BeforeAll' hook #758

Closed
3 tasks done
sszemer opened this issue Jul 11, 2022 · 9 comments · Fixed by #1114
Closed
3 tasks done

Add 'BeforeAll' hook #758

sszemer opened this issue Jul 11, 2022 · 9 comments · Fixed by #1114

Comments

@sszemer
Copy link

sszemer commented Jul 11, 2022

Current behavior

'Before' and 'After' hook names are missleading
from wiki:
"Before() and After() is similar to Cypress' beforeEach() and afterEach()"

Desired behavior

i want some code to run only once for all tests. in cypress i would do something like:
before(() => { // runs once before all tests in the block })
in preprocessor the name is confusing as it suggests running once.

it would be preferable to change 'Before' to 'BeforeEach' and make 'Before' behave in a way that it only runs once per test run

Before({ tags: "@foo" }, function () { // This hook will be executed once before all scenarios tagged with @foo. });

BeforeEach({ tags: "@foo" }, function () { // This hook will be executed before each scenario tagged with @foo. });

Test code to reproduce

Before({ tags: "@foo" }, function () { // This hook will be executed before scenarios tagged with @foo. });

Versions

  • Cypress version:
  • 9.7.0
  • Preprocessor version:
  • 4.0.1
  • Node version:
    *v16.15.1

Checklist

  • I've read the FAQ.
  • I've read Instructions for logging issues.
  • I'm not using cypress-cucumber-preprocessor@4.3.1 (package name has changed and it is no longer the most recent version, see #689).
@badeball
Copy link
Owner

i want some code to run only once for all tests. in cypress i would do something like:
before(() => { // runs once before all tests in the block })
in preprocessor the name is confusing as it suggests running once.

You can still do this. After and Before aren't there to replace Cypress' own hooks, but to replicate behavior seen in cucumber-js.

Before({ tags: "@foo" }, function () { // This hook will be executed once before all scenarios tagged with @foo. });

This doesn't exist in cucumber-js, thus it will not exist here.

@jljouannic
Copy link

I think it does exist in cucumber-js. There are two hooks BeforeAll and AfterAll which are not supported in cypress-cucumber-preprocessor but would deserve to be: https://github.com/cucumber/cucumber-js/blob/main/docs/support_files/hooks.md#beforeall--afterall

@badeball
Copy link
Owner

You're right, these aren't implemented but should be. I can't look into it now, contributions are welcome.

@lotmek
Copy link

lotmek commented Sep 24, 2023

Since the feature is about adding some setup before or after the run, would'nt it be better if we allow the users to add some customized callback inside the Cypress after:run & before:run events ?

if (!options.omitBeforeRunHandler) {
on("before:run", () => beforeRunHandler(config));
}
if (!options.omitAfterRunHandler) {
on("after:run", () => afterRunHandler(config));
}

@badeball
Copy link
Owner

@lotmek, this is already possible through the API that was devised before the existence of cypress-on-fix and is described here.

As for the original proposal, a BeforeAll(..) hook as seen in cucumber-js, would not support any tags argument, nor be provided a world context. Additionally it would have no impact on any report generation, as least not as of yet. It would probably be a simple wrapper around Cypress' own before(..) hook and not allow anything that isn't already possible in user-land. Thus, I suspect that the desire for this stem from a misunderstanding. However, I'm inclined to add it for consistency.

@VidhiRambhia
Copy link

Hi,

I'm interested in contributing. Can I pick this up?

@badeball
Copy link
Owner

badeball commented Oct 9, 2023

You may. I recommend starting by looking at it as it exist in cucumber-js. Draw inspiration from their tests. Describe the feature using tests before committing to an implementation and propose it.

@VidhiRambhia
Copy link

Hi @badeball,

I have described the feature and proposed an implementation based on the above comments for going about this - #1114. Please share your thoughts/feedback on this initial draft. Thank you!

@badeball
Copy link
Owner

badeball commented Nov 8, 2023

The above-mentioned PR has been merged and released with v19.1.0.

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

Successfully merging a pull request may close this issue.

5 participants