Exclusions/Inclusions should be able to filter by file name

I tried to use unit tests and code at the same level and use inclusion/exclusion parameters, just like it is describe here: Narrowing the focus with analysis scope.

The problem is that the wildcard pattern seems to not take on consideration file name and therefore makes impossible to use an structure like this:

- MyComponent
-- MyComponent.tsx
-- MyComponent.test.tsx

The already mentioned documentation only shows folder filter but would be great to filter by file name, like:

source: 'src/',
tests: 'src/',
exclusions: [ 'src/**/*.test.tsx', 'src/**/*.test.ts' ]

Separation by concern is much better than separation by type and also increases visibility of what is lacking unit tests.

Hey there.

File names should be considered – can you share some examples of what you’ve tried, and how you know it didn’t work?

Hi!

It was how it was configured in one of my tries:

const sonarSources = ['./src'];
const sonarExclusions = ['./src/**/*.test.*'];
const sonarTests = ['./src/'];
const sonarTestInclusions = ['./src/**/*.test.*'];

module.exports = {
  token: '******',
  serverUrl: 'https://sonarcloud.io',
  options: {
    'sonar.sources': sonarSources.join(','),
    'sonar.exclusions': sonarExclusions.join(','),
    'sonar.tests': sonarTests.join(','),
    'sonar.test.inclusions': sonarTestInclusions.join(','),
    'sonar.coverage.exclusions': sonarTestInclusions.join(','),
    'sonar.javascript.lcov.reportPaths': './coverage/lcov.info',
    'sonar.testExecutionReportPaths': './reports/test-report.xml',
    'sonar.organization': '*****',
    'sonar.projectKey': '****',
  },
};

The files were like:

- components
-- MyComponent
---- MyComponent.tsx
---- MyComponent.test.tsx
-- AnotherComponent
...

It always listed the .test files having lines to cover, leaving it really low.
Was only able to overcome it when I separated the tests from code, but organisation wise it is very bad :confused:

Would love leave tests in the same folder again, thanks for the help!!

Hey there.

I spot the problem. Inclusion/Exclusion globs don’t play friendly with relative paths… and they aren’t necessary here (you’re defining a pattern, not a file path like you are with sonar.sources).

So you just need to remove the ./ from your sonarExclusions and sonarTestInclusions

const sonarSources = ['./src'];
const sonarExclusions = ['src/**/*.test.*'];
const sonarTests = ['./src/'];
const sonarTestInclusions = ['src/**/*.test.*'];

Tested on my side and works like a charm. :slight_smile:

2 Likes

Oh I see, I´m not sure if it tested like you sent me.

I’ll make a test right away! Thanks for the help and fast answers!!

Hey Colin, it worked as you mentioned. Coverage for these files are ignored.

Thank you very much!!

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