It is not good enough to test if an exception is raised, without checking which exception it is. Such tests will not be able to differentiate the expected exception from an unexpected one. They should instead validate the exception message and/or type.

This rule raises an issue in the following cases:

Rule doesn’t raise an issue when assertion is negated using not, it such case the exception doesn’t need to be specific.

Noncompliant Code Example

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
    });
});

Compliant Solution

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/);
    });
});