# chai-determinate-assertion

Rule `sonarjs/chai-determinate-assertion`. Chai assertions should have only one reason to succeed

- 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: #1190 by reach, #491 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `sonarjs/chai-determinate-assertion` | yes | no | package `eslint-plugin-sonarjs` | [docs](https://rules.sonarsource.com/javascript/RSPEC-6092/) |

## Code examples

### Incorrect: the rule reports this

```js
const expect = require('chai').expect;

describe("uncertain assertions", function() {
    const throwsTypeError = () => { throw new TypeError() }

    it("uses chai 'expect'", function() {
        expect(throwsTypeError).to.not.throw(ReferenceError) // Noncompliant
        expect({a: 42}).to.not.include({b: 10, c: 20});  // Noncompliant
        expect({a: 21}).to.not.have.property('b', 42); // Noncompliant
        expect({a: 21}).to.not.have.ownPropertyDescriptor('b', {   // Noncompliant
            configurable: true,
            enumerable: true,
            writable: true,
            value: 42,
        });
        expect([21, 42]).to.not.have.members([1, 2]); // Noncompliant

        var myObj = { value: 1 }
        const incThree = () => { myObj.value += 3; };
        const decThree = () => { myObj.value -= 3; };
        const doNothing = () => {};

        expect(incThree).to.change(myObj, 'value').by(3); // Noncompliant
        expect(decThree).to.change(myObj, 'value').by(3); // Noncompliant

        expect(decThree).to.not.increase(myObj, 'value'); // Noncompliant
        expect(incThree).to.not.decrease(myObj, 'value'); // Noncompliant

        expect(doNothing).to.not.increase(myObj, 'value'); // Noncompliant
        expect(doNothing).to.not.decrease(myObj, 'value'); // Noncompliant

        expect(incThree).to.increase(myObj, 'value').but.not.by(1); // Noncompliant
// …
```

### Correct: the rule accepts this

```js
const expect = require('chai').expect;

describe("uncertain assertions", function() {
    const throwsTypeError = () => { throw new TypeError() }

    it("uses chai 'expect'", function() {
        expect(throwsTypeError).to.throw(TypeError)
        expect({a: 42}).to.not.have.any.keys('b', 'c');
        expect({a: 21}).to.not.have.property('b');
        expect({a: 21}).to.not.have.ownPropertyDescriptor('b');
        expect([21, 42]).to.not.include(1).and.not.include(2);

        var myObj = { value: 1 }
        const incThree = () => { myObj.value += 3; };
        const decThree = () => { myObj.value -= 3; };
        const doNothing = () => {};

        expect(incThree).to.increase(myObj, 'value').by(3);
        expect(decThree).to.decrease(myObj, 'value').by(3);

        expect(decThree).to.decrease(myObj, 'value').by(3);
        expect(incThree).to.increase(myObj, 'value').by(3);

        expect(doNothing).to.not.change(myObj, 'value');

        expect(incThree).to.increase(myObj, 'value').by(3);

        let toCheck;
        // Either of the following is valid
        expect(toCheck).to.be.a('string');
        expect(toCheck).to.be.NaN;
        expect(toCheck).to.equal(Infinity);
// …
```

Examples from the ESLint documentation.

## Enable it

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

```js
export default [
  {
    rules: {
      "sonarjs/chai-determinate-assertion": "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/call-argument-line](https://lintradar.dev/rules/sonarjs/call-argument-line.md): Function call arguments should not start on new lines
- [sonarjs/class-name](https://lintradar.dev/rules/sonarjs/class-name.md): Class names should comply with a naming convention
- [sonarjs/block-scoped-var](https://lintradar.dev/rules/sonarjs/block-scoped-var.md): Variables should be used in the blocks where they are declared
- [sonarjs/code-eval](https://lintradar.dev/rules/sonarjs/code-eval.md): Dynamic code execution should not use user-controlled data
- [sonarjs/bitwise-operators](https://lintradar.dev/rules/sonarjs/bitwise-operators.md): Bitwise operators should not be used in boolean contexts
- [sonarjs/comma-or-logical-or-case](https://lintradar.dev/rules/sonarjs/comma-or-logical-or-case.md): Comma and logical OR operators should not be used in switch cases

---

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