jest-community / jest-runner-eslint

An ESLint runner for Jest
MIT License
480 stars 46 forks source link

Invalid report (for sonarqube): report refers to a file which is not configured as a test file #147

Closed Neizan93 closed 1 year ago

Neizan93 commented 1 year ago

sonarqube_report.xml generated by jest-sonar is not valid when including jest-runner-eslint files (non spec files should not be in tests reports) Pipeline:

00:56:37  ERROR: Error during SonarQube Scanner execution
00:56:37  ERROR: Error during parsing of generic test execution report '/home/jenkins/agent/workspace/myproject/reports/sonarqube_report.xml'. Look at the SonarQube documentation to know the expected XML format.
00:56:37  ERROR: Caused by: Line 72 of report refers to a file which is not configured as a test file: src/app/services/table-details/table-details.service.ts
00:56:37  ERROR: 
00:56:37  ERROR: Re-run SonarQube Scanner using the -X switch to enable full debug logging.

Local (with -X)

09:19:32.778 INFO: Sensor Generic Test Executions Report
09:19:32.781 INFO: Parsing C:\Users\myuser\myproject\reports\sonarqube_report.xml
09:19:32.837 INFO: ------------------------------------------------------------------------
09:19:32.839 INFO: EXECUTION FAILURE
09:19:32.840 INFO: ------------------------------------------------------------------------
09:19:32.842 INFO: Total time: 51.615s
09:19:32.905 INFO: Final Memory: 7M/48M
09:19:32.905 INFO: ------------------------------------------------------------------------
09:19:32.907 ERROR: Error during SonarScanner execution
Error during parsing of generic test execution report 'C:\Users\myuser\myproject\reports\sonarqube_report.xml'. Look at the SonarQube documentation to know the expected XML format.
Caused by: java.lang.IllegalStateException: Line 327 of report refers to a file 
which is not configured as a test file: src\app\shared\pipes\pipes.module.ts    
        at com.google.common.base.Preconditions.checkState(Preconditions.java:197)
        at org.sonar.scanner.genericcoverage.GenericTestExecutionReportParser.parseFiles(GenericTestExecutionReportParser.java:124)
        at org.sonar.scanner.genericcoverage.GenericTestExecutionReportParser.parseRootNode(GenericTestExecutionReportParser.java:104)
        at org.sonar.scanner.genericcoverage.GenericTestExecutionReportParser.lambda$parse$0(GenericTestExecutionReportParser.java:87)
        at org.sonar.scanner.genericcoverage.StaxParser.parse(StaxParser.java:110)
        at org.sonar.scanner.genericcoverage.StaxParser.parse(StaxParser.java:90)
        at org.sonar.scanner.genericcoverage.GenericTestExecutionReportParser.parse(GenericTestExecutionReportParser.java:88)
        at org.sonar.scanner.genericcoverage.GenericTestExecutionReportParser.parse(GenericTestExecutionReportParser.java:77)
        at org.sonar.scanner.genericcoverage.GenericTestExecutionSensor.execute(GenericTestExecutionSensor.java:86)
        at org.sonar.scanner.sensor.AbstractSensorWrapper.analyse(AbstractSensorWrapper.java:48)
        at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:85)
        at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:62)
        at org.sonar.scanner.scan.ModuleScanContainer.doAfterStart(ModuleScanContainer.java:82)
        at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:136)
        at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:122)
        at org.sonar.scanner.scan.ProjectScanContainer.scan(ProjectScanContainer.java:359)
        at org.sonar.scanner.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:354)
        at org.sonar.scanner.scan.ProjectScanContainer.doAfterStart(ProjectScanContainer.java:317)
        at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:136)
        at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:122)
        at org.sonar.scanner.bootstrap.GlobalContainer.doAfterStart(GlobalContainer.java:128)
        at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:136)
        at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:122)
        at org.sonar.batch.bootstrapper.Batch.doExecute(Batch.java:73)
        at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:67)
        at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:46)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.base/java.lang.reflect.Method.invoke(Unknown Source)
        at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60)
        at com.sun.proxy.$Proxy0.execute(Unknown Source)
        at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:189)
        at org.sonarsource.scanner.api.EmbeddedScanner.execute(EmbeddedScanner.java:138)
        at org.sonarsource.scanner.cli.Main.execute(Main.java:112)
        at org.sonarsource.scanner.cli.Main.execute(Main.java:75)
        at org.sonarsource.scanner.cli.Main.main(Main.java:61)
npm ERR! code ELIFECYCLE

I had to comment the line sonar.testExecutionReportPath tohave a valid sonar report until gets fixed.

Neizan93 commented 1 year ago

There is an incompatibility because sonarqube can not read the result of non spec (non tests) files.

so I changed the line // 'testMatch': [ '<rootDir>/src/app/**/*.ts' ] to lint only spec... : 'testMatch': [ '<rootDir>/src/app/**/*.spec.ts' ] in my jest.config.ts

/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
  'projects': [
    {
      'displayName': 'test',
      'preset': 'jest-preset-angular',
      'setupFilesAfterEnv': [
        '<rootDir>/setup-jest.ts'
      ],
      'globalSetup': 'jest-preset-angular/global-setup',
      'resolver': 'jest-preset-angular/build/resolvers/ng-jest-resolver.js',
      'testRunner': 'jest-jasmine2',
      'globals': {
        'ts-jest': {
          'tsconfig': '<rootDir>/tsconfig.spec.json',
          'stringifyContentPathRegex': '\\.(html|svg)$',
        },
      },
      'testEnvironment': 'jsdom',
      'transformIgnorePatterns': [
        'node_modules/(?!@angular|rxjs|ngrx|ngx-socket-io|jest-cli|ngx-echarts)', // List any packages here that error
        'node_modules/(?!.*\\.mjs$)'
      ],
      'transform': {
        '^.+\\.(ts|js|mjs|html|svg)$': 'jest-preset-angular'
      },
      'testPathIgnorePatterns': [
        '<rootDir>/node_modules/',
        '<rootDir>/dist/',
        '<rootDir>/src/test.ts'
      ],
      'moduleFileExtensions': [ 'ts', 'html', 'js', 'json', 'mjs' ],
      'moduleNameMapper': {
        '^src/(.*)$': '<rootDir>/src/$1',
        '^app/(.*)$': '<rootDir>/src/app/$1',
        '^assets/(.*)$': '<rootDir>/src/assets/$1',
        '^environments/(.*)$': '<rootDir>/src/environments/$1',
        '@components/(.*)': '<rootDir>/src/app/components/$1',
        '@directives/(.*)': '<rootDir>/src/app/directives/$1',
        '@services/(.*)': '<rootDir>/src/app/services/$1',
        '@environments/(.*)': '<rootDir>/src/environments/$1',
        '@models/(.*)': '<rootDir>src/app/models/$1',
        '@config/(.*)': '<rootDir>src/app/config/$1',
        '@shared/(.*)': '<rootDir>src/app/shared/$1'
      },
      'moduleDirectories': [ 'node_modules', __dirname ],
      'rootDir': './',
      'modulePaths': [
        '<rootDir>'
      ],
      'coveragePathIgnorePatterns': [
        '^.*\\.mock\\.[jt]sx?$',
        '^.*\\.config\\.[jt]sx?$',
        'node_modules',
        '.spec.ts',
      ],
      'collectCoverage': true,
      'collectCoverageFrom': [
        'src/**/*.ts'
      ],
      'coverageDirectory': '<rootDir>/coverage/',
      'coverageThreshold': {
        'global': {
          'branches': 50,
          'functions': 70,
          'lines': 70,
          'statements': 70
        }
      },
      'verbose': false,
      'testResultsProcessor': 'jest-sonar',
      'testMatch': [ '<rootDir>/src/app/**/*.spec.ts' ]
    },
    {
      'runner': 'jest-runner-eslint',
      'displayName': 'lint',
      // 'testMatch': [ '<rootDir>/src/app/**/*.ts' ]
      // jest-runner-eslint is able to lint any *.ts file
      // but sonarqube is not ready to understand the report
      // so, the pipeline should check any *.!(spec).ts file
      // and jest-runner-eslint checks any *.spec.ts file
      'testMatch': [ '<rootDir>/src/app/**/*.spec.ts' ]
    }
  ],
  'reporters': [
    'default',
    [
      'jest-sonar',
      {
        outputDirectory: 'reports',
        outputName: 'sonarqube_report.xml',
        reportedFilePath: 'relative'
      }
    ]
  ]
};
Neizan93 commented 1 year ago

Another alternative seems to be change "sonar.test.inclusions" in sonarqube-project.properties from: sonar.test.inclusions=**/*.spec.ts to sonar.test.inclusions=**/*.ts this will work, but the coverage will disappear from sonarqube... (so better go to my previous comment if you are here searching a workaround with coverage included)

ljharb commented 1 year ago

This seems like an issue with sonarqube and not with this package.

Happy to reopen if there's something actionable here.