Ignore certain blocks of code

Hi everybody, I want to ignore specific blocks in my code, is it possible to achieve this?
this is my current configuration at my .properties file:

sonar.projectKey=cuentame-v21234-backend
sonar.organization=cuentame12345test
sonar.host.url=https://sonarcloud.io

# Nombre y versión del proyecto mostrados en la UI de SonarCloud
sonar.projectName=Test Backend
#sonar.projectVersion=1.0

# Ruta relativa al archivo sonar-project.properties. Reemplaza "\" por "/" en Windows.
sonar.sources=.

# Directorios de pruebas
# sonar.tests=src/controllers/test,src/models/test,src/helpers/test,src/utils/test

# Cobertura de código
sonar.javascript.lcov.reportPaths=coverage/lcov.info

# Exclusiones: directorios y archivos a excluir del análisis
sonar.exclusions=node_modules/**,build/**,dist/**,coverage/**,logs/**,docs/**,src/config/**, src/middleware/**, src/constants/**, src/interceptors/**, src/controllers/**, src/schemas/**, src/models/translations/**, src/models/roles/**, src/cron/**, src/routes/**

# Inclusiones opcionales para archivos de prueba
# Esto asegura que solo los archivos de prueba relevantes sean analizados
sonar.test.inclusions=**/*.test.ts,**/*.spec.ts

# Ignorar bloques específicos de código en src/models/clientRHSection.ts
sonar.issue.ignore.block=block1
sonar.issue.ignore.block.block1.filePattern=src/models/**
sonar.issue.ignore.block.block1.beginBlockRegexp=.*//\s*sonar-ignore-start.*
sonar.issue.ignore.block.block1.endBlockRegexp=.*//\s*sonar-ignore-end.*

I even add the regex at the project console…


this is the code I’m testing to ignore…

// sonar-ignore-start
ClientDashboard.addHook('beforeCreate', async (instance: ClientDashboard | any) => {
  // Client validation
  const hasClient: any = await Client.findByPk(instance.clientId, { attributes: ['idClient'] });

  // Evaluate wheater there´s an error to handle
  let errorResponse = handlerErrorResponse(hasClient, 600, 404);

  // If an error exist's
  if (errorResponse.status !== 200) {
    errorResponse.details = 'Client not found';
    throw errorResponse;
  }
  // Dashboard type validation
  const hasDashboardType: any = await DashboardType.findByPk(instance.dashboardTypeId, { attributes: ['idDashboardType'] });

  // Evaluate wheater there´s an error to handle
  let errorResponseDashboardType = handlerErrorResponse(hasDashboardType, 610, 404);

  // If an error exist's
  if (errorResponseDashboardType.status !== 200) {
    errorResponseDashboardType.details = 'Dashboard type not found';
    throw errorResponseDashboardType;
  }
});
// sonar-ignore-end

And always appears as if it’s uncovered

Hi!

Welcome to the community!

You should use the sonar coverage exclusions property to exclude files from coverage metrics:

The patterns you have used are related to “issues” exclusions only, and not coverage.

As far as I know, there is no way to exclude certain blocks from coverage. You can only exclude full files.

Best regards.

That’s right. However, if your coverage tool allows you to exclude certain sections of your code (with some annotation to comment) from coverage, SonarQube will “ignore” those lines.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.