S2004 (`sonarjs/no-nested-functions`) triggers in describe()/it() test files

The sonarjs/no-nested-functions of eslint-plugin-sonarjs 2.0.4 complains about code like:

import { describe, expect, it } from "vitest";

describe("Foo", () => {
  describe("visiting", () => {
    describe("accept()", () => {
      it("throws an exception", () => {
        expect(() => "id".accept("bar")).toThrowError("some error");
      });
    });
  });
});

The error message is:

Refactor this code to not nest functions more than 4 levels deep

In such test suites, functions are a way to group tests and don’t add to the complexity of the file. I would advocate for deactivating this rule in such files or raising the limit.

1 Like

Hello Damien,

Thank you for the feedback.

I have opened a ticket to track this issue: JS-486.

We got bitten by this same issue, but for typescript:S2004. Are there any plans to work on the ticket?

We’re running into typescript:S2004 in tests as well, on Sonar Qube Server Data Centre Edition v2025.2 (105476). In a parametrised test, you trigger this very easily. We don’t want to disable the rule, as it is relevant in non-test code. Current company example project excludes all typescript tests from sonar, and this is one of the major reasons.

We’re using jest, it also uses describe, it and expect. There is also a parametrised test syntax with it.each Globals · Jest

Hi y’all, we just got hit by the same false positive today and I’ve came across this thread and noticed that you fixed this last week.

Thank you for fixing this!

1 Like