💼 This rule is enabled in the ✅ recommended
config.
Mocha allows you to write asynchronous tests by adding a done
callback to the parameters of your test function.
It is easy to forget calling this callback after the asynchronous operation is done.
Example:
it('should work', function (done) {
fetchData(options, function (error, data) {
expect(error).not.to.be.ok;
expect(data).to.deep.equal({ foo: 'bar' });
// done callback was not called
});
});
In this example the done
callback was never called and test will time out.
This rule checks each FunctionExpression
or ArrowFunctionExpression
inside of it
, it.only
, test
, test.only
, specify
, specify.only
, before
, after
, beforeEach
and afterEach
.
The following patterns are considered warnings:
it('foo', function (done) {});
it('foo', function (done) {
asyncFunction(function (err, result) {
expect(err).to.not.exist;
});
});
before(function (done) {
asyncInitialization(function () {
initialized = true;
});
});
These patterns would not be considered warnings:
it('foo', function (done) {
done();
});
it('foo', function (done) {
asyncFunction(function (err, result) {
expect(err).to.not.exist;
done();
});
});
before(function (done) {
asyncInitialization(function () {
initialized = true;
done();
});
});
This rule supports the following options:
ignoreSkipped
: When set totrue
skipped test cases won’t be checked. Defaults tofalse
.
{
"rules": {
"mocha/handle-done-callback": ["error", { "ignoreSkipped": true }]
}
}
If you don’t write asynchronous tests you can safely disable this rule.