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

**URL:** <https://community.sonarsource.com/t/s2004-sonarjs-no-nested-functions-triggers-in-describe-it-test-files/131292>\
**Category:** Report False-positive / False-negative...\
**Tags:** javascript\
**Created:** [November 29, 2024, 8:39am UTC](https://community.sonarsource.com/t/s2004-sonarjs-no-nested-functions-triggers-in-describe-it-test-files/131292 "2024-11-29T08:39:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![DamienCassou](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/damiencassou/32/60028_2.png) [@DamienCassou](https://community.sonarsource.com/u/DamienCassou)\
**Post date:** [November 29, 2024, 8:39am UTC](https://community.sonarsource.com/t/s2004-sonarjs-no-nested-functions-triggers-in-describe-it-test-files/131292/1 "2024-11-29T08:39:28Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![ilia](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/ilia/32/33594_2.png) [@ilia](https://community.sonarsource.com/u/ilia)\
**Post date:** [December 6, 2024, 8:18am UTC](https://community.sonarsource.com/t/s2004-sonarjs-no-nested-functions-triggers-in-describe-it-test-files/131292/4 "2024-12-06T08:18:11Z")

</div>

Hello Damien,

Thank you for the feedback.

I have opened a ticket to track this issue: [JS-486](https://sonarsource.atlassian.net/browse/JS-486).

---

<div class="post-metadata">

**Author:** ![fniessink](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/fniessink/32/110_2.png) [@fniessink](https://community.sonarsource.com/u/fniessink)\
**Post date:** [November 11, 2025, 3:48pm UTC](https://community.sonarsource.com/t/s2004-sonarjs-no-nested-functions-triggers-in-describe-it-test-files/131292/7 "2025-11-11T15:48:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pshem](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/pshem/32/42237_2.png) [@pshem](https://community.sonarsource.com/u/pshem)\
**Post date:** [November 12, 2025, 9:07am UTC](https://community.sonarsource.com/t/s2004-sonarjs-no-nested-functions-triggers-in-describe-it-test-files/131292/8 "2025-11-12T09:07:33Z")

</div>

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](https://jestjs.io/docs/api#each)

---

<div class="post-metadata">

**Author:** ![7ph](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/7ph/32/68905_2.png) [@7ph](https://community.sonarsource.com/u/7ph)\
**Post date:** [January 12, 2026, 11:31am UTC](https://community.sonarsource.com/t/s2004-sonarjs-no-nested-functions-triggers-in-describe-it-test-files/131292/9 "2026-01-12T11:31:26Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![pshem](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/pshem/32/42237_2.png) [@pshem](https://community.sonarsource.com/u/pshem)\
**Post date:** [February 9, 2026, 9:18am UTC](https://community.sonarsource.com/t/s2004-sonarjs-no-nested-functions-triggers-in-describe-it-test-files/131292/10 "2026-02-09T09:18:22Z")

</div>

Glad this got implemented! Which version of SonarQube server will the improvements be released? 2026.1 or 2026.2?

---

<div class="post-metadata">

**Author:** ![ganncamp](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/ganncamp/32/1261_2.png) [@ganncamp](https://community.sonarsource.com/u/ganncamp)\
**Post date:** [February 9, 2026, 2:42pm UTC](https://community.sonarsource.com/t/s2004-sonarjs-no-nested-functions-triggers-in-describe-it-test-files/131292/11 "2026-02-09T14:42:46Z")

</div>

Hi,

This fix is in the current versions of SonarQube: Server 2026.1 LTA, and Community Build 26.2.

&nbsp;  
HTH,  
Ann
