The Best way to Analyze React Project with SonarQube
Run npx create-react-app react --template typescript
script{
...
"coverage": "react-scripts test --coverage",
}
Run npm install --save-dev sonarqube-scanner
Run npm install --save-dev jest-sonar-reporter
On the root of the project, create a ‘sonarqube-scanner.js’ file
const scanner = require('sonarqube-scanner');
scanner(
{
serverUrl: "http://localhost:9000",
options: {
"sonar.login": "admin",
"sonar.password": "sonar",
"sonar.projectName": "react_app",
"sonar.projectDescription": "Just for demo...",
"sonar.sourceEncoding":"UTF-8",
"sonar.sources": "./src",
"sonar.test.inclusions": "**/*.test.tsx,**/*.test.ts",
"sonar.exclusions": "**/*.test.tsx",
"sonar.tests":"./src",
"sonar.testExecutionReportPaths":"test-report.xml",
"sonar.javascript.lcov.reportPaths":"coverage/lcov.info"
},
},
() => process.exit()
);
Run coverage
script{
...
"test-sonar": "react-scripts test --watchAll=false --coverage --testResultsProcessor jest-sonar-reporter",
"sonar": "node sonarqube-scanner.js"
}
Run -d --name sonarqube -p 9000:9000 -p 9092:9092
Run test-sonar
Run sonar