{"id":12208,"date":"2017-06-08T09:30:00","date_gmt":"2017-06-08T09:30:00","guid":{"rendered":"https:\/\/viewmyprojects.com\/winwirewp\/?p=12208"},"modified":"2023-11-30T06:16:57","modified_gmt":"2023-11-30T06:16:57","slug":"the-birth-of-karma-a-javascript-test-runner","status":"publish","type":"post","link":"https:\/\/viewmyprojects.com\/winwirewp\/blog\/the-birth-of-karma-a-javascript-test-runner\/","title":{"rendered":"The birth of Karma- A Spectacular JavaScript Test Runner"},"content":{"rendered":"\n<p>When picking up a new tool, it is important to understand where it comes from and why it is built. This section will give us a background of the origin of Karma.<\/p>\n\n\n\n<p><strong>The Karma difference<\/strong><\/p>\n\n\n\n<p>Karma was created by Vojta J\u00edna. The project was originally called Testacular. In Vojtech J\u00edna\u2019s thesis, he discusses the design, purpose, and implementation of Karma.<\/p>\n\n\n\n<p>In his thesis (JavasScript Test Runner,&nbsp;<a href=\"https:\/\/github.com\/karma-runner\/karma\/raw\/master\/thesis.pdf\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/karma-runner\/karma\/raw\/master\/thesis.pdf<\/a>), he describes Karma as follows:<\/p>\n\n\n\n<p>\u201ca test runner that helps web application developers to be more productive and effective by making automated testing simpler and faster. In fact, I have a much higher ambition and this thesis is only a part of it \u2013 I want to promote Test Driven Development (TDD) as \u201cthe\u201d way to develop web applications because I believe it is the most effective way to develop high-quality software.\u201d<\/p>\n\n\n\n<p>Karma has the ability to easily and automatically run JavaScript unit tests on real browsers. Traditionally, tests were run by launching a browser manually and checking for results by continually clicking on the refresh button.<\/p>\n\n\n\n<p><strong>Installing Karma<\/strong><\/p>\n\n\n\n<p>It\u2019s time to start using Karma. Installations and applications are constantly changing. The following guide is intended to be brief; you can go to the Karma website at&nbsp;<a href=\"http:\/\/karma-runner.github.io\/\" target=\"_blank\" rel=\"noreferrer noopener\">http:\/\/karma-runner.github.io\/<\/a>&nbsp;and find the latest instructions there.<\/p>\n\n\n\n<p>The focus of this section will be the specific configuration used in this book and not an in-depth installation guide.<\/p>\n\n\n\n<p><strong>Installation prerequisites<br><\/strong><br>To install Karma, we will need to have Node.js on our computer. Node.js runs on Google\u2019s V8 engine and allows JavaScript to be run on several operating systems.<\/p>\n\n\n\n<p>Developers can publish node applications and modules using NPM (Node Package Manager). NPM allows developers to quickly integrate applications and modules into their applications.<\/p>\n\n\n\n<p>Karma runs and is installed through the npm package; therefore, we need Node.js before we can use or install Karma. To install Node.js, go to&nbsp;<a href=\"http:\/\/nodejs.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">http:\/\/nodejs.org\/<\/a>&nbsp;and follow the installation instructions.<\/p>\n\n\n\n<p>Once we have Node.js installed, let\u2019s type the following command in the Command Prompt to install Karma:<\/p>\n\n\n\n<p>$ npm install karma -g<br>The preceding command uses npm to install Karma globally using -g. This means that we can use Karma on the Command Prompt by simply typing the following:<br>$ karma \u2013version<br>By default, installing Karma will install karma-chrome-launcher and karma-jasmine as dependencies. Ensure that these modules are installed globally as well.<\/p>\n\n\n\n<p><strong>Configuring Karma<\/strong><\/p>\n\n\n\n<p>Karma comes equipped with an automated way to create a configuration file. To use the automated way, type the following command:<br>$ karma init<br>Here is a sample of the options chosen:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"645\" height=\"433\" src=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/Karma-V.webp\" alt=\"\" class=\"wp-image-18648\" srcset=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/Karma-V.webp 645w, https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/Karma-V-300x201.webp 300w\" sizes=\"auto, (max-width: 645px) 100vw, 645px\" \/><\/figure>\n\n\n\n<p><strong>Customizing Karma\u2019s configuration<br><\/strong><br>The following instructions describe the specific configuration required to get Karma running for the project. Customization includes the test framework (Jasmine), the browser (Chrome) to test with, and the files to test. To customize the configuration, open karma.conf.js and perform the following steps:<br>1. Ensure that the enabled framework says jasmine using the following code:<br>frameworks: [\u2018jasmine\u2019],<br>2. Configure the test directory. Note that the following definition needs to include the tests that are required to be run along with any potential dependencies. The directory that will hold our tests is \/test\/unit\/:<br>files: [\u2018test\/unit\/**\/*.js\u2019],<br>3. Set the test browser to Chrome, as follows. It will then be initialized and will run a popup after every test:<br>browsers: [\u2018Chrome\u2019],<br>Confirming Karma\u2019s installation and configuration<br>To confirm Karma\u2019s installation and configuration, perform the following steps:<br>1. Run the following command to confirm that Karma starts with no errors:<br>$ karma start<br>2. The output should be something like this:<br>$ INFO [karma]: Karma v0.12.16 server started at<br>http:\/\/localhost:9876\/<br>3. In addition to this, the output should state that no test files were found:<br>$ WARN [watcher]: Pattern \u201ctest\/unit\/**\/*.js\u201d does not match any file.<br>The output should do this along with a failed test message:<br>$ Chrome 35.0.1916 (Windows 7): Executed 0 of 0 ERROR<br>(0.016 secs \/ 0 secs)<\/p>\n\n\n\n<p><strong>Note<\/strong><\/p>\n\n\n\n<p>An important point to note is that we will need to install jasmine-core globally on the system, or else Karma will not run successfully.<br>This is expected as no tests have been created yet. Continue to the next step if Karma starts, and we will see our Chrome browser with the following output:<a href=\"https:\/\/www.winwire.net\/wp-content\/uploads\/2017\/06\/Karma-V.0.jpg\" target=\"_blank\" rel=\"noopener\"><\/a><\/p>\n\n\n\n<p><strong>Common installation\/configuration issues<br><\/strong><br>If the Jasmine or Chrome launcher are missing, perform the following steps:<br>1. When running the test, an error might occur saying missing Jasmine or Chrome Launcher. If you get this error, type the following command to install the missing dependencies:<br>$ npm install karma-jasmine -g<br>$ npm install karma-chrome-launcher -g<\/p>\n\n\n\n<p>2. Retry the test and confirm that the errors have been resolved.<br>In some cases, you might not be able to install npm_modules globally using the -g command. This is generally due to permission issues on your computer. The following is what you need to do to provide permissions (sudo\/administrator):<\/p>\n\n\n\n<p>1. The resolution is to install Karma directly in your project folder. Use the same command without -g to do this:<br>$ npm install karma<\/p>\n\n\n\n<p>2. Run Karma using the relative path:<br>$ .\/node_modules\/karma\/bin\/karma \u2013version<br>Now that Karma is installed and running, it\u2019s time to put it to use.<\/p>\n\n\n\n<p><strong>Testing with Karma<br><\/strong><br>In this section, we will create a test to confirm that Karma is working as expected. To do this, perform the following steps:<\/p>\n\n\n\n<p>1. Create the test directory. In the Karma configuration, tests were defined in the following directory:<br>files: [ \u2018test\/unit\/**\/*.js\u2019],<\/p>\n\n\n\n<p>2. Go ahead and create the test\/unit directory.<\/p>\n\n\n\n<p>3. Create a new firstTest.js file in the test\/unit directory.<\/p>\n\n\n\n<p>4. Write the first test as follows:<br>describe(\u2018when testing karma\u2019, function (){<br>it(\u2018should report a successful test\u2019, function (){<br>expect(true).toBeTruthy();<br>});<br>});<\/p>\n\n\n\n<p class=\"blog-detail-list\">The preceding test uses the Jasmine functions and has the following properties:<br>\u2022 Describe: This provides a brief string description of the test suite, the things that will be tested.<br>\u2022 it: This provides a brief string of the specific assertion called a test spec<br>\u2022 expect: This provides a way to assert values<br>\u2022 toBeTruthy: This is one of the several properties of an expectation that can be used to make assertions<\/p>\n\n\n\n<p><strong>Confirming the Karma installation<\/strong><\/p>\n\n\n\n<p>Now, the initial set up and configuration of Karma is complete. Here is a review of the steps:<br>1. We installed Karma through the npm command.<br>2. We initialized a default configuration through the&nbsp;<strong>karma init<\/strong>&nbsp;command.<br>3. Next, we configured Karma with Jasmine and a&nbsp;<strong>test\/unit<\/strong>&nbsp;test directory.<br>4. We started Karma and confirmed that it could be opened with Chrome.<br>5. Then, we added a Jasmine test,&nbsp;<strong>firstTest.js<\/strong>, to our&nbsp;<strong>test\/unit<\/strong>&nbsp;test directory.<br>6. Karma recognized that&nbsp;<strong>firstTest.js<\/strong>&nbsp;had been added to the test directory.<br>7. Finally, Karma executed our&nbsp;<strong>firstTest.js<\/strong>&nbsp;and reported our output.<\/p>\n\n\n\n<p><strong>In a nutshell<br><\/strong><br><em>Karma is undoubtedly a preferred test runner for projects written with AngularJS and is smoothly making its way to larger acceptance within the greater JavaScript community. Its plugin architecture makes it easily compliant to other test suites and reporters, all of which add value to the core of Karma. In the continuous integration environment, Karma sparkles as an indispensable tool to dev teams, providing an easy and reliable way to alter existing code and craft new code as part of Test-driven development. It is unlikely that a day goes by without this tool running on my system.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When picking up a new tool, it is important to understand where it comes from and why it is built. This section will give us a background of the origin of Karma. The Karma difference Karma was created by Vojta J\u00edna. The project was originally called Testacular. In Vojtech J\u00edna\u2019s thesis, he discusses the design,&hellip; <a class=\"more-link\" href=\"https:\/\/viewmyprojects.com\/winwirewp\/blog\/the-birth-of-karma-a-javascript-test-runner\/\">Continue reading <span class=\"screen-reader-text\">The birth of Karma- A Spectacular JavaScript Test Runner<\/span><\/a><\/p>\n","protected":false},"author":62,"featured_media":16658,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_eb_attr":"","_uag_custom_page_level_css":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-12208","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","entry"],"acf":[],"featured_image_src":"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/The-birth-of-Karma-A-Spectacular-JavaScript-Test-Runner-graphic.webp","author_info":{"display_name":"Ankaraju","author_link":"https:\/\/viewmyprojects.com\/winwirewp\/author\/ankaraju\/"},"views":3572,"uagb_featured_image_src":{"full":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/The-birth-of-Karma-A-Spectacular-JavaScript-Test-Runner-graphic.webp",800,440,false],"thumbnail":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/The-birth-of-Karma-A-Spectacular-JavaScript-Test-Runner-graphic-150x150.webp",150,150,true],"medium":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/The-birth-of-Karma-A-Spectacular-JavaScript-Test-Runner-graphic-300x165.webp",300,165,true],"medium_large":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/The-birth-of-Karma-A-Spectacular-JavaScript-Test-Runner-graphic-768x422.webp",750,412,true],"large":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/The-birth-of-Karma-A-Spectacular-JavaScript-Test-Runner-graphic.webp",750,413,false],"1536x1536":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/The-birth-of-Karma-A-Spectacular-JavaScript-Test-Runner-graphic.webp",800,440,false],"2048x2048":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/The-birth-of-Karma-A-Spectacular-JavaScript-Test-Runner-graphic.webp",800,440,false],"post-thumbnail":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/The-birth-of-Karma-A-Spectacular-JavaScript-Test-Runner-graphic.webp",800,440,false]},"uagb_author_info":{"display_name":"Ankaraju","author_link":"https:\/\/viewmyprojects.com\/winwirewp\/author\/ankaraju\/"},"uagb_comment_info":0,"uagb_excerpt":"When picking up a new tool, it is important to understand where it comes from and why it is built. This section will give us a background of the origin of Karma. The Karma difference Karma was created by Vojta J\u00edna. The project was originally called Testacular. In Vojtech J\u00edna\u2019s thesis, he discusses the design,&hellip;&hellip;","_links":{"self":[{"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/posts\/12208","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/users\/62"}],"replies":[{"embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/comments?post=12208"}],"version-history":[{"count":2,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/posts\/12208\/revisions"}],"predecessor-version":[{"id":18675,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/posts\/12208\/revisions\/18675"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/media\/16658"}],"wp:attachment":[{"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/media?parent=12208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/categories?post=12208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/tags?post=12208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}