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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/interfaces/common.js
Original file line number Diff line number Diff line change
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 !== undefined) {
errors.deprecate(
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
'[mocha] Suite "' +
suite.fullTitle() +
'" returned a Promise. ' +
Copy link
Member

Choose a reason for hiding this comment

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

A Promise

Now that it's !== undefined above, this'll need to be updated.

'Asynchronous suites are not supported, use a ' +
Copy link
Member

Choose a reason for hiding this comment

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

Asynchronous

...and this too.

'synchronous callback instead.'
);
}
suites.shift();
} else if (typeof opts.fn === 'undefined' && !suite.pending) {
throw createMissingArgumentError(
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Copy link
Member

Choose a reason for hiding this comment

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

[Testing] This has a very clean + good unit test for the async (returning a Promise) case. But since the common.js change now is the more general !== undefined check, could you also add a unit test for a non-undefined, non-Promise value?

Original file line number Diff line number Diff line change
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'}
);
});
});
Loading