12

I am using TypeScript and Jest and have my tests next to my source files. e.g:

  • someDir
    • someCode.ts
    • someCode.spec.ts

When I try and import the text-report.xml (which looks to be fine and matches the format), I get an error saying:

'Line X report refers to a file which is not configured as a test file: /someDir/someCode.spec.ts'

What configuration do I need in in the Sonarqube properties so that it understand which files are tests and which are source?

skyboyer
  • 22,209
  • 7
  • 57
  • 64
Jareth
  • 431
  • 1
  • 3
  • 16
  • In the settings for the project in sonarqube, what is the pattern you have given for test files? Could perhaps also be given command line when running the analysis. – fredrik Aug 12 '19 at 18:34
  • 3
    Have a look at [this](https://stackoverflow.com/questions/47913557/sonarqube-with-jest-unit-tests) for some hints. – fredrik Aug 12 '19 at 18:36

4 Answers4

12

Seems it doesn't detect files in sub-folders using ".". Only way I was able to get it working was to list all of the folders.

sonar.sources=helpers,managers,routes,schemas,types
sonar.tests=helpers,managers,routes,schemas,types
sonar.exclusions=**/*.js,test-data,dist,coverage
sonar.test.inclusions=**/*.spec.ts
sonar.testExecutionReportPaths=test-report.xml
sonar.javascript.lcov.reportPaths=coverage/lcov.info
patyx
  • 324
  • 1
  • 3
  • 17
Jareth
  • 431
  • 1
  • 3
  • 16
3

You can try

sonar.sources = **/someDir/**/*
sonar.tests = **/someDir/*
//or
sonar.sources = **/someDir/*
sonar.tests = **/someDir/*

Depending on if you have any sub-directories. Might be a better alternative to listing all locations for both tests and sources.

ttoshev
  • 136
  • 7
  • `sonar.sources` and `sonar.tests` are not compatible with wildcards. "The entries in the list are simple paths. Wildcards (*, **, and ?) are not allowed." https://docs.sonarqube.org/latest/project-administration/narrowing-the-focus/#file-exclusion-and-inclusion – patyx May 15 '23 at 17:55
2

For me even after adding sonar.test.inclusions=**/*-spec.js , test reports were not coming. It worked after adding sonar.tests=.(same as sonar.sources)

RAVI SINGH
  • 379
  • 2
  • 7
0

A bad value in the sonar.test.inclusions= setting in the sonar.properties (or whatever SQ filename you are using for properties) file is what caused the error for me. Sonar needs that setting to identify the test files.

You can use something like:

sonar.test.inclusions=src/__test__/**/*.test.ts,src/**/*.spec.ts

Dmitri R117
  • 2,502
  • 23
  • 20