karma-sonarqube-unit-reporter icon indicating copy to clipboard operation
karma-sonarqube-unit-reporter copied to clipboard

Support regexps for testFilePattern

Open P3tronius opened this issue 6 years ago • 5 comments

Hello,

I'm using your plugin in an hybrid angularJS/angular6 app, so I have both .spec.ts and .spec.js. Unfortunately, in file-util.js, there is: } else if (filename.endsWith(filter)) { results.push(filename) }

It would be really nice to change that to .matches(), so regex would be supported !

Thanks,

P3tronius avatar Oct 12 '18 14:10 P3tronius

Especially as in index.js there is: var testFilePattern = reporterConfig.testFilePattern || '(.spec.ts|.spec.js)'

P3tronius avatar Oct 12 '18 15:10 P3tronius

Hi Guys, Even I'm facing this issue with react application. Any plans for this change?

kiran8910 avatar Apr 27 '20 21:04 kiran8910

@kiran8910 no jest for react?

Boltyk avatar Apr 27 '20 22:04 Boltyk

There is an easy hack, at the top of your karma.config.js file (or any other loaded in the browser), add this override:

// Override of string.endWith to workaround sonarqubeUnit unable to use regex in testFilePattern
String.prototype.endsWith = function(regex) 
    if (regex.startsWith("regex:")) {
        return this.match(/.*(.spec.ts|.spec.js)$/);
    }
    // original implementation
    if (this.length < regex.length) {
        return false;
    }
    return this.lastIndexOf(regex) === this.length - regex.length;
};

and then in your sonarQubeUnitReporter config block, add

sonarQubeUnitReporter: {
            [...]
            testFilePattern: "regex:"
        },

Not super clean, but it works.

P3tronius avatar Apr 28 '20 07:04 P3tronius

Thanks for the update.

kiran8910 avatar Apr 28 '20 17:04 kiran8910