Closed Neizan93 closed 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'
}
]
]
};
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)
This seems like an issue with sonarqube and not with this package.
Happy to reopen if there's something actionable here.
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:
Local (with -X)
I had to comment the line sonar.testExecutionReportPath tohave a valid sonar report until gets fixed.