What is unit testing?

Back to basics

Let’s all agree on a common definition of unit testing to start with and quote the esteemed  Martin Fowler in one of his blog articles dedicated to unit testing, in which he states having encountered no less than 24 definitions of unit testing 😱 

In the end, this is the definition which he believes to be most accurate, and we would tend to agree:

“Despite the variations, there are some common elements. Firstly there is a notion that unit tests are low-level, focusing on a small part of the software system. Secondly, unit tests are usually written these days by the programmers themselves using their regular tools - the only difference being the use of some sort of unit testing framework. Thirdly, unit tests are expected to be significantly faster than other kinds of tests.”

What is unit testing?

It’s testing the smallest entity in your code in order to significantly improve the quality of your code, avoid regressions and provide documentation.

Why should you bother testing anyway? 

Clean and bug free code means performant software and happy users! Plus, you will feel more confident about your coding and will be even bolder and more creative regarding your future development projects, ain’t that nice 😏

If you want to know more about the benefits of unit testing, we can only encourage you to check our previous articles.

Your Unit Test Step by Step

Here is an easy illustrated tutorial of how you should proceed to conduct your first Unit Test in Javascript in VS Code.

  1. Install Jest

First thing you will need to do is to install your testing framework, here we are using Jest as it is one of the most convenient frameworks for Javascript. To install it, simply type npm install jest and link a dedicated test file to it. 

  1. Choose your function

As previously said, unit testing is only about testing the smallest entity of your code so let’s take a function to perform the test, here : isEmailValid

import { isEmailValid } from ‘./index.js’


  1. Create a test file and import your function

You could test in the same file but the best practice is to create a separate test file for better clarity for your colleagues and your future self 😉

Then, we will need to import the function. Once it’s done you may write your test for the function is email valid as below.  

function isEmailValid(email){
	if (typeof email !== “string”){		
  	return false	
  var re = /^(([^()\[\]\\.,;:\s@”]+(\.[^()\[\]\\.,;:\s@”]+)*)|(“.+”))@((\[[0-9]{1,3}\.
  return re.test(email.toLowerCase())
  1. Start Testing

If you are testing in Jest as we are, you will need to create the “describe” parameter to be compliant with the testing frameworks of Javascript as follows: 

describe(“isEmailValid”, () => {	
	test(“Simple email address”, () => {		
  	const result = isEmailValid(“contact@circleci.com”);
  test(“Email address with underscore”, () => {		
  	const result = isEmailValid(“ex_ponicode@circleci.com”);		
  test(“Email address with dots”, () => {		
  	const result = isEmailValid(“developer.experience@circleci.com”);		
  test(“Email address with dots and underscore”, () => {		
  	const result = isEmailValid(“awesome_developer.experience_team@circleci.com”);	

  1. Don’t forget the edge cases

You want to make sure that edge cases are covered in your tests, so here are some examples of email addresses that wouldn’t do and consequently wouldn’t pass the tests.  

test(“Not valid email address”, () => {
	const result = isEmailValid(“notWorking@circleci_com”);

test(“Not valid email address”, () => {
	const result = isEmailValid(“still_no@[].com”);

test(“Not a string”, () => {
	const result= isEmailValid(1);

  1. Run test 

It’s time to run your tests and if you have followed the previous steps you should get the same result as shown here below: 


Accelerating the process using unit teast generation tools

Now let’s show you how you can watch your unit tests — almost — write themselves with our awesome unit test generation solution🦄

First you’ll need to install the Ponicode VS Code extension on the marketplace. Once it’s done link it and copy the token appearing at the bottom right of your screen right here ⬇️ 


Open the file you’d wish to test. On the left of your functions, you will start seeing little ponies, that means that the tool is able to make suggestions for these. Right click on the function you want to test and select “Ponicode Unit Test”: 


A window will appear on the right of your screen showing the suggestions our AI-powered unicorn assumes are relevant to test; choose the inputs you would like to test and press the little thunderbolt to run them. It only takes a few seconds to run and see if they pass or not! 


Once you’re done, you can find all your tests in the automatically generated test file which should look like this: 


Voilà! Now you know everything, you can go practice on your first unit tests 🎉

If you are looking for more guidance on how to perform your unit tests, we can only recommend you to watch this comprehensive but not too long video tutorial again in Javascript. 


Of course if you are curious about our AI-powered solution for unit testing, you will find plenty of videos on our YouTube channel but if you’re in a hurry, here is a 1-min demo of the steps we’ve detailed below, enjoy and write beautiful code! 

Quick link to Ponicode platform

Also on the hub

Ready to write beautiful code?

Smart and simple unit testing assistant. Now available for free.

Try it now

Solutions for JS, TS, Java and Python

Lines Footer
Flexing Unicorn by Ponicode