sonarjs/test-check-exception
eslint-plugin-sonarjs
No autofixRecommended
eslint.config.js
export default [
{
rules: {
"sonarjs/test-check-exception": "error",
},
},
];const expect = require("chai").expect;
const fs = require("fs");
describe("exceptions are not tested properly", function() {
const funcThrows = function () { throw new TypeError('What is this type?'); };
const funcNoThrow = function () { /*noop*/ };
it("forgot to pass the error to 'done()'", function(done) {
fs.readFile("/etc/zshrc", 'utf8', function(err, data) {
try {
expect(data).to.match(/some expected string/);
} catch (e) { // Noncompliant
// Either the exception should be passed to done(e), or the exception should be tested further.
done();
}
});
});
it("does not 'expect' a specific exception", function() {
expect(funcThrows).to.throw(); // Noncompliant
// Error is not precise enough
expect(funcThrows).to.throw(Error); // Noncompliant
});
});const expect = require("chai").expect;
const { AssertionError } = require('chai');
const fs = require("fs");
describe("exceptions are tested properly", function() {
const funcThrows = function () { throw new TypeError('What is this type?'); };
const funcNoThrow = function () { /*noop*/ };
it("forgot to pass the error to 'done()'", function(done) {
fs.readFile("/etc/zshrc", 'utf8', function(err, data) {
try {
expect(data).to.match(/some expected string/);
} catch (e) {
expect(e).to.be.an.instanceof(AssertionError);
done();
}
});
});
it("does not 'expect' a specific exception", function() {
expect(funcThrows).to.throw(TypeError);
expect(funcNoThrow).to.not.throw(Error, /My error message/);
});
});Examples from the ESLint documentation.
sonarjs/test-check-exception
eslint-plugin-sonarjs
export default [
{
rules: {
"sonarjs/test-check-exception": "error",
},
},
];Oxlint does not have this rule.
Biome does not have this rule.
Table cells should reference their headers
Testing Library disappearance waits should use non-throwing queries
Testing Library queries should match presence assertions
Tests should not be registered asynchronously in suite callbacks