sonarjs/no-code-after-done
eslint-plugin-sonarjs
No autofixRecommended
eslint.config.js
export default [
{
rules: {
"sonarjs/no-code-after-done": "error",
},
},
];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
// …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();
// …Examples from the ESLint documentation.
sonarjs/no-code-after-done
eslint-plugin-sonarjs
export default [
{
rules: {
"sonarjs/no-code-after-done": "error",
},
},
];Oxlint does not have this rule.
Biome does not have this rule.
Collection size and array length comparisons should make sense
"switch" statements should not contain non-case labels
Regular expressions should not contain control characters
Constructors should not contain asynchronous operations