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

  • ALM used : Azure DevOps
  • CI system used : Azure DevOps
  • Languages of the repository : typescript
  • Error observed : False positive - S2699 - Add at least one assertion to this test case
  • Steps to reproduce :
    GitHub - 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.

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

new ticket after my first one was closed : False positive - S2699 - Add at least one assertion to this test case

Thanks for your help
Hugo

1 Like

hello,
Any update on that ?
Thanks a lot for your help
Hugo

Hello again, Hugo!

Thank you for providing this feedback, I have opened an issue on SonarJS.

Best,
Ilia

2 Likes

Thanks. We are getting the same issue with import of sinon where the assertion of “expect” global on Jest not detected.

Hi @ken.bak,

Welcome to the community!

Could you provide a reproducer, either here or in a new thread?

 
Thx,
Ann

Hi G Ann Campbell, the code was fine for years, but Sonarcloud started reporting these false positives since September/October 2023. Now we have 130+ code smells with the same issue. See code example below:

import { createStructuredSelector } from 'reselect';
import sinon from 'sinon';
import * as dummyViewSelector from 'dummy-view-selector';
describe('Modification using dummyViewSelector', () => {
  beforeEach(() => {
    sinon.spy(dummyViewSelector, 'default');
  });
  afterEach(() => {
    dummyViewSelector.default.restore();
  });
  describe('dummy view selector', () => {
    const dummyViewSelector =
      require('dummy-view-selector').select;
    const SELECTORS = [
      'dummyConfiguration',
      'isLoading',
    ];
    it('should call our dummy selector with the original selectors supplied', () => {
// Sonarcloud reports code smell for the line above - Add at least one assertion to this test case.
      expect(dummyViewSelector.default.called).toBeFalsy();
      dummyViewSelector();
      expect(dummyViewSelector.default.calledOnce).toBeTruthy();
      expect(dummyViewSelector.default.firstCall.args).toEqual([
        createStructuredSelector,
        expect.objectContaining(
          SELECTORS.reduce(
            (_, name) => ({
              ..._,
              [name]: expect.any(Function),
            }),
            {},
          ),
        ),
      ]);
    });
  });

Hi,

Thanks for the examples. I’ll re-flag this for the language experts.

 
Ann

2 Likes

Hello @ken.bak,

Appreciate your feedback.

The rule you mentioned underwent some modifications recently, specifically to accommodate Sinon.js and Vitest assertion APIs.

It appears these changes resulted in false positives in your case, particularly where you’re utilizing the Sinon.js API for spying purposes. Additionally, your code uses the Jest assertion API, which the current rule doesn’t recognize.

I’ve opened a ticket to address this false positive promptly.

Best regards,

2 Likes