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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/interfaces/common.js
Expand Up @@ -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.

errors.deprecate(
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
'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?

suite.fullTitle() +
'" returned a Promise. ' +
'Asynchronous suites are not supported, use a ' +
'synchronous callback instead.'
);
}
suites.shift();
} else if (typeof opts.fn === 'undefined' && !suite.pending) {
throw createMissingArgumentError(
Expand Down
@@ -0,0 +1,3 @@
'use strict';

describe('a suite with an async callback', async function () {});
21 changes: 21 additions & 0 deletions test/integration/suite.spec.js
Expand Up @@ -72,3 +72,24 @@ describe('suite returning a value', function () {
);
});
});

describe('suite w/async callback', function () {
it('should print a helpful deprecation message when a callback for suite is async', function (done) {
run(
'suite/suite-async-callback.fixture.js',
args,
function (err, res) {
if (err) {
return done(err);
}
expect(
res.output,
'to match',
/Asynchronous suites are not supported, use a synchronous callback instead./
);
done();
},
{stdio: 'pipe'}
);
});
});