2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-04-28 16:49:39 +02:00
The ``++done++`` callback is used to inform Mocha when an asynchronous test ends. Exceptions thrown after ``++done++`` (with or without parameters) is called are not handled in a consistent manner. Sometimes they will be correctly handled, but they might as well be assigned to a different test, no test at all, or even be completely ignored. Even when it works as expected this will be a source of confusion for other developers. Thus no code should be executed after ``++done++`` is called.
This rule raises an issue when some code is executed after a call to ``++done++``.
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,javascript]
2021-04-28 16:49:39 +02:00
----
const expect = require("chai").expect;
const fs = require("fs");
describe("Code is executed after Done", function() {
it("Has asserts after done()", function(done) {
try {
expect(1).toEqual(2);
} catch (err) {
done();
// This assertion will be ignored and the test will pass.
expect(err).to.be.an.instanceof(RangeError); // Noncompliant
}
});
it("Throws an error some time after done()", function(done) {
fs.readFile("/etc/bashrc", 'utf8', function(err, data) {
done();
setTimeout(() => { // Noncompliant
// This assertion error will not be assigned to any test.
// Developers will have to guess which test failed.
expect(data).to.match(/some expected string/);
}, 3000);
});
});
it("Has code after done(err)", function(done) {
try {
throw Error("An error");
} catch (err) {
done(err);
}
fs.readFile("/etc/bashrc", 'utf8', function(err, data) { // Noncompliant
// This assertion error will be assigned to "Other test".
expect(data).to.match(/some expected string/);
});
});
it("Other test", function(done) {
done()
});
});
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,javascript]
2021-04-28 16:49:39 +02:00
----
const expect = require("chai").expect;
const fs = require("fs");
describe("Code is executed after Done", function() {
it("Has asserts after done()", function(done) {
try {
expect(1).toEqual(2);
} catch (err) {
expect(err).to.be.an.instanceof(RangeError);
done();
}
});
it("Throws an error some time after done()", function(done) {
fs.readFile("/etc/bashrc", 'utf8', function(err, data) {
setTimeout(() => {
expect(data).to.match(/some expected string/);
done();
}, 3000);
});
});
it("Has code after done(err)", function(done) {
try {
throw Error("An error");
} catch (err) {
return done(err);
}
fs.readFile("/etc/bashrc", 'utf8', function(err, data) {
// This assertion error will be assigned to "Other test".
expect(data).to.match(/some expected string/);
done();
});
});
it("Other test", function(done) {
done()
});
});
----
2021-04-28 18:08:03 +02:00
2021-09-20 15:38:42 +02:00
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Move this code before the call to "done".
=== Highlighting
Primary: The first line of code which can be executed after a call to "done()"
Secondary: every call to "done() which can be executed before this code
message: 'Call to "done()".'
2021-09-20 15:38:42 +02:00
endif::env-github,rspecator-view[]