Wrote my first unit test using Jest!

So finally, today was the day when I started implementing the unit test cases for the application I have been working on since some time. The development for the application is not over yet – meaning it doesn’t yet do everything it is supposed to do, but it is at a level where I call it “usable”. Having addressed the linting errors and the code review feedback, I think now would be the good time to implement unit test cases for the current code base. Eventually this would be required for the DevOps build process.

It is not a huge code base to manage currently and something deep inside my mind told me that now would be the good time to start writing unit test scripts. I will be honest here, writing unit test cases is completely new to me and this is literally the first time I am doing this. Since I am using NodeJS, I chose Jest Javascript testing framework because it was highly recommended. So much so that I was intrigued to dig into it already. I did a bit of research last week to get initial impressions of Jest but it was not the correct time to use it then.

Writing unit test cases in Jest for NodeJS is really easy. I am sure it is the same for other areas like front-end frameworks, Typescript and Babel. Jest uses “matchers” to match the output of functions or expressions with expectations. It expresses these tests in a similar syntax. Below is an example of a sample test.

This is a describe function which describes the test being performed. describe takes a callback function as the 2nd parameter which defines the test step. The it function has a test step level description, which comes in very handy when the test results are printed on the console. it takes in the 2nd parameter as a callback function which establishes the rule to decide whether the test was successful or not. The rule goes like this: expect some expression toBe this value.

import { greeter } from 'greeter';

describe('Greet test.', () => {
  it('should greet with name', () => {
    expect(greeter.greet('Sumeet')).toBe('Hello, Sumeet');

The expression used here is a function greet on greeter class. It basically accepts string as a parameter and returns the same appended with Hello, . In this test, we are passing the sample string as Sumeet after which we want to make sure the function correctly returns Hello, Sumeet.

toBe is just one example and there are so many more matchers which are very helpful to establish these rules. I obviously need to spend more time on unit test cases before I proceed to write integration test cases. I will do another post to register any interesting observations here.


Tagged as: , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s