JavaScript is a beautiful language. You must believe I’m crazy. Maybe you’re crazy for agreeing with me. But why would I say something like this? As a language, JavaScript gives you no support whatsoever. It bites your head off if you give it the slightest chance, and it has bizarre error messages if left unhandled. So you tell me, why is it beautiful? Because it creates good, responsible, and intelligent developers. By worrying about getting your head ripped off by the smallest mistake, you adapt and overcome. The skill gained has less in common with programming and much more with a programmer’s state of mind. Because you are getting used to not having an invisible force guide you through the code. Instead, you rely on yourself, and your own skill. Hence, me stating something as crazy as I did above. Why then, does this create good programmers? A good programmer is responsible, meticulous and reliable. Programmers like these make sure their code works. No matter which environment, or which machine. These masters of their craft, always cover code with tests, to ensure their work is valid. They have my utmost respect. I would like to believe they have your as well. Baby steps. To lay the foundation of what a basic test case would look like let’s create a simple function. Calling this function we can see the result is 6. Because we know basic math, it makes perfect sense. But what if the function is really complex? Let’s make sure to write a test case to ensure the function is valid no matter what. See this? We’re defining the values to add, and creating their sum. Then we call assigning it to another variable. Having done this, we’re ready to test the equality. What’re we expecting? Well, should be equal to , if the function we created works as expected. Running this piece of code you should see the following get logged to the command line: addTwoNumbers() sum1 sum2 addTwoNumbers() should return the sum of its two parameters.Expect 6 to equal 6.Passed. Congratulations, you’ve written your first unit test! The act of unit testing lies in writing tests for small units of code. Hence the name. Meaning you’ll write individual test cases for validating the behavior of functions, methods, and objects. Exactly like we did above. What if we add a deliberate bug to our code? For the heck of checking if the unit test will fail gracefully. Change the function to: addTwoNumbers() function addTwoNumbers(x, y) {return x + x; // deliberate bug!} Run the unit test once again and you’ll see it fail like it should. addTwoNumbers() should return the sum of its two parameters.Expect 6 to equal 10.Failed. A bit of theory. A unit test consists of three parts. Arrange Act Assert From their names alone it is easy to comprehend what they stand for. Let’s break it down while looking at some code. In the first part we all necessary preconditions and inputs. You can see we defined the variables to add and the sum of these variables. The second step is to on the function, object or method under test. Lastly, we that the expected results have occurred. arrange act assert You may find the word assert a bit overwhelming. As a non native English speaker, I sure did, when I first heard it. Not to worry, it only means You are asserting a truth, meaning you claim something is true. As simple as that. to claim. are a tool to do basic sanity checking for programmer errors. Assertions — Marijn Haverbeke, Eloquent JavaScript Want to write your own assertion? Sure you do. Check this out. On line 1 we instantiate a new object named , immediately adding a method called . If the two passed parameters are not equal, the function will throw an error. That’s it, that’s all the logic in the whole method. Now, on line 27 we’re wrapping the stage in a try catch block, and calling the method. Only if the values are not equal will an error be thrown and caught in the catch block. Otherwise, the thread of execution will continue and log to the console. Go ahead and try it out! assert equal assert assert.equal() 'Passed.' How about we get serious? The examples above have shown the fundamentals of testing in general. Also pointing out the required mindset needed to succeed in the field of programming. It’s time to bring out the big guns. You’ll rarely ever use the code above in a production environment. Nevertheless, it’s crucial in the understanding of what is to come. You can use many various tools to write tests for Node.js applications in production. An example is the built-in assertion library. Yes, Node does have assertions baked in. Only change line 1. By changing out our custom assert object for the built-in Node module our code works exactly the same. The default assertions in Node are extremely powerful, you can take a longer look at them . here However, tools like and are the bread and butter of testing Node.js applications. Mocha Chai is a feature-rich JavaScript test framework running on and in the browser, making asynchronous testing and . Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases. — mochajs.org Mocha Node.js simple fun is a BDD / TDD assertion library for and the browser that can be delightfully paired with any javascript testing framework. — chaijs.com Chai node Let’s check this out. First of all you’ll need to init a new Node project by hooking it up to npm. Open up a terminal window in your directory of choice, and run: npm init Please feel free to enter through all of the prompts. When you’ve done that, you’ll need to install the required modules. npm install --save-dev mocha chai Now you can open up your code editor of choice and start by adding files like so: > test test.js- addTwoNumbers.js One directory with a file, and another file named in the root of the directory. Go ahead and paste the function into the file like so: test test.js addTwoNumbers.js addTwoNumbers addTwoNumbers.js Don’t forget to export it to be able to require it later on. Now we can start with the fun part. Open up and start by laying the foundation for our tests. test.js At the beginning of the file we need to require both and . Look at the way we required , only grabbing . comes with three types of interfaces for creating assertions. They are all valid. Which one you choose is only preference. I feel like suits me just fine. Don’t get mindblown by the test syntax. It’s created to simulate natural human speech patterns. The block creates a test environment. The blocks defines test cases which need to pass. Reading it out loud sounds rather fine. Describe , it should add two numbers. Makes perfect sense! Can you now see why testing is important apart from making sure the code works? A test is in itself documentation. Writing a test will explain what the code does. Every other developer working on the code base will have no issue understanding it in no time. Chai addTwoNumbers Chai expect Chai expect describe it addTwoNumbers() All that’s left is to run the tests. Add in the scripts section of your and you’ll be ready to go! "test": "mocha" package.json , your should look like this: Hint package.json Jump back to your terminal window and run . You’ll see an awesome interface with some green text saying there is 1 passing test! npm test Taking it all in. You’ve now experienced the natural process of covering code with tests. All the examples have been showing unit tests, which is more than enough to begin with. When you get comfortable with these concepts, understanding integration and end-to-end testing will be like a walk in the park. But that’s a topic for another article. I urge you to continue playing with these testing tools. Try to include them into your existing development process. You will see an overall improvement in code quality and mental health. Trust me, having peace of mind with a completely green test suite does wonders for the nerves. If you want to take a look at all the code we wrote above, . Or if you want to read my latest articles, head over here. here’s the repository _Read the latest stories written by Adnan Rahić on Medium. Software engineer @bookvar_co. Coding educator @ACADEMY387…_medium.com Latest stories written by Adnan Rahić - Medium Hope you guys and girls enjoyed reading this as much as I enjoyed writing it. Do you think this tutorial will be of help to someone? Do not hesitate to share. If you liked it, click the clap below so other people will see this here on Medium.