# test-check-exception

Rule `sonarjs/test-check-exception`. Tests should check which exception is thrown

- Source: sonarjs
- Technologies: JavaScript
- Kind: style
- Needs type information: no
- Autofix: no
- Reach: 6.2M weekly downloads of presets that enable it
- Trend: +62.1% last quarter
- Rank: #1373 by reach, #674 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `sonarjs/test-check-exception` | yes | no | package `eslint-plugin-sonarjs` | [docs](https://rules.sonarsource.com/javascript/RSPEC-5958/) |

## Code examples

### Incorrect: the rule reports this

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

### Correct: the rule accepts this

```js
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.

## Enable it

### ESLint: `eslint.config.js`

```js
export default [
  {
    rules: {
      "sonarjs/test-check-exception": "error",
    },
  },
];
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [SonarJS recommended](https://lintradar.dev/presets/sonarjs-recommended.md) | ESLint | `eslint-plugin-sonarjs` | 230 | 6.2M | +62.1% |

## Related rules

- [sonarjs/table-header-reference](https://lintradar.dev/rules/sonarjs/table-header-reference.md): Table cells should reference their headers
- [sonarjs/testing-library-prefer-query-by-disappearance](https://lintradar.dev/rules/sonarjs/testing-library-prefer-query-by-disappearance.md): Testing Library disappearance waits should use non-throwing queries
- [sonarjs/table-header](https://lintradar.dev/rules/sonarjs/table-header.md): Tables should have headers
- [sonarjs/testing-library-query-assertion](https://lintradar.dev/rules/sonarjs/testing-library-query-assertion.md): Testing Library queries should match presence assertions
- [sonarjs/synchronous-suite-callback](https://lintradar.dev/rules/sonarjs/synchronous-suite-callback.md): Tests should not be registered asynchronously in suite callbacks
- [sonarjs/todo-tag](https://lintradar.dev/rules/sonarjs/todo-tag.md): Track uses of "TODO" tags

---

Web page: https://lintradar.dev/rules/sonarjs/test-check-exception. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
