Typescript Test Code consuming Lines of Code (LOC)

  • ALM used Azure DevOps
  • CI system used Azure DevOps
  • Languages of the repository: typescript, html, javascript, css

It’s said in Lines of code | SonarCloud Docs that Test Code should not be counted.

However, when looking to my project Source in Sonarcloud, my .spec.ts files are counted:

For .NET/msbuild based analyses (another project) it does identify Test Code apparently based on my .NET projects.

It also fails for javascript based tests where extension we use is .spec.js .

Are we missing any parameter to make such files be considered Test Code?

I found out about sonar.test and sonar.test.inclusions.

That did decrease the overall LOC, but when checking in detail, the .spec.ts files are now gone from the Code area in Sonarcloud and I am not sure if they were analysed at all. So it still behaves diferenttly than what SonarScanner for .NET does.

When using msbuild, .NET test project files still appear in Code section, but without LOC.


Welcome to the community!

Yes, .NET analysis does behave differently.

Can you share your analysis log?

The analysis / scanner log is what’s output from the analysis command. Hopefully, the log you provide - redacted as necessary - will include that command as well.

This guide will help you find them.



Looking to the logs I can see the following:

The configuration (setup by configure task) is:

    "sonar.host.url": "https://sonarcloud.io/",
    "sonar.login":  *** ,
    "sonar.organization": "Company",
    "sonar.projectKey": "Company_appWebClient",
    "sonar.projectVersion": "1.0",
    "sonar.sources": "d:\\agent\\251\\s",
    "sonar.branch.name": "tests/sonarcloud-test-code",
    "sonar.scanner.metadataFilePath": "d:\\agent\\_temp\\sonar\\20230602.14\\55bca54d-4d92-e3ac-4f91-734a98f4d508\\report-task.txt",
    "sonar.cs.vstest.reportsPaths": "d:\\agent\\_temp\\TestResults\\*.trx",
    "sonar.coverage.exclusions": "**/Company*Migrations/**,**/Company*Test/**,**/Company*Tests/**,**/environments/environment*.ts,**/*.conf.js,**/src/main.ts,**/lib/http-services/*.services.ts,**/src/polyfills.ts,**/src/test.ts,**/lib/http-services/*.services.ts,**/lib/enumerations/*.enum.ts,**/lib/models/*.model.ts,**/e2e/src/app.e2e-spec.ts,**/e2e/src/app.po.ts,**/*.spec.ts,**/app-config.ts,**/*.interface.ts,**/Company-app-app/src/app/testing/**",
    "sonar.exclusions": "**/coverage-include-all.spec.ts,**/TestResultJunit/*.xml,no_extra_exclusion_but_a_value_is_needed_after_comma",
    "sonar.tsql.file.suffixes": ".sql,.tsql",
    "sonar.plsql.file.suffixes": ".plsql,.pkb",
    "sonar.test": "d:\\agent\\251\\s",
    "sonar.test.inclusions": "**/*.spec.ts,**/*.spec.js,**/*.mock.ts,**/*.mock.js,no_extra_inclusions_but_a_value_is_needed_after_comma"

(please ignore “no_extra_inclusions_but_a_value_is_needed_after_comma”)

At the beginning we can read:

2023-06-02T18:18:32.7075062Z INFO: Project configuration:
2023-06-02T18:18:32.7076690Z INFO:   Excluded sources: **/build-wrapper-dump.json, **/coverage-include-all.spec.ts, **/TestResultJunit/*.xml, no_extra_exclusion_but_a_value_is_needed_after_comma, **/*.spec.ts, **/*.spec.js, **/*.mock.ts, **/*.mock.js, no_extra_inclusions_but_a_value_is_needed_after_comma
2023-06-02T18:18:32.7081069Z INFO:   Included tests: **/*.spec.ts, **/*.spec.js, **/*.mock.ts, **/*.mock.js, no_extra_inclusions_but_a_value_is_needed_after_comma
2023-06-02T18:18:32.7083302Z INFO:   Excluded sources for coverage: **/Company*Migrations/**, **/Company*Test/**, **/Company*Tests/**, **/environments/environment*.ts, **/*.conf.js, **/src/main.ts, **/lib/http-services/*.services.ts, **/src/polyfills.ts, **/src/test.ts, **/lib/http-services/*.services.ts, **/lib/enumerations/*.enum.ts, **/lib/models/*.model.ts, **/e2e/src/app.e2e-spec.ts, **/e2e/src/app.po.ts, **/*.spec.ts, **/app-config.ts, **/*.interface.ts, **/Company-app-app/src/app/testing/**
2023-06-02T18:18:32.7084355Z INFO:   Excluded sources for duplication: **\*.spec.ts

We can see sonar.test.inclusions got added to the sonar.exclusions somehow

Let me know if this is not enough and you need the full log


Yes, I’d like the full log.


This is it:
log.txt (32.4 KB)


Thanks for the log.

I see you’re using Java 19. Only 11 and 17 are currently supported, although I don’t think that’s relevant to your question.

I don’t see anything else of note in your log. So I went back to your configuration. The correct parameter name is sonar.tests, with a trailing ‘s’. Can you correct that and try again? Because even though your sonar.test.inclusions was well handled, without a valid sonar.tests configuration the set to include from is empty.


thanks, that did it!

It’s easy when we use the right names :man_facepalming:

1 Like

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