How Setting Up Unit Tests with TypeScript

How Setting Up Unit Tests with TypeScript

Unit testing is one of the most valuable and tedious measures of your codebase at the same time. Writing tests is time-consuming, and it could be another sort of “sub-application” near to your main code. If correctly handled, unit tests may save your life and your mind when debugging code. We all know it! This approach will help you to set up an opinionated unit testing environment with TypeScript that also works for JavaScript projects. You can create your next project following this approach, or you can adapt your existing app with a “quite-zero” effort. This approach is both for a client and a server app. Setting up an enterprise test environment that works for server and client developers. How Setting Up Unit Tests with TypeScript

Unit testing is one of the most valuable and tedious measures of your codebase at the same time. Writing tests is time-consuming, and it could be another sort of “sub-application” near to your main code. If correctly handled, unit tests may save your life and your mind when debugging code. We all know it!

This approach will help you to set up an opinionated unit testing environment with TypeScript that also works for JavaScript projects. You can create your next project following this approach, or you can adapt your existing app with a “quite-zero” effort. This approach is both for a client and a server app.

Setup

Let’s start with the project folder structure.

root
| node_modules
| src
| test
| package.json
| tsconfig.json

I considered a dedicated test folder in which you will organize your tests following the same internal structure of the src folder. It is not strictly required. It also works if you want to maintain a common approach and keeping the tests in the component folder for the client-side projects. It will work as well.

As usual, for JavaScript/TypeScript projects, we need some dependencies.

npm install --dev ts-node mocha @testdeck/mocha nyc chai ts-mockito

or

yarn add -D ts-node mocha @testdeck/mocha nyc chai ts-mockito

What we’ll get:

  • ts-node: allow to run .ts file by transpiling them on the fly.
  • *mocha: *we’ll use it as the test runner.
  • *@testdeck/mocha: *it will allow mocha to interpret TypeScript classes as test suites.
  • nyc: it will generate the coverage report.
  • *chai: *the expectation library we’ll use.
  • ts-mockito: a supercool stubbing and mocking library inspired by mockito for Java.

nodejs javascript mocha node typescript

Bootstrap 5 Complete Course with Examples

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

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Serverless Node TypeScript

Serverless Node TypeScript is a starter kit to write serverless applications by using the Serverless Framework in Node.js and TypeScript. An Apollo GraphQL server and a REST endpoint come configured out of the box

A template for Node.js & TypeScript.

typescript-starter .A template for Node.js & TypeScript.

Drivine is a graph database client for Node.js and TypeScript

Drivine is a graph database client for Node.js and TypeScript .Best and fastest graph database client (Neo4j & AgensGraph) for NestJS, Node.js & TypeScript.

A starter kit to write serverless applications in Node.js and TypeScript

Serverless Node TypeScript is a starter kit to write serverless applications in Node.js and TypeScript and deploying them by using the Serverless Framework. It comes with several configurations set up out of the box, such as MongoDB, a GraphQL API server, a REST endpoint and Domain Events via AWS SNS.

NodeJS Instagram private API client. Written in TypeScript

NodeJS Instagram private API client .NodeJS Instagram private API client. Written in TypeScript