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

Feat: add linting rule for before/beforeEach just like the async test rule #151

Merged
merged 5 commits into from
Apr 12, 2024

Conversation

Arcturus5404
Copy link
Contributor

@Arcturus5404 Arcturus5404 commented Mar 1, 2024

PR for adding linting rules to the before and beforeEach. Having an async in the beforeEach and before can introduce unexpected behaviour, where the async process is not finished yet before the test runs. This can cause a test to be flaky. This rule will mark it as an error.

@CLAassistant
Copy link

CLAassistant commented Mar 1, 2024

CLA assistant check
All committers have signed the CLA.

@cypress-app-bot
Copy link

@Arcturus5404 Arcturus5404 marked this pull request as draft March 2, 2024 11:30
@Arcturus5404 Arcturus5404 marked this pull request as ready for review March 2, 2024 11:30
Copy link
Contributor

@AtofStryker AtofStryker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Arcturus5404. Thank you for opening a PR! Can you please fill out the PR description to give reviewers better idea of what you are trying to accomplish with this contribution?

@@ -0,0 +1,52 @@
# Prevent using async/await in Cypress test cases (no-async-tests)

Cypress before that return a promise will cause tests to prematurely start, can cause unexpected behavior. An `async` function returns a promise under the hood, so a before using an `async` function might also error.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we sure this is always the case? This might be true for certain commands but I don't think generally this applies.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not know if this is always true, I guess the cypress team knows this better than me. I noticed that the test method was already being executed while the before was still in progress

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting. I don't think I have run into this but I will take your word for it. Knowing how mocha hooks process and how we integrate with them, I can absolutely see this being true

docs/rules/no-async-before.md Outdated Show resolved Hide resolved
docs/rules/no-async-before.md Outdated Show resolved Hide resolved
@@ -36,6 +36,7 @@ You can add rules:
"cypress/assertion-before-screenshot": "warn",
"cypress/no-force": "warn",
"cypress/no-async-tests": "error",
"cypress/no-async-before": "error",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this issue also occur in after/afterEach lifecycle hooks? Maybe no-async-hooks might be a better rule, if this is the case.

Copy link
Contributor Author

@Arcturus5404 Arcturus5404 Apr 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess so, but I am not sure they will cause a problem. It might has the same potential to break your testcase. Should the new hook rule also include the test rule? one to rule them all?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do know that in before and beforeEach the async code might actually break the test, because of the dependency. But there might not be a problem as such in the after because of race conditions


## When Not To Use It

If there are genuine use-cases for using `async/await` in your before then you may not want to include this rule (or at least demote it to a warning).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If there are genuine use-cases for using `async/await` in your before then you may not want to include this rule (or at least demote it to a warning).
If there are genuine use-cases for using `async/await` in your `before/beforeEach` function callback, then you may not want to include this rule (or at least demote it to a warning).

@AtofStryker AtofStryker merged commit 8628331 into cypress-io:master Apr 12, 2024
7 checks passed
@MikeMcC399

This comment was marked as resolved.

@jennifer-shehane
Copy link
Member

I think that will be fine to just have the release triggered when the other things merge

@cypress-app-bot
Copy link

🎉 This PR is included in version 2.15.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

@MikeMcC399
Copy link
Collaborator

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

Successfully merging this pull request may close these issues.

7 participants