How does the exclusions work?

Hi, I’m testing the exclusion logic using a Sonarqube 10.5.1 community Docker image. The Sonar Scanner version is 5.0.1. Our test files are located in the same folders as the source file and we’d like to exclude them from the scans. Below are some examples how the source and test files are named:



I tried setting sonar.exclusions to **/*test.ts, **/*test.js and **/*.test.ts, **/*.test.js, but in both cases, I can still see the test files shown in the scan results page.

What is the proper way to exclude the test files from the scan? Any suggestion will be greatly appreciated. Thanks for your time.

Hey ther.e

I’d expect to see these files excluded with these exclusions.


Do you see the exclusions represented in the scanner logs?

[**INFO**] 11:24:54.473 Project configuration:
[**INFO**] 11:24:54.473 Excluded sources: **/*.test.ts

Hi Colin, thanks for your reply. Please see the logs below showing the excluded sources.

INFO: Excluded sources: /.test.ts, **/*.test.js

The files with names ending in “.test.ts” were still included in the scan as shown.

Please let me know if I miss anything. Thanks for your help.

Thanks. Are these files being indexed as test files (which would seem expected)? In that case, sonar.test.exclusions would need to be used (but if your test files are being indexed as test, there’s nothing wrong with that, and they aren’t even counting towards your license).

You can see how individual files are indexed by turning on DEBUG analysis logs (sonar-scanner -X or setting -Dsonar.verbose=true as an analysis parameter)

Thanks, Colin.
I specify the exclusion options as below:
"sonar.exclusions": "*/*.test.ts, **/*.test.js",
"sonar.test.exclusions": "*/*.test.ts, **/*.test.js",
"sonar.verbose": "true"

I captured some of the log messages below:

20:26:22.598 DEBUG: 'packages/alchemy-theme/src/helpers/breakpoint.test.ts' indexed as test with language 'ts'
20:26:22.600 DEBUG: 'packages/alchemy-theme/src/helpers/color.test.ts' generated metadata as test with charset 'UTF-8'**
20:26:22.600 DEBUG: 'packages/alchemy-theme/src/helpers/color.test.ts' indexed as test with language 'ts'
20:27:42.593 DEBUG: Cache entry created for key 
20:27:42.593 DEBUG: Cache strategy set to 'WRITE_ONLY' for file 'packages/alchemy-theme/src/helpers/color.test.ts' as current analysis requires all files to be analyzed**
20:27:42.593 DEBUG: Analyzing file: file:///home/jenkins/alchemy/packages/alchemy-theme/src/helpers/color.test.ts****
20:27:42.596 DEBUG: Analyzing file "/home/jenkins/alchemy/packages/alchemy-theme/src/helpers/color.test.ts" with linterId "default" 20:27:42.609 DEBUG: Parsing /home/jenkins/alchemy/packages/alchemy-theme/src/helpers/color.test.ts with @typescript-eslint/parser**
20:27:42.612 DEBUG: Cache entry created for key 'jssecurity:ucfgs:SEQ:' containing 0 file(s)**
20:27:42.613 DEBUG: Cache entry created for key 'jssecurity:ucfgs:JSON:'**
20:27:42.614 DEBUG: Cache entry created for key 'js:filemetadata:'**
20:28:24.290 DEBUG: File already analyzed: '/home/jenkins/alchemy/packages/alchemy-theme/src/helpers/color.test.ts'. Check your project configuration to avoid files being part of multiple projects.

From the message, it appears the test files are still analyzed as sources?

This should be **/*.test.ts to traverse all directories. Like you’ve done for **/*.test.js*

That’s a dumb mistake I made. :sweat_smile:
Thanks for pointing it out. The scan worked as expected after I fixed the syntax. Thank you so much for your help.

1 Like