Node.js Typescript and Jest

Setting Up a Modern Node.js Project with TypeScript and Jest

In today’s development world, it’s almost impossible to imagine a Node.js project without TypeScript and a testing framework, like Jest TypeScript brings type safety to your code, while Jest ensures your application is reliable through comprehensive testing. This guide will walk you through setting up a Node.js project with TypeScript and Jest, so your development environment is optimized for success.

Step 1: Initialize Your Node.js Project

Begin by creating a new directory for your project, navigate to it in your terminal, and initialize a Node.js project:

npm init -y

This command will generate a package.json file, which acts as the backbone for managing your project’s dependencies.

Step 2: Install TypeScript and Essential Tools

To integrate TypeScript into your Node.js project, install TypeScript and some necessary tools:

npm install typescript ts-node @types/node @tsconfig/node20 --save-dev

These packages enable TypeScript in your Node.js environment, allowing you to write and compile TypeScript code seamlessly.

Unlock Your Backend Development Skills! Ready to master Node.js and Express for backend development? Check out this comprehensive course on Coursera: Developing Backend Apps with Node.js and Express

Whether you’re a beginner or looking to refine your skills, this course covers everything you need to know to build powerful backend applications. Enroll now and take your coding journey to the next level! ??

Step 3: Configure TypeScript for Node.js

Now that TypeScript is installed, create a tsconfig.json file to define your TypeScript configuration:

npx tsc --init

Here’s an example of a tsconfig.json file tailored for Node.js projects. It uses Node.js 20 typescript base.

{
  "extends": "@tsconfig/node20/tsconfig.json",
  "compilerOptions": {
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "moduleResolution": "node"
  },
  "include": ["src/**/*.ts"],
  "exclude": ["node_modules"]
}

This configuration sets up TypeScript with strict type-checking, compatibility with Node.js modules, and a clear structure for your project files.

Step 4: Set Up Jest for Testing with TypeScript

Testing is crucial for any project, and Jest is a powerful testing framework that pairs perfectly with TypeScript. To set it up, install Jest and its TypeScript support:

npm install jest ts-jest @types/jest --save-dev

Next, create a Jest configuration file to make sure it works smoothly with TypeScript:

// jest.config.js
module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  roots: ['./tests'],
  transform: {
    '^.+\\.tsx?$': 'ts-jest',
  },
  testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
};

This setup allows Jest to transpile TypeScript files and ensures that your TypeScript code is properly tested.

Step 5: Write and Test Your Code with TypeScript and Jest

Let’s create a simple utility function in the src directory:

// src/utils.ts
export function add(a: number, b: number): number {
  return a + b;
}

Now, let’s write a Jest test for this function:

// tests/add.test.ts
import { add } from '../src/utils';

test('adds 1 + 2 to equal 3', () => {
  expect(add(1, 2)).toBe(3);
});

To run your tests, simply add the script "test": "jest" to your package.json file and run:

npm run test

If everything is set up correctly, Jest will run the tests and confirm that your TypeScript code is functioning as expected.

Conclusion

Setting up a Node.js project with TypeScript and Jest is essential for creating reliable, type-safe applications. By following these steps, you’ll have a modern development environment where TypeScript ensures your code is robust, and Jest guarantees it’s thoroughly tested. This powerful combination of TypeScript and Jest not only enhances your development process but also results in a codebase that’s easier to maintain and scale.

Share this article

Similar Posts