A Clean and Simple Approach for Unit Testing in TypeScript or JavaScript

A Clean and Simple Approach for Unit Testing in TypeScript or JavaScript

No matter your project size or the frameworks you’re using: just write testing code that matters and works using TypeScript and JavaScript.

No matter your project size or the frameworks you’re using: just write testing code that matters and works.

I’ve seen so many libraries and projects having hard times dealing with unit test code.

Let’s be honest, I was also that kind of guy that flee when he saw the unit test requirements. The thing is: I was just not mentally wired to make code that is testable.

I had a triangle mould (my testing requirements) and I was trying to put a square into it. Good luck!

And I guess this is mostly why people are afraid of testing.

They took the easy path of implementation with a lot of coupling which gave them a triangle, and then they try to make tests battery with square, which is close to impossible. The result is that they are disgusted and just do not make tests.

Maybe you’re writing tests in TypeScript, you’re not transpiling code for testing or you get some strange red message, not really related to your code.

Maybe you’re having issues with a dependency wired to the network, it slows, it fails randomly.

It looks like you’re testing the same thing, multiple times, without any value-added.

You’re failing to test and while we might think that this is because testing is complicated, the truth is way more simple: This is frequently a problem of software architecture.

Why it is so hard to test some code?

It is hard because you have tight coupling on implementation details code. An example of such code could be a code responsible to fetch a pricing map configuration from a remote service. This pricing map could be located on a CMS or just in a simple file on the system.

If you write your code as such, there are a lot of problems that might happen

import configService from "../services/config-service";

export async function getFinalPrice(quantity: number) {
  const pricingMap = await configService.getPricingMap();
  return pricingMap.get(quantity);
}
  1. The code is async as you need to fetch the configuration each time, I don’t talk about caching you could use inside here, the initial conception is just wrong for proper testing.
  2. You’re coupling your module file to an external dependency, which will be hard to test.
  3. You’re coupling your module to other modules, which is bad for reusability.

If you start having to mock modules importation to test your code it’s because you broke one of the core rules of dependencies injection and have tightly coupled code.

Sometimes it’s okay, on some file of the application but not every part of it.

As the code is tightly coupled, you need to make some magic tricks to mock the code that is used, this results in some highly hacky techniques, such as mocking imports.

You end up installing tons of libraries just to bypass a misconception you did to develop quickly, it’s also hard to just plug and replace.

programming javascript typescript

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Object-Oriented Programming with Javascript — using Typescript

Object-Oriented Programming With JavaScript — Using Typescript. TypeScript lets you write JavaScript the way you really want to. TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. TypeScript is pure Object-oriented with classes, interfaces, and statically typed like C# or Java.

What is TypeScript? Why TypeScript? and Why Not TypeScript?

TypeScript extends JavaScript by adding Types. There are many great reasons to switch to TypeScript. Especially if your team uses JavaScript. There are some reasons to not use TypeScript as there are with any language or framework.

TypeScript Tutorial For JavaScript Developers - TypeScript Basics

TypeScript Tutorial For JavaScript Developers - TypeScript Basics. I will show you guys 4 example of JavaScript code, and how to convert it to TypeScript. This is a typescript beginners tutorial.

TypeScript: A Superset of JavaScript

Multi-paradigm, Functional, Generic, Imperative, Object-Oriented. In this article, I have shared the essential information about TypeScript programming language. This article contains a basic overview, brief history, features and applications of TypeScript. TypeScript is getting more popular in Full-stack development. In terms of popularity, in the last two years TypeScript pulled ahead of Ruby, Shell, and C.

How an Anti-TypeScript “JavaScript Developer” Like Me Became a TypeScript Fan

In this article, I will discuss my journey from being an anti-TypeScript developer to a developer who now couldn’t think of going back to the plain JavaScript world. Let's see How an Anti-TypeScript “JavaScript Developer” Like Me Became a TypeScript Fan.