Improving your testing code coverage in Javascript

Marcos Henrique da Silva
17 min readNov 17, 2021

Actually, in Typescript. It’s 2021, you should be using it already, period.

Hello everyone, welcome back! And as usual, source code in the end of the article!

Photo by ThisIsEngineering from Pexels

In this article I am aiming to write a bit about unit testing, specifically targeting testing coverage. It is supposed that you are a beginner to intermediary level with Node.JS and knowing basics of Typescript and any common Node.JS framework. I will be specifically talking about NestJS and their Jest integration. You can take the ideas from this article and adapt to your project in a case that you are using a different framework.

Why Typescript?

Typescript is commonly used in consolidate enterprise companies and it works really well when you are working in a mixed team full of juniors/mid/seniors. It allows you to guarantee some code standards that sometimes using pure Javascript might get easier to bypass some quality gates. If you need any introduction to setup your Typescript project, please read my tutorial.

The majority of frameworks that you use already supports Typescript by default, and usually they already have Typescript configured. Typescript also make it easy to apply SOLID principles. Rather if you like Typescript or hate it, it is also a common job interview question “How about Typescript? Are you comfortable working with it?”.

Why NestJS?

Mostly because I work with it and it is easier and faster to scaffold a project to write something about in Medium. I really believe that I should write about things that I use and do in my daily working base rather than writing about something I don’t have skin in the game.

Why Jest?

Mostly due the fact that NestJS has Jest integration built in by default. You can use it anywhere and also with your frontend app like React.

Let’s start!

Before starting to code, let’s take a quick review of what the final project would look like:

  • A scaffolded backend project made with NestJS
  • GraphQL by default (yes, you will have unit testing and a plus of GraphQL)
  • A basic CRUD using mongoose

--

--