{"id":40140,"date":"2024-10-14T03:43:18","date_gmt":"2024-10-14T09:13:18","guid":{"rendered":"https:\/\/www.solutionanalysts.com\/blog\/?p=40140"},"modified":"2024-12-16T03:49:05","modified_gmt":"2024-12-16T09:19:05","slug":"write-test-case-and-code-coverage-using-sonarqube-in-nodejs","status":"publish","type":"post","link":"https:\/\/www.solutionanalysts.com\/blog\/write-test-case-and-code-coverage-using-sonarqube-in-nodejs\/","title":{"rendered":"NodeJs &#8211; Write Test case and Code Coverage using SonarQube"},"content":{"rendered":"<p>In the ever-evolving world of software development, maintaining code quality is crucial. This is where tools like SonarQube and practices like unit testing come into play. Together, they ensure that the code is functional, clean, maintainable, and free from bugs. This guide will walk you through writing test cases in Node.js, integrating them with SonarQube, and achieving comprehensive code coverage.<\/p>\n<p>Let\u2019s start by exploring the first SonarQube integration in NodeJS.<\/p>\n<p>Developers use SonarQube to analyze static code, making it a universal standard in the IT industry. SonarQube can make the code cleaner, more straightforward, and easier to read.<\/p>\n<p>Moving ahead, let us understand how SonarQube works.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"What_is_SonarQube\"><\/span>What is SonarQube?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>SonarSource developed SonarQube as an open-source platform to continuously inspect or analyze developers&#8217; code. It analyzes or scans all the static code written by developers and provides detailed reports on Bugs, Vulnerabilities, Code smells, code duplicates, and Security issues.<\/p>\n<p>No matter whatever programming languages you have written your code, SonarQube supports 25+ programming languages through built-in rule sets. You can also extend that rule set using various plugins. You can find all the supported languages from this reference: <span style=\"font-weight: 400;\"><a href=\"https:\/\/rules.sonarsource.com\/\" target=\"_blank\" rel=\"noopener\">Rules Sonarsource<\/a>\u00a0<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Benefits_of_SonarQube\"><\/span>Benefits of SonarQube<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-40142\" src=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2024\/10\/07-10-2024_Node-JS-02.jpg\" alt=\"\" width=\"8075\" height=\"4575\" \/><\/p>\n<ol>\n<li><strong>Sustainability<\/strong><br \/>\nReduces complexity, possible vulnerabilities, and code duplications, as well as optimizes the life of applications.<\/li>\n<li><strong>Increase productivity<\/strong><br \/>\nReduces the scale, cost of maintenance, and risk of the application; as such, it eliminates the need to spend more time changing the code<\/li>\n<li><strong>Quality Code<\/strong><br \/>\nCode quality control is an inseparable part of the software development process.<\/li>\n<li><strong>Detect Errors<\/strong><br \/>\nDetects errors in the code and alerts developers to fix them automatically before submitting them for output<\/li>\n<li><strong>Increase consistency<\/strong><br \/>\nDetermines where the code criteria are breached and enhances the quality.<\/li>\n<li><strong>Business scaling<\/strong><br \/>\nThere is no restriction on the number of projects to be evaluated.<\/li>\n<li><strong>Enhance developer skills<\/strong><br \/>\nRegular feedback on quality problems helps developers to improve their coding skills.<\/li>\n<\/ol>\n<h2><span class=\"ez-toc-section\" id=\"Why_SonarQube\"><\/span>Why SonarQube?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>So far, we have discussed SonarQube and its benefits. Now, let\u2019s examine why we should use It.<\/p>\n<p>As you know, developers are doing too much coding in their lives for many reasons, such as achieving project timelines and deliveries and fixing bugs on products to fulfill client requirements and make production stable. In that case, devs don\u2019t look back on the quality of the code and fix the issues hurriedly. Also, leads do not have the time to review code fixes, and they directly move the buggy or non-quality code to the product, which leads to the failure of production performance at any time and, alternatively, creates impacts on costs for clients.<\/p>\n<p>Ideally, this is a very wrong impression of developers and against the best practices that affect the developer\u2019s career.<\/p>\n<p>To avoid such scenarios and maintain good code quality, we have great free tools in the IT industry. One of them is SonarQube, which improves and delivers the best code quality on production each time devs change or add new code.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-40168\" src=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2024\/10\/undefined-0.png\" alt=\"\" width=\"602\" height=\"250\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Time_to_set_up_and_use_SonarQube\"><\/span>Time to set up and use SonarQube<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We have seen the benefits and features of SonarQube. Now, it&#8217;s time to implement it into your system and leverage its features. Let\u2019s start with what we require and how to set it up.<\/p>\n<p>Before starting the setup of SonarQube, you need to check the below prerequisites in your system:<\/p>\n<ul>\n<li>JAVA (Oracle JRE 11 or OpenJDK 11), you can also find more [<a href=\"https:\/\/docs.sonarsource.com\/sonarlint\/intellij\/getting-started\/requirements\/\" target=\"_blank\" rel=\"noopener\">details<\/a>]<\/li>\n<li>Should have NodeJS version 8 or higher installed<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Download_SonarQube\"><\/span>Download SonarQube<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>There are two options for downloading Sonar Scanner, you can choose any one<br \/>\nGet the LTS version from [<span style=\"font-weight: 400;\"><a href=\"https:\/\/www.sonarqube.org\/downloads\/\" target=\"_blank\" rel=\"noopener\">Download<\/a>]<\/span><br \/>\nUsing <a href=\"https:\/\/hub.docker.com\/_\/sonarqube\" target=\"_blank\" rel=\"noopener\">Docker image<\/a><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Sonar_Scanner\"><\/span>Sonar Scanner<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>There are two options for downloading Sonar Scanner, you can choose any one<br \/>\nUsing following this link &#8211; <a href=\"https:\/\/docs.sonarqube.org\/latest\/analysis\/scan\/sonarscanner\" target=\"_blank\" rel=\"noopener\">sonar<\/a><a href=\"https:\/\/docs.sonarqube.org\/latest\/analysis\/scan\/sonarscanner\" target=\"_blank\" rel=\"noopener\"> scanner<\/a><br \/>\nUsing <a href=\"https:\/\/www.npmjs.com\/package\/sonarqube-scanner\" target=\"_blank\" rel=\"noopener\">npm package\u00a0<\/a><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Follow_the_steps_to_run_SonarQube\"><\/span>Follow the steps to run SonarQube<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Below are commands that can be run from your cmd or terminal, either on Windows or Another OS. To set the SonarQube bin folder path cho on the platform, run the following command.<\/p>\n<p><strong>#For Windows(cmd):<\/strong><br \/>\nC:\\sonarqube\\bin\\windows-x86-64&gt;StartSonar.bat<\/p>\n<p><strong>#For other OS (terminal):<\/strong><br \/>\nC:\\sonarqube\\bin\\[OS]&gt;sonar.sh<\/p>\n<p>If you are running SonarQube using a Docker image, you do not need to set up the above command.<\/p>\n<p>Now SonarQube will be run on <a href=\"http:\/\/localhost:9000\/\" target=\"_blank\" rel=\"noopener\">localhost<\/a> and\u00a0log in using default credentials (login: admin, password: admin)<\/p>\n<p>Once you are logged in, we need to set up an analysis project.<\/p>\n<ol>\n<li>Go to \u201cCreate Project\u201d on the right side of the page and click on the manual tab.<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-40167\" src=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2024\/10\/undefined-1.png\" alt=\"\" width=\"450\" height=\"174\" \/><\/li>\n<li>Enter a project name and key name that will be used in future sonar properties.<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-40166\" src=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2024\/10\/undefined-2.png\" alt=\"\" width=\"602\" height=\"464\" \/><\/li>\n<li>Now click on sonar analysis setup for \u2018Locally\u2019<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-40165\" src=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2024\/10\/undefined-3.png\" alt=\"\" width=\"538\" height=\"427\" \/><\/li>\n<li>Select \u2018Generate a Token\u2019<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-40164\" src=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2024\/10\/undefined-4.jpg\" alt=\"\" width=\"602\" height=\"400\" \/><br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-40163\" src=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2024\/10\/undefined-5.png\" alt=\"\" width=\"602\" height=\"259\" \/><\/li>\n<li>Select the Programming Language that you are using in your project and the OS used<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-40162\" src=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2024\/10\/undefined-6.png\" alt=\"\" width=\"602\" height=\"335\" \/><\/li>\n<li>Now you can run the sonar scanner by executing the command that is provided in the above screenshot<\/li>\n<li>OR you can add the scanner property in your project and use the sonar-scanner command in package.json<br \/>\nTo do so, you need to add the dependency package below to your project.<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-40161\" src=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2024\/10\/undefined-7.png\" alt=\"\" width=\"251\" height=\"32\" \/><br \/>\nUse the script below to run the scanner from the command.<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-40160\" src=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2024\/10\/undefined-8.png\" alt=\"\" width=\"513\" height=\"49\" \/><br \/>\nNow run the below command to run the scanner.<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-40159\" src=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2024\/10\/undefined-9.png\" alt=\"\" width=\"145\" height=\"34\" \/><br \/>\nThe sonar property file will look like in the root folder:<br \/>\nsonar-project.properties<br \/>\nsonar.host.url=http:\/\/localhost:9000\/<br \/>\nsonar.projectKey=test-nodejs<br \/>\nsonar.projectName=test-nodejs<br \/>\nsonar.login=****************************<br \/>\nsonar.projectVersion=1.0<br \/>\nsonar.sources=.<br \/>\nsonar.coverage.exclusions=node_modules\/*,app.js,package.json,.scannerwork,.env,public\/*,db\/*,test\/*You define your files in exclusions or in inclusion based on your needs.<\/li>\n<li>Once the scanner analysis is done, you will be able to see the scanner report under the projects section for your project. It will look like the one below.<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-40158\" src=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2024\/10\/undefined-10.png\" alt=\"\" width=\"577\" height=\"345\" \/><\/li>\n<\/ol>\n<h2><span class=\"ez-toc-section\" id=\"SonarLint_with_SonarQube\"><\/span>SonarLint with SonarQube<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>As you now have more knowledge of SonarQube and Scanner to improve your code quality and implement best code practices, there is also one package or extension available to check errors and issues in your local IDE before committing your code and before running SonarQube. Yes, you are right. We are discussing the SonarLint extension, which is available for almost all IDE editors.<\/p>\n<p>Here, we are going to explain how to use <a href=\"https:\/\/www.sonarqube.org\/sonarlint\/\" target=\"_blank\" rel=\"noopener\">SonarLint<\/a> in <a href=\"https:\/\/www.sonarsource.com\/products\/sonarlint\/features\/vs-code\/\" target=\"_blank\" rel=\"noopener\">VS Code<\/a>. This will allow you to have clean code in advance before pushing it to your branch.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Steps_to_download_and_install_SonarLint_in_VS_Code\"><\/span>Steps to download and install SonarLint in VS Code<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>1. Open your VS Code IDE and click on the \u201cExtensions\u201d menu from the left side pane<br \/>\n2. Now search \u201csonarlint\u201d, and it will appear in the first place, like below<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-40157\" src=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2024\/10\/undefined-11.png\" alt=\"\" width=\"415\" height=\"169\" \/><\/p>\n<p>3. Now click on install, and it will start installing in your VS code. Once it&#8217;s done, it will show you the page where you will find all the details and it\u2019s.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-40156\" src=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2024\/10\/undefined-12.png\" alt=\"\" width=\"602\" height=\"127\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-40155\" src=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2024\/10\/undefined-13.png\" alt=\"\" width=\"602\" height=\"428\" \/><\/p>\n<p>4. Once it\u2019s installed, it will auto-enable in your current working directory project, as shown below in the \u201cPROBLEMS\u201d section, with its pre-resolution suggestion.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-40154\" src=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2024\/10\/undefined-14.png\" alt=\"\" width=\"602\" height=\"143\" \/><\/p>\n<p>When you click on any problem, it will redirect you to the problem, which will be like<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-40153\" src=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2024\/10\/undefined-15.png\" alt=\"\" width=\"602\" height=\"100\" \/><\/p>\n<p>Now you are an expert developer, so you know how to fix it!!! Please fix it and commit it. So you don\u2019t have to wait till you commit your code and run SonarQube.<\/p>\n<p>Using this SonarLint, you can identify your issues and errors priorly and fix them. This will reduce your time, and you do not have to wait for the Sonar Scanner output. With this, productivity will increase, and there will be fewer chances to have bugs on production levels.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-40152\" src=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2024\/10\/undefined-16.png\" alt=\"\" width=\"602\" height=\"152\" \/><\/p>\n<p>This quick tutorial document will help you kickstart SonarQube integration in your project by leveraging its core functionality.<\/p>\n<p>Now that you have learned and integrated SonarQube, we can move on to writing test cases, which will help you test your code and ensure its quality.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Here_are_some_insights_of_Unite_Testing_topic\"><\/span>Here are some insights of Unite Testing topic:<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Software development necessitates unit testing, which ensures a high-quality product by testing each software component. Developers can quickly find and fix bugs before the product goes into production by using the Unit testing method. The correction of the isolated code is the primary objective of this unit testing.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Unit_Testing_what_is_it\"><\/span>Unit Testing: what is it?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The first level of testing, known as unit testing, involves examining each individual module or component of your code to prioritize and address bugs before they are released to production.<\/p>\n<p>One method of testing, known as unit testing, is typically carried out by developers. However, quality assurance engineers also perform unit testing despite developers&#8217; reluctance to test.<\/p>\n<p>By providing the necessary inputs to the test script and asserting that the actual output matches the expected results, the developers create unit tests.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-40151\" src=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2024\/10\/undefined-17.png\" alt=\"Life cycle of Unit testing [PC: tutorialspoint] \" width=\"462\" height=\"479\" \/><\/p>\n<p>Now that we&#8217;ve covered the fundamentals of unit testing let&#8217;s take a look at its advantages.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"What_are_its_advantages\"><\/span>What are its advantages?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ol>\n<li>Modifying existing functionality reduces bugs or defects in newly developed features. Hence, the cost of testing can decrease as defects are discovered early on.<\/li>\n<li>It improves design and makes it easier to refactor code, which ultimately improves the build&#8217;s quality.<\/li>\n<li>Since bugs are discovered in the early stages of development, this approach has the potential to reduce overall testing costs.<\/li>\n<li>Because unit testing is more reliable, it is easier to refactor code.<\/li>\n<li>This training additionally conditions engineers to reevaluate how they code.<\/li>\n<li>It is precisely developing modular parts that can be mocked if they have dependencies.<\/li>\n<li>Tests can be automated, which is very helpful for scaling code maintenance.<\/li>\n<li>Overall, it raises the code&#8217;s quality.<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-40150\" src=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2024\/10\/undefined-18.png\" alt=\"Advantages of Unit testing [PC: TestingExperts]\" width=\"602\" height=\"284\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"What_are_the_Detriments_of_Unit_testing\"><\/span>What are the Detriments of Unit testing?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ol>\n<li>Integration or interface issues between two modules cannot be detected by unit testing. Non-functional aspects like the system&#8217;s scalability and performance cannot be tested.<\/li>\n<li>It is unable to detect system-wide errors involving multiple modules.<\/li>\n<li>The application&#8217;s business requirements or functional correctness cannot be guaranteed by unit tests as well.<\/li>\n<li>Depending on the level of complexity, one or more unit tests are typically required; hence, the amount of code that must be written must increase.<\/li>\n<li>It is very challenging to test the user interface (UI), but it is useful for testing the implementation of business logic. However, unit testing is not great for UI as it is a back-end test. It can check that a calculation is correct, but it can&#8217;t verify whether the results display correctly to the user.<\/li>\n<li>Unit Testing cannot identify all programming errors, and it can&#8217;t test integration errors.<\/li>\n<\/ol>\n<p>The following are some of the best unit testing tools and frameworks, some of which we will use in our NodeJs test case integration:<\/p>\n<p>&#8211; <a href=\"https:\/\/mochajs.org\/\" target=\"_blank\" rel=\"noopener\">Mocha<\/a> &#8211; a JavaScript test framework running on Node.js and in the browser.<br \/>\n&#8211; <a href=\"https:\/\/www.chaijs.com\/\" target=\"_blank\" rel=\"noopener\">Chai<\/a> &#8211; is an assertion library that adds a more expressive language to tests.<br \/>\n&#8211; <a href=\"https:\/\/jasmine.github.io\/\" target=\"_blank\" rel=\"noopener\">Jasmin<\/a> &#8211; a behavior-driven development (BDD) framework for testing JavaScript code.<br \/>\n&#8211; <a href=\"https:\/\/jestjs.io\/\" target=\"_blank\" rel=\"noopener\">Jest<\/a> &#8211; for testing javascript front-end and back-end applications.<br \/>\n&#8211; There are a <a href=\"https:\/\/en.wikipedia.org\/wiki\/List_of_unit_testing_frameworks\" target=\"_blank\" rel=\"noopener\">lot of unit testing frameworks <\/a>available based on different languages,<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Workflow\"><\/span>Workflow<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>This is the unit testing workflow, which will help you better understand how it works.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-40149\" src=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2024\/10\/undefined-19.png\" alt=\"\" width=\"605\" height=\"87\" \/><\/p>\n<p>Now that we&#8217;ve seen the advantages, drawbacks, and workflow, let&#8217;s try putting it into your system so we can generate more ideas. First things first: what we need and how to set it up.<\/p>\n<p>Before starting, you need to check the below prerequisites in your system:<\/p>\n<p>&#8211; JAVA (Oracle JRE 11 or OpenJDK 11), you can also find more [<a href=\"https:\/\/docs.sonarsource.com\/sonarlint\/intellij\/getting-started\/requirements\/\" target=\"_blank\" rel=\"noopener\">details<\/a>]<br \/>\n&#8211; Should have NodeJS version 8 or higher installed<br \/>\n&#8211; SonarQube and Sonar Scanner (refer <a href=\"https:\/\/www.solutionanalysts.com\/blog\/sonarqube-integration-in-nodejs\/\">SonarQube Integration in NodeJs<\/a>) to download and install Sonar Scanner and SonarQube.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_to_integrate_it_in_NodeJs\"><\/span>How to integrate it in NodeJs<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Assuming that all the above prerequisites are installed in your system now, you need to install two more npm packages by using the following steps.<\/p>\n<p>1) Install globally<br \/>\nnpm install mocha -g<\/p>\n<p>OR<\/p>\n<p>2) install in the current directory<br \/>\nnpm install mocha &#8211;save-dev OR npm install mocha &#8211;save<\/p>\n<p>Which will be shown in your package.json file in the dependencies object below<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-40148\" src=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2024\/10\/undefined-20.png\" alt=\"\" width=\"481\" height=\"95\" \/><\/p>\n<p>After that, you need to add a test script in your package.json file in the script object.<\/p>\n<p>&#8220;test&#8221;: &#8220;mocha &#8211;exit &#8211;colors src\/test\/**\/*.ts&#8221;,<br \/>\nHere is an example of a Test case:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-40147\" src=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2024\/10\/undefined-21.png\" alt=\"\" width=\"525\" height=\"205\" \/><\/p>\n<p>We can also write test cases for third-party API-called functions as well by using the Chai plugin. Now let\u2019s look at the following steps for installing Chai:<\/p>\n<p>1) Install globally<br \/>\nnpm install chai -g<\/p>\n<p>OR<\/p>\n<p>2) install in the current directory<br \/>\nnpm install chai &#8211;save-dev OR npm install chai &#8211;save<\/p>\n<p>Which will be shown in your package.json file in the dependencies object below<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-40146\" src=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2024\/10\/undefined-22.png\" alt=\"\" width=\"407\" height=\"56\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Here_is_an_example_of_a_Test_case\"><\/span>Here is an example of a Test case:<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-40145\" src=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2024\/10\/Undefined-23.png\" alt=\"\" width=\"491\" height=\"291\" \/><\/p>\n<p>Run the below command in the terminal in your project root directory to start the sonar scanner:<\/p>\n<p>npm run test<\/p>\n<p>npm, run coverage \/\/ will show your code coverage report on Sonar. To do this, add the script below to your package.json script object.<\/p>\n<p>&#8220;coverage&#8221;: &#8220;nyc &#8211;reporter=lcov npm run test&#8221;<\/p>\n<p>npm run Sonar<\/p>\n<p>Once the command runs successfully, your dashboard will look like this:<\/p>\n<p>&nbsp;<\/p>\n<p>In short, when businesses conduct software testing well from the beginning of product development, they can ensure bug-free and high-quality software. Teams test the program&#8217;s individual modules using this testing strategy. This strategy allows teams to achieve certain advantages, such as early detection of bugs and correction of the code before costly resolutions in the SDLC. The earliest form of testing that focuses on the code is called unit testing.<\/p>\n<p>Businesses must engage in unit testing from the beginning of the software development lifecycle (SDLC) to guarantee quality and bug-free software.<\/p>\n<p>As a result, defects are discovered, analyzed, and fixed when a straightforward automated test could have detected them.<\/p>\n<p><a href=\"https:\/\/www.solutionanalysts.com\/contact-us\/\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-40143 size-full\" src=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2024\/10\/07-10-2024_Node-JS-03.jpg\" alt=\"cta\" width=\"8075\" height=\"2692\" \/><\/a><\/p>\n<p>If you\u2019re seeking a partner to support your project&#8217;s technical needs and ensure its success, look no further. Contact Solution Analysts to arrange a free consultation call, where we can discuss how our technical expertise can be tailored to meet your specific requirements and help you achieve your goals.<\/p>\n<div class=\"card mobile-card\">\n<p><img decoding=\"async\" class=\"profile-pic\" style=\"width: 9em;\" src=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2024\/10\/hitesh-sapariya.jpg\" alt=\"Profile Picture\" \/><\/p>\n<div class=\"card-content\">\n<p><span style=\"font-weight: 400;\"><b>Hitesh Sapariya<\/b><\/span><\/p>\n<p class=\"title\">Technical Lead<\/p>\n<p>Hitesh Sapariya is a Technical Leader at Solution Analysts. He brings over 14 years of experience and a genuine passion for software development to his role. He is an adept troubleshooter with a strong attention to detail, his expertise spans a diverse range of technologies, including Next.js, Node.js, Angular, React, MongoDB, MySQL, and PostgreSQL.<\/p>\n<div class=\"social-links\"><a href=\"https:\/\/www.linkedin.com\/in\/hitesh-sapariya\/\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2024\/08\/link.png\" alt=\"LinkedIn\" \/><\/a><\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In the ever-evolving world of software development, maintaining code quality is crucial. This is where tools like SonarQube and practices like unit testing come into play. Together, they ensure that the code is functional, clean, maintainable, and free from bugs. This guide will walk you through writing test cases in Node.js, integrating them with SonarQube, [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":40141,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[128],"tags":[563,562,564,401],"class_list":["post-40140","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-js","tag-benefits-of-sonarqube","tag-nodejs-write-test-case","tag-nodejs-write-test-case-and-code-coverage-using-sonarqube","tag-sonarqube"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts\/40140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/comments?post=40140"}],"version-history":[{"count":12,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts\/40140\/revisions"}],"predecessor-version":[{"id":40484,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts\/40140\/revisions\/40484"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/media\/40141"}],"wp:attachment":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/media?parent=40140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/categories?post=40140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/tags?post=40140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}