Coder Social home page Coder Social logo

3dmind / jest-sonar-reporter Goto Github PK

View Code? Open in Web Editor NEW
68.0 6.0 43.0 118 KB

A Sonar test reporter for Jest.

License: MIT License

JavaScript 98.32% Shell 1.68%
sonarqube jest jest-testresultsprocessor javascript nodejs sonar jest-sonar-reporter

jest-sonar-reporter's Introduction

jest-sonar-reporter

Build Status Quality Gate

jest-sonar-reporter is a custom results processor for Jest. The processor converts Jest's output into Sonar's generic test data format.

Installation

Using npm:

$ npm i -D jest-sonar-reporter

Using yarn:

$ yarn add -D jest-sonar-reporter

Configuration

Configure Jest in your package.json to use jest-sonar-reporter as a custom results processor.

{
  "jest": {
    "testResultsProcessor": "jest-sonar-reporter"
  }
}

Configure Sonar to import the test results. Add the sonar.testExecutionReportPaths property to your sonar-project.properties file.

sonar.testExecutionReportPaths=test-report.xml

Customization

To customize the reporter you can use package.json to store the configuration.

Create a jestSonar entry like this:

{
  "jestSonar": {}
}

You can customize the following options:

  • reportPath This will specify the path to put the report in.
  • reportFile This will specify the file name of the report.
  • indent This will specify the indentation to format the report.
{
  "jestSonar": {
    "reportPath": "reports",
    "reportFile": "test-reporter.xml",
    "indent": 4
  }
}

Important: Don't forget to update sonar.testExecutionReportPaths when you use a custom path and file name.

Support for Sonarqube 5.6.x

Sonarqube 5.6.x does not support Generic Test Data however it has a Generic Test Coverage plugin which offers similar functionality.

If you have the plugin installed on Sonarqube, you can configure this reporter to produce files in supported format.

{
  "jestSonar": {
    "sonar56x": true
  }
}

Configure Sonar to import the test results. Add the sonar.genericcoverage.unitTestReportPaths property to your sonar-project.properties file.

sonar.genericcoverage.unitTestReportPaths=test-report.xml

Support for different configuration environments

To support different environments add the env property to the configuration and overwrite the value of the option you want to modify for the specific environment. You can overwrite the following configuration options: reportPath, reportFile, indent, sonar56x

For example: Overwrite the path were the report will be stored.

{
  "jestSonar": {
    "reportPath": "reports",
    "reportFile": "test-reporter.xml",
    "indent": 4,
    "env": {
      "test": {
        "reportPath": "reports-test"
      }
    }
  }
}

Use the NODE_ENV variable to activate the environment specific configuration.

NODE_ENV=test npm run test

Usage

  1. Run Jest to execute your tests.

Using npm:

$ npm run test

Using yarn:

$ yarn run test
  1. Run sonar-scanner to import the test results.
$ sonar-scanner

Licence

This project uses the MIT licence.

jest-sonar-reporter's People

Contributors

3dmind avatar dubes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

jest-sonar-reporter's Issues

Error when running jest without coverage (no folder)

Expected behaviour

To be able to run jest without coverage and without jest-sonar-reporter another

Actual behaviour

jest-sonar-reporter throws an error for no folder was found

image

Steps to reproduce the behaviour

When running jest with without coverage, no coverage folder is created and then jest-sonar-reporter throws an error for no folder was found

Use package.json for configuration.

As a user of the Jest Sonar Reporter I would like to use my package.json file to configure various settings.

  • Add namespace to package.json file.
  • Load and search package.json for namespace.
  • Add and process configuration properties.

Configuration example:

{
  "jestSonar": {
    "reportPath": "reports"
  }
}

Update Documentation

  • How to use package.json file to configure the Jest Sonar Reporter. [#8]
  • Mark TEST_REPORT_PATH environment variable as deprecated.

Cannot run jest successfully from subfolder with this installed

Expected behaviour

Running Jest in a subfolder should work equivalent to the root

Actual behaviour

package.json is not found since this attempts to use the equivalent of ${process.cwd()}/package.json

Steps to reproduce the behaviour

Run Jest from a subfolder.

Suggestion:

Use https://www.npmjs.com/package/pkg-up

report folder is not generating

Expected behaviour

report folder should created with xml

Actual behaviour

report folder is not creating

Steps to reproduce the behaviour

done this configuration

package.json

{
"scripts": {
"test": "jest --coverage -u",
},
"devDependencies": {
"jest": "^22.4.2",
"jest-sonar-reporter": "^2.0.0",
},
"jest": {
"testEnvironment": "node",
"testResultsProcessor": "jest-sonar-reporter"
},
"jestSonar": {
"reportPath": "reports",
"reportFile": "test-report.xml",
"indent": 4
}
}

sonar.project.properties

sonar.language=js
sonar.sources=src
sonar.tests=src
sonar.sourceEncoding=UTF-8

sonar.test.inclusions=src//*.spec.js,src//.spec.jsx,src/**/.test.js,src/**/*.test.jsx

sonar.testExecutionReportPaths=reports/test-report.xml
sonar.javascript.lcov.reportPaths=coverage/lcov.info

and doing npm run test

anything which i am missing

test-report.xml only giving Generic Execution data and no Generic Coverage data...

Expected behaviour

I would expect Jest-Sonar-Reporter to generate a full Generic Test Data report as the readme suggests.

Actual behaviour

Generating a test-report.xml file which only contains Generic Execution data. Was expecting Generic Coverage information as well.

Steps to reproduce the behaviour

Install plugin, generate test-report.xml and inspect the xml file to see that it only includes Generic Execution as outlined by https://docs.sonarqube.org/display/SONAR/Generic+Test+Data

Configuration for relative or absolute path in report

Hi.

First of all I do not know, if this is an issue, I would say it is more of a feature request. I need a configuration option where I can define, if path for file element in XML report uses absolute or relative path. In this moment the path is absolute and I had a problem with that, such that Sonar Qube would not import my JUnit reports, because the path would not match with the path that I set for tests and source in Sonar Qube properties.

Expected behaviour

Config option to set, if path for file element in report should be either relative or absolute.

Actual behaviour

It is always absolute path.

Steps to reproduce the behaviour

Run jest test runner with jest-sonar-reporter inside a AWS CodeBuild.

Best regards.

react-boilerplate

I am using react-boilerplate that use jest and istanbul for coverage.

I have tried to run sonnar scanner and I encounter trouble to get the proper configuration.

Would you mind showing an example of sonar configuration on react-boilerplate project?

[DOC] Configuring sonar-project.properties

Hi,

I've been playing with it for some time and I looks like I have figured out the correct setup needed for this to work.

In my case, both source files and test files are in src folder. Something like this

  • src/components/Foo/Foo.jsx the main component.
  • src/components/Foo/Foo.spec.jsx the test file.

So I had to set it up the following way.

# Source
sonar.sources=src
# Where to find tests file, also src
sonar.tests=src
# But we get specific here
# We don't need to exclude it in sonar.sources because it is automatically taken care of
sonar.test.inclusions=src/**/*.spec.js,src/**/*.spec.jsx,src/**/*.test.js,src/**/*.test.jsx

# Now specify path of lcov and testlog
sonar.javascript.lcov.reportPaths=coverage/jest/lcov.info
sonar.testExecutionReportPaths=coverage/jest/testlog.xml

This made it work.

The above directory is pretty common, and we do need to specify sonar.tests for this to work. Maybe you should add it to the docs.

Error during parsing of generic test execution report

I'm trying to use the reporter with a react-native app. After generating a report and running sonar-scanner it fails with:

12:52:12.293 ERROR: Error during parsing of generic test execution report '<logn path>/test-report.xml'. Look at the SonarQube documentation to know the expected XML format.
12:52:12.294 ERROR: Caused by: Line 3 of report refers to a file which is not configured as a test file: <long path>/Feature/__tests__/api.test.js

test-report.xml:

<?xml version="1.0" encoding="UTF-8"?>
<testExecutions version="1">
  <file path="<long path>/__tests__/api.test.js">
    <testCase name="Feature when called with proper params then does somthing" duration="5"/>
    <testCase name="Feature when called with wrong paramg then does nothing" duration="1"/>
  </file>
</testExecutions>

jest.confi.json

{
  "preset": "react-native",
  "transformIgnorePatterns": [
    "node_modules/(?!(jest-)?react-native|react-navigation)"
  ],
  "transform": {
    "^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js"
  },
  "testResultsProcessor": "jest-sonar-reporter"
}

sonar-project.properties

sonar.projectKey=my-app
sonar.projectName=my-app
sonar.projectVersion=1.0
sonar.sourceEncoding=UTF-8
sonar.sources=src
sonar.javascript.file.suffixes=.js,.jsx
sonar.testExecutionReportPaths=test-report.xml

Can someone please help me to understand what could be wrong?

Does this plugin even work anymore?

  1. SonarQube 6.3
  2. Followed the exact instruction
  3. getting error as following

screen shot 2018-01-03 at 4 25 48 pm

What inside Header/index.test.js is following code

import React from 'react';
import renderer from 'react-test-renderer';
import Header from './index';

describe('Header Container', () => {
  test('renders as expected', () => {
    const tree = renderer.create(<Header />).toJSON();
    expect(tree).toMatchSnapshot();
  });

  describe('When Unauthorized', () => {
    test('unauthorized', () => {
      const tree = renderer.create(<Header roles={{}} />).toJSON();
      expect(tree).toMatchSnapshot();
    });
  });
});

Is there anything I missed that maybe not inside the document?

Support for reporters?

Is there any planned support for the jest reporters property. I hear there is talk that they will start deprecating testResultsProcessor soon in favor of reporters?

Expected behaviour

As a test I placed jest-sonar-reporter in the reporters field of the jest config and expected it to work.

Actual behaviour

The reporter fails to initialize with the following exception:

Error: An error occurred while adding the reporter at path "node_modules/jest-sonar-reporter/index.js".Reporter is not a constructor

Steps to reproduce the behaviour

Place jest-sonar-reporter in the reporters array instead of testResultsProcessor

Parsing error due to invalid character

Sonar fails on attempt to parse generate file:

10:23:06.365 ERROR: Error during parsing of generic test execution report '/root/repo/reports/test-reporter.xml'. Look at the SonarQube documentation to know the expected XML format.
10:23:06.365 ERROR: Caused by: Illegal character ((CTRL-CHAR, code 27))
 at [row,col {unknown-source}]: [13,61]

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.