# no-code-after-done

Rule `sonarjs/no-code-after-done`. Tests should not execute any code after "done()" is called

- 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: #1246 by reach, #547 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `sonarjs/no-code-after-done` | yes | no | package `eslint-plugin-sonarjs` | [docs](https://rules.sonarsource.com/javascript/RSPEC-6079/) |

## Code examples

### Incorrect: the rule reports this

```js
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
// …
```

### Correct: the rule accepts this

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

## Enable it

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

```js
export default [
  {
    rules: {
      "sonarjs/no-code-after-done": "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/no-clear-text-protocols](https://lintradar.dev/rules/sonarjs/no-clear-text-protocols.md): Clear-text protocols should not be used
- [sonarjs/no-collection-size-mischeck](https://lintradar.dev/rules/sonarjs/no-collection-size-mischeck.md): Collection size and array length comparisons should make sense
- [sonarjs/no-case-label-in-switch](https://lintradar.dev/rules/sonarjs/no-case-label-in-switch.md): "switch" statements should not contain non-case labels
- [sonarjs/no-control-regex](https://lintradar.dev/rules/sonarjs/no-control-regex.md): Regular expressions should not contain control characters
- [sonarjs/no-async-constructor](https://lintradar.dev/rules/sonarjs/no-async-constructor.md): Constructors should not contain asynchronous operations
- [sonarjs/no-dead-store](https://lintradar.dev/rules/sonarjs/no-dead-store.md): Unused assignments should be removed

---

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