Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FP S2699 (Tests should include assertions): global expect() aren't detected #4459

Closed
ilia-kebets-sonarsource opened this issue Dec 8, 2023 · 0 comments · Fixed by #4628
Labels
must-have type: false positive Issue is reported when it should NOT be
Milestone

Comments

@ilia-kebets-sonarsource
Copy link
Contributor

Assertion functions such as expect() are not detected when available globally.

This issue was raised in https://community.sonarsource.com/t/false-positive-s2699-add-at-least-one-assertion-to-this-test-case-with-repro/103847

Reproducer:

link

import { TestBed } from '@angular/core/testing';
import { AppComponent } from './app.component';
import { NxWelcomeComponent } from './nx-welcome.component';
import { RouterTestingModule } from '@angular/router/testing';
import { spy } from 'sinon';

describe('AppComponent', () => {
  beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [AppComponent, NxWelcomeComponent, RouterTestingModule],
    }).compileComponents();
  });

  it('should render title', () => {
    const fixture = TestBed.createComponent(AppComponent);
    fixture.detectChanges();
    const compiled = fixture.nativeElement as HTMLElement;
    expect(compiled.querySelector('h1')?.textContent).toContain('Welcome org');
  });

  it(`should have as title 'org'`, () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;
    expect(app.title).toEqual('org');
  });

  it('should not call action', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;
    const spyFn = spy(app, 'action');
    fixture.detectChanges();
    expect(spyFn.calledOnce).toBeFalsy();
  });
});
@ilia-kebets-sonarsource ilia-kebets-sonarsource added the type: false positive Issue is reported when it should NOT be label Dec 8, 2023
@ilia-kebets-sonarsource ilia-kebets-sonarsource added this to the 10.13 milestone Mar 26, 2024
ericmorand-sonarsource added a commit to ericmorand-sonarsource/SonarJS that referenced this issue Mar 26, 2024
ericmorand-sonarsource added a commit to ericmorand-sonarsource/SonarJS that referenced this issue Mar 26, 2024
vdiez pushed a commit that referenced this issue Mar 26, 2024
ericmorand-sonarsource added a commit to ericmorand-sonarsource/SonarJS that referenced this issue Mar 26, 2024
@ericmorand-sonarsource ericmorand-sonarsource removed their assignment Mar 26, 2024
@ericmorand-sonarsource ericmorand-sonarsource removed their assignment Apr 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
must-have type: false positive Issue is reported when it should NOT be
Projects
None yet
2 participants