False positive - S2699 - Add at least one assertion to this test case

Hello,

Suddenly since last weekend, we are facing false positives for rule S2699 (Add at least one assertion to this test cas) on our Jest tests (Angular Project 15.2, Mono Repo NX 15.8).
The tests contain valid assertions but Sonar is rising a code smell.

ALM : Azure DevOps

Regards,
Hugo

1 Like

Hey there.

Thanks for the report. Can you provide a code snippet that reproduces the false-positive?

nothing fancy

  beforeEach(() => {
    TestBed.configureTestingModule({
// ...
    });
    fixture = TestBed.createComponent(MyComponent);
    component = fixture.componentInstance;
    mockStore = TestBed.inject(MockStore);
  });

  it('should create', () => {
    fixture.detectChanges();
    expect(component).toBeTruthy();
  });

does it help ?

Thanks for you help

Hello,
Maybe a public repository can help too:

Hello Hugo and Pascal and thank you for your feedback!

Currently, the rule logic is as described in its definition:

This rule raises an issue when the assertion library chai or sinon is imported but no assertion is used in a test.

For Hugo’s case, you import sinon, but use another lib (vitest) for assertions, hence the FP. We could add vitest in to the list of supported libraries.

@HugoMercierYuc, Could you provide the imports that you are using in your file?

Best,
Ilia

Hello,

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { MockStore, provideMockStore } from '@ngrx/store/testing';
import { MockComponent, ngMocks } from 'ng-mocks';
import { spy } from 'sinon';
// private stuff

but your brillant remarks give me any idea, maybe I have a conflict between Jest and Cypress in my monorepo.

I will check

Thanks a lot for your help.

Hugo

Hello @HugoMercierYuc,

Thank you for the info. Do you require more assistance?

I have opened a ticket to support vitest here: Fix FP S2699 (`assertions-in-tests`): Add support for `vitest` library · Issue #4195 · SonarSource/SonarJS · GitHub

Best,
Ilia

Hello,

We can close this issue, I’m 99% sure that it was due to cypress and jest type conflict.

Thanks for your help
Hugo

Hello,

I would like to re-open the issue, because I manage to isolate the problem in a public repository :
https://github.com/HugoMercierYuc/sonar-jest-sinon

This is a default NX repository for Angular with Jest, when you import sinon in your unit test file, you get the false-positive Sonar issue.

  • open src/app/app.component.spec.ts
  • the 3 unit tests raising a Sonar issue S2699
  • if you remove the third test and remove the line import { spy } from ‘sinon’;, the Sonar issue S2699 is not raised anymore

Thanks for your help
Hugo