Configuring Sonar analysis with co-located test files

I’ve used sonarqube in the past with Java projects. I’ve recently move to a new typescript project.

The project has been setup with unit tests co-located with the files under test, and integration tests in a separate spec folder, e.g.

app/app.ts
app/app.spec.ts
spec/integration.spec.ts

I’d like to include the test files for general analysis, but not for coverage which I believe is basically what the sonar.sources and sonar.tests settings are for.

However I can’t seem to find a way to configure this as these settings only accept folder paths, not filtering by file pattern (i.e. test files are *.spec.ts)

For now my work around is just to exclude all the test files (using sonar.exclusions=**/*.spec.ts) but this is not ideal.

Hi @egmacke,

You are correct, sonar.tests only supports a comma seperated list of directories.

We currently assume test files are in a separate folder. Could you explain why you made the choice to have test files and main source code in the same folder?

@TomVanBraband

We made the decision to co-locate our source and unit test files for a couple of reasons.

  1. It makes it very easy to see where there are missing tests (this was before we started using proper coverage analysis tools - but is still very useful during development and code review)
  2. working on a reasonably large code base, it’s far easier to switch between test and source files if they’re co-located rather than having to jump between two nested folder trees
  3. As we’re using javascript/typescript we can very simply exclude the test files from our build using glob patterns in tsconfig.json so until we moved to sonarcloud there has been no downside to this approach.

While I understand that having test files separate is more normal - I’m sure there are other developers who co-locate their test files. It would be great if sonarcloud could support glob patterns for sonar.source and sonar.test

Is there any possibility of this feature being added to sonarcloud?

This is how Angular and many other modern TypeScript/JavaScript projects are setup by default. I shouldn’t be asked to re-architect my entire project against the framework design just to use SonarCloud.

For the project structure described above, the following configuration should help:

sonar.sources=app
sonar.tests=app
sonar.test.inclusions=**/*.spec.ts
1 Like