Coder Social home page Coder Social logo

Comments (5)

acwatson avatar acwatson commented on August 19, 2024

Ran into this problem myself today. I think that the issue is really with the JsTestDriverSensor.java implementation from the sonar-javascript-plugin v2.8. That class has a method to get the test file names (AKA the "spec" file names). The method implementation cannot work when you have dots in the file name because it replaces all dots with "/". It does this because it assumes you are using dots as directory delimiters and that your file naming strategy is compatible with Java, (which doesn't allow dots in class names). Here is the method implementation I'm speaking of:

protected String getUnitTestFileName(String className) {
// For JsTestDriver assume notation com.company.MyJsTest that maps to com/company/MyJsTest.js
String fileName = getUnitTestClassName(className);
fileName = fileName.replace('.', File.separatorChar);
fileName = fileName + ".js";
return fileName;
}

from karma-junit-reporter.

ryepup avatar ryepup commented on August 19, 2024

Spent some time on this, found a way to make it work w/ john papa's guidelines.

In sonar-project.properties, pull in the tests using the jstest option, not the jstestdriver:

# tell sonar our tests and source are intermingled
sonar.sources=src
sonar.tests=src
# spec.js files aren't "source"
sonar.exclusions=**/*.spec.js
# tests are spec.js files
sonar.test.inclusions=**/*.spec.js
# use the jstest sensor, look in the "tests" folder for results
sonar.javascript.jstest.reportsPath=tests/

jstest doesn't do any transformations to the test name (see https://github.com/SonarCommunity/sonar-javascript/blob/master/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/unittest/jstest/JsTestSensor.java)

Then, in your tests, the first describe block has to match the path and filename exactly; so dir/subdir/avengers.controller.spec.js needs to start with describe('dir/subdir/avengers.controller.spec.js', ...)

Then, in your karma.conf.js, configure the junit reporter:

junitReporter: {
  useBrowserName: false,
  outputDir: 'tests',
  outputFile: 'TESTS-results.xml' // sonarqube is looking for TESTS-*.xml
}

When you run tests, karma-junit-reporter will write a tests/TESTS-results.xml file with XML that looks like:

<testcase name="does things" time="0.014" classname="dir/subdir/avengers.controller.spec.js"/>

With those in place, sonar-runner can read the xml, use the classname to find the path to the test, combines that with the sonar.tests setting to produce the full path to test file, and then be happy.

from karma-junit-reporter.

acwatson avatar acwatson commented on August 19, 2024

thanks @ryepup. I ended up writing my own sonar plugin to handle the test report xml. My plugin does not require that the describe block have any particular name. I found it too restrictive and brittle to limit what developers could name the describe blocks. For anyone interested, see the free plugin I wrote here: https://github.com/acwatson/sonar-karma-test-report-plugin

from karma-junit-reporter.

americoneto1 avatar americoneto1 commented on August 19, 2024

Thank you @ryepup! I was trying to do this for a long time!

from karma-junit-reporter.

jehon avatar jehon commented on August 19, 2024

I see that sonar will use a new schema (see bug #78). I think that this problem should dissapear as soon as this new schema is implemented.
Is my analysis correct and of any help?

from karma-junit-reporter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.