fadc80 / karma-sonarqube-reporter

Karma reporter plugin for generating SonarQube generic test reports
MIT License
7 stars 8 forks source link

ERROR: Please, report issue => Test file path not found! {} | #23

Open helabenkhalfallah opened 5 years ago

helabenkhalfallah commented 5 years ago

Hello,

When I run test I got : ERROR: Please, report issue => Test file path not found! {} |

And the generated report is empty :

<?xml version='1.0'?>
<testExecutions version='1'/>

My Conf :

config.set({
        // Default configuration
        sonarqubeReporter: {
            basePath: './test',        // test files folder
            filePattern: '**/*spec.ts', // test files glob pattern
            encoding: 'utf-8',          // test files encoding
            outputFolder: 'target/report-tests/',    // report destination
            legacyMode: false,          // report for Sonarqube < 6.2 (disabled)
            reportName: () => 'karma-sonar-test.xml'
        },
        reporters: [
            'sonarqube'
        ],
    });
 "karma": "=0.13.22",
  "karma-sonarqube-reporter": "^1.2.5",

Thanks :)

fadc80 commented 5 years ago

Your Karma version seems to be very old. Did you add all the error output? It was expected more details have been outputted. Could you provide a sample project to reproduce this error? Thank you!

helabenkhalfallah commented 5 years ago

I will try using a more recent version of karma, thank you :) :)

fadc80 commented 5 years ago

Great! Let me know if it works with a newer Karma version. Meanwhile, I'll try to find out the minimal required version. I think I should have made it clear in the README instructions.

helabenkhalfallah commented 5 years ago

Hello @fadc80 I changed karma version to "karma": "=4.0.0", but I have the same issue. :(

fadc80 commented 5 years ago

Maybe you are not defining basePath and/or filePattern corretcly. According to the provided configuration, your project structure should look like this:

yourProject [folder] --> karma.conf.js [configuration file] --> test [basePath] ----> componetA [folder] ------> componentA.ts [source file] ------> componentA.spec.ts [filePattern match]

Verify your karma.conf.js file and test folder are inside the same folder. Additionally, be sure all test files are stored under the test folder or subfolders recursively.

gearsdigital commented 5 years ago

The issue occurs every time when parseTestFile() processes tests or test suites which are skipped. This means while describe() is matched, describe.skip() isn't. Same is true for it.

After some debugging I figured that the root cause lied within this regular expression:

((describe)|(it))\s*\(\s*((?<![\\])[\`\'\"])((?:.(?!(?<![\\])\4))*.?)\4

Walk through

  1. onSpecComplete defined in index.js checks if a test is successful, skipped or failed
  2. In case of describe.skip(), this.specSkipped is invoked and calls
  3. pathfinder.testFile, which iterates over all matched files and invokes
  4. exist() for each file but returns undefined because
  5. existDescribe looks for a property describe on an object which was never created

Explanation

The method parseTestFile() iterates over the result array of exec and assigns the values of result[2] || result[3]; to type.

As the regex doesn't match describe.skip(), type is undefined.

Therefore paths[path] = { describe: [], it: [] }; is never created, exist (existDescribe) will fail which results in Test file path not found!

function testFile(paths, describe, it) {
  var testFile = Object.keys(paths).find(path =>
    exist(paths, path, describe, it));
  if (testFile === undefined) {
    logger.error('Test file path not found! %s | %s | %s',
        JSON.stringify(paths), describe, it);
  }
  return testFile;
}

Long story short

I fixed this issue in #24 by slightly changing the regular expression. It now matches:

  1. describe.skip
  2. describe.only
  3. fdescribe
  4. xdescribe
  5. it.skip
  6. it.only
  7. fit
  8. xit

All of this is afaik valid at least for Mocha and Jasmine

Adapted regular expression

((\S{0,2}describe?[^(]+)|(\s\S{0,2}it?[^(]+))\s*\(\s*((?<![\\])[`'"])((?:.(?!(?<![\\])\4))*.?)\4

You can see it in action here: https://regex101.com/r/HUyh3u/1

This regex will match things like describe.lorem or describeLore() too. I guess this shouldn't be a problem because Jasmine or Mocha won't execute it either.

helabenkhalfallah commented 5 years ago

Hello @gearsdigital , your fix seems good for me, I will try to modify on my local and let you in ;) Thanks. :)

gearsdigital commented 5 years ago

@helabenkhalfallah I wonder if you can confirm to have skipped tests on your setup.

fadc80 commented 5 years ago

As I didn't have a sample project to reproduce it, I was just trying to guess what was wrong. I'm going to check your PR. Thank you.

gearsdigital commented 5 years ago

@fadc80 Do you need a sample project to verify? I can provide mine if you want...

fadc80 commented 5 years ago

It would be good! :+1:

gearsdigital commented 5 years ago

There you go: https://github.com/gearsdigital/karma-sonarqube-reporter-issue-23

fadc80 commented 5 years ago

@all-contributors please add @helabenkhalfallah as a contributor for bug.

allcontributors[bot] commented 5 years ago

@fadc80

I've put up a pull request to add @helabenkhalfallah! :tada:

helabenkhalfallah commented 5 years ago

Hello, I have not skipped tests on my configuration :

module.exports = function (config) {
    require("awt-unit-testing")(config, require("./webpack.config.js"));

    config.set({
        sonarqubeReporter: {
            basePath: 'test',        // test files folder
            filePattern: '**/*spec.ts', // test files glob pattern
            encoding: 'utf-8',          // test files encoding
            outputFolder: 'target/report-tests',    // report destination
            legacyMode: false,          // report for Sonarqube < 6.2 (disabled)
            reportName: "karma-sonar-test.xml"
        },
        reporters: ['sonarqube']
    });
};

I have only this and when I run : ERROR: Please, report issue => Test file path not found! {}

I use the version 1.3.0.

Thank you so much for your help :)

helabenkhalfallah commented 5 years ago

A test example :

describe("path", function () {

    /**
     * Test jasmine
     */
    it("first test", function () {
        expect(true).not.toBe(false);
    });

    /**
     * Test jasmine
     */
    it("second test", function () {
        expect(true).toBe(true);
    });

});
gearsdigital commented 5 years ago

@helabenkhalfallah Could you please try to delete the node_modules directory and your package-lock.json and re-run your test after a clean install?

If this doesn't help please provide the package.json and karma.config file.

helabenkhalfallah commented 5 years ago

Okay I will do this tomorrow and let you in ;)

helabenkhalfallah commented 4 years ago

Hello, I deleted node modules, package lock and rerun but the same issue :

11 12 2019 14:13:29.558:INFO [PhantomJS 2.1.1 (Windows 8.0.0)]: Connected on socket Si_EpP9Ey6zIsEutAAAA with id 78512712
ERROR: Please, report issue => Test file path not found! {} | xx| xxx: xx- xx
ERROR: Please, report issue => Test file path not found! {} | xx| xxx: xx- xx

Karma config :

module.exports = function (config) {
    require("unit-testing-commons")(config, require("./webpack.config.js"));

    config.set({
        sonarqubeReporter: {
            basePath: 'test',        // test files folder
            filePattern: '**/*spec.ts', // test files glob pattern
            encoding: 'utf-8',          // test files encoding
            outputFolder: 'target',    // report destination
            legacyMode: true,          // report for Sonarqube < 6.2 (disabled)
            reportName: "karma-sonar-test.xml"
        },
        reporters: ['sonarqube'],
    });
};

Package json :

  "devDependencies": {
    "awt-natif-pro-ent": "^1.x",
    "babel-plugin-transform-class-properties": "=6.24.1",
    "babel-plugin-transform-runtime": "=6.23.0",
    "babel-preset-env": "=1.7.0",
    "extend": "=3.0.0",
    "karma": "=4.0.0",
    "karma-phantomjs-launcher": "=1.0.2",
    "karma-sonarqube-reporter": "=1.3.0",
    "karma-webpack": "=3.0.5",
    "webpack": "^4.x"
  },
fadc80 commented 4 years ago

@helabenkhalfallah I couldn't reproduce this error with the information you provided. Is this all the content you have in your karma.conf.js file? I don't see any test framework. Which ones are you using (jasmine, mocha, etc)? This is an example of a valid config file:

https://github.com/fadc80/karma-sonarqube-reporter/blob/bcb397614fba5fe13c6c60e74c3e5d44ef35a75f/karma.conf.js#L1-L43

By the way, are you working with an Angular project?

gearsdigital commented 4 years ago

Same here. Wasn't able to reproduce it... It would be really helpful if you could provide a create a Minimal, Reproducible Example.

AKharytonchyk commented 4 years ago

@gearsdigital seem it fails if you use test data and string interpolation for test names in Jasmine.

Spec:

const expectedUrls = {
  modern: 'test.com?fm=webp',
  obsolete: 'test.com?fm=png&fl=png8',
};
const testData = [
    { browser: 'isChrome', url: 'test.com', expectedUrl: expectedUrls.modern },
    { browser: 'isIE', url: 'test.com', expectedUrl: expectedUrls.obsolete },
];

testData.forEach(({ browser, url, expectedUrl }) => {
  it(`should return "${expectedUrl.split('?')[1]}" format for "${browser}"`, () => {
    const browsers = {};
    browsers[browser] = true;
    fooService.getBrowsers.and.returnValue(browsers);

   service = TestBed.get(BarService);
   const actualUrl = service.getImageSrc({ url }, 'url');

    expect(actualUrl).toBe(expectedUrl);
  });
});

When running tests I get an error like: ERROR: Please, report issue => Test file path not found! {JSON-Output} | BarService | should return "fm=webp" format for "IsChrome"

Parsed JSON shows:

"src/app/services/bar.service.spec.ts": {
        "describe": [
            "BarService",
        ],
        "it": [
            "should return \"${expectedUrl.split('?')[1]}\" format for \"${browser}\""
        ]
    },
fadc80 commented 4 years ago

@AKharytonchyk This problem seems to be the same discussed on #18