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: deprecation message when an async callback is used in a Suite #4921

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

alcuadrado
Copy link

I'm sending this PR after having proposed this feature in #4920, getting some positive reactions, and researching the history of a similar feature that was removed

Description of the Change

This PR introduces a change that prints a depreciation message if an async callback is used in a Suite.

Alternate Designs

v6.0.0 introduced a similar feature, but it printed a deprecation warning if a Suite returned any value other than undefined, which was not practical in some cases (see #3744), so it was removed.

This PR is a specialization of that feature that prints a warning when Promises are returned instead.

Also, I originally proposed throwing a fatal error, but that would be a breaking change, so I changed it to a deprecation message. This same criteria was applied to the previous version of this feature.

Why should this be in core?

I don't think this can be implemented outside of core.

As this PR benefits users who don't have a clear understanding of how a Suite works, taking this functionality out of core will have much lower, as there wouldn't be a clear path for those users to discover how to enable it.

Benefits

Many users don't realize that Suite's callbacks can't be async. In some cases, they get away with async callbacks, which further increases the confusion.

This PR is intended to clarify that confusion.

Possible Drawbacks

I don't think this version of the feature has any serious drawback.

It may print deprecation warnings for people who were using async callbacks in Suites with some success (and a ton of luck), but those users were already at high risk of their code breaking. This change is intended to help them.

Applicable issues

Fixes #4920
semver-patch
(copying the semver criteria of #3550)

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Sep 13, 2022

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: alcuadrado / name: Patricio Palladino (5ba820b)

Co-authored-by: Franco Victorio <victorio.franco@gmail.com>
@juergba
Copy link
Member

juergba commented Sep 20, 2022

@alcuadrado thank you, I will review within the next few days.

I certainly don't agree with the type of warning (deprecation), since we have never offered a feature "asynchronous describe".

@juergba
Copy link
Member

juergba commented Sep 20, 2022

@wdavidw are you still working with Mocha and CoffeeScript?
I don't understand your comment in #3744:

One way to please everyone is to detect if the return value found in the suite is not the same as the one returned by it.

Does this PR work with CoffeeScript? What happens if the last it in a describe returns a Promise?

@alcuadrado
Copy link
Author

I certainly don't agree with the type of warning (deprecation), since we have never offered a feature "asynchronous describe".

I copied this from the previous implementation. I guess the reasoning behind that is that some users are getting away with async callbacks now, and a fatal error can be seen as breaking by them.

@juergba juergba added area: usability concerning user experience or interface semver-minor implementation requires increase of "minor" version number; "features" labels Sep 27, 2022
@juergba juergba added this to the next milestone Sep 27, 2022
@coveralls
Copy link

Coverage Status

Coverage increased (+0.003%) to 94.329% when pulling 9b338f6 on alcuadrado:issue/4920 into 41567df on mochajs:master.

Copy link
Member

@juergba juergba left a comment

Choose a reason for hiding this comment

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

@alcuadrado I'm quite sure this change will trigger deprecation warnings to CoffeeScript users in certain circumstances. Anyway this PR is much softer than the original proposal and there is a work-around by adding a return statement to the suites. Otherwise they will have to live with a single deprecation warning.

Could you please do some manual browser testing, since we don't run our integration tests on browsers. And confirm the outcome, thank you.

lib/interfaces/common.js Show resolved Hide resolved
const suiteResult = opts.fn.call(suite);
if (suiteResult instanceof Promise) {
errors.deprecate(
'Suite "' +
Copy link
Member

Choose a reason for hiding this comment

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

Please add "[mocha] Suite ..." to the message as done in other parts of our code. The origin of a warning is clearer that way.

Copy link
Member

Choose a reason for hiding this comment

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

@alcuadrado ping - can you make this change?

@juergba juergba removed this from the next milestone Oct 9, 2022
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

Looks good as a general re-implementation of #3550! Heck, I even think we should go further and fully re-implement to check all truthy values - not just Promises.

Thoughts?

@@ -145,7 +145,16 @@ module.exports = function (suites, context, mocha) {
throw createUnsupportedError('Pending test forbidden');
}
if (typeof opts.fn === 'function') {
opts.fn.call(suite);
const suiteResult = opts.fn.call(suite);
if (suiteResult instanceof Promise) {
Copy link
Member

Choose a reason for hiding this comment

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

Going back to https://github.com/mochajs/mocha/pull/3550/files#diff-28d67f27fa361db19b8a3aa9181d89c5f0d5ae1089167c55f0341675386e3a28R115, I think we no longer need to prioritize supporting CoffeeScript users. The implicit returns that caused #3550 to be reverted in #3759 are not a good idea for the exact reason we saw here: that they unintentionally return values even when those values shouldn't be returned!

Suggested change
if (suiteResult instanceof Promise) {
if (suiteResult !== undefined) {

https://typescript-eslint.io/rules/no-confusing-void-expression is the right direction to go IMO.

@JoshuaKGoldberg JoshuaKGoldberg added the status: waiting for author waiting on response from OP - more information needed label Mar 4, 2024
@JoshuaKGoldberg JoshuaKGoldberg changed the title Deprecation message when an async callback is used in a Suite feat: deprecation message when an async callback is used in a Suite Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: usability concerning user experience or interface semver-minor implementation requires increase of "minor" version number; "features" status: waiting for author waiting on response from OP - more information needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🚀 Feature: Fatal error when async functions are passed to describe and context
5 participants