Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Unit testing: The Beginner's guide

Posted on Oct 9 Unit Testing: Beginners guideThink about it, how is it that builders are able to erect a tall, straight wall? By checking each block laid with a plumb line to make sure they are on a straight line. That is what every developer does to each Function, method and code to make sure they give the expected output. Unit testing is one of the early precautions taken during the software development process. It is a process in which the smallest, testable part of an application is scrutinized to see if it works as intended. It involves the isolation of code such as function or method into small independent units and testing them individually. The idea is to pick these functions and methods and isolate them by replacing the external dependencies like the network connection and database with dummy objects. As a developer, you might not be able to test every line of code as this will waste a lot of your time, it is important to focus on codes that are critical to the performance of your application.Unit testing is a component of test-driven development (TDD). TDD is a methodology that involves three critical activities that are interwoven. They include unit testing, coding and design (in the form of refactoring of code). TDD is all about Red/Green testing. This means that we would write the test, fail the test, and then finally write the code to pass the test. Properties of a good unit testa. It helps in the early detection of bugsb. It makes it easier for a developer to change the code basec. It makes fixing errors cheaperd. Improves code qualitye. Faster development cycle as it reduces the time for debuggingWe will look at how to write a simple unit test in JavaScript using Jest.To do this you need to make sure you already have node and vscode installed in your computer. Open your Vscode terminal and type the following commanda. npm init –y this helps you to install the node module dependencies and the package.json fileb. npm i –D jest this installs the jestc. Create a file named function.js. This is where you write the function you want to test. In this case, the name of the function is “add”. You will notice that it is isolated and packaged inside an object called functions. This function object is then exported to the file where it is going to be testedd. Import the function into the test function, function.test.jsIn line 1, the code is imported from the function file.In line 3, the test method is used to test the function imported from the function file.The test method takes two parameters, the first parameter is a string and tells us what to expect. The second parameter is a function that uses the expect method to test the add function. In this case, the add function accepts the required parameters, 2 and 2. This is then compared to the output which is 4 using the toBe method.e. At the terminal, type npm test to test the code if it passed or failed.We might want to just include the function in the test function rather than importing it from an outside file. For ExampleTemplates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse betpido - Oct 9 Dawy Dentalz - Oct 9 tech digit - Oct 9 Rahul Karda - Oct 9 Once suspended, ozor will not be able to comment or publish posts until their suspension is removed. Once unsuspended, ozor will be able to comment and publish posts again. Once unpublished, all posts by ozor will become hidden and only accessible to themselves. If ozor is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to Ozoemena. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag ozor: ozor consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging ozor will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Unit testing: The Beginner's guide

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×