{"id":30822,"date":"2024-08-11T07:45:09","date_gmt":"2024-08-11T15:45:09","guid":{"rendered":"https:\/\/alexrusin.com\/?p=30822"},"modified":"2024-10-12T07:02:33","modified_gmt":"2024-10-12T15:02:33","slug":"setting-up-a-modern-node-js-project-with-typescript-and-jest","status":"publish","type":"post","link":"https:\/\/blog.alexrusin.com\/setting-up-a-modern-node-js-project-with-typescript-and-jest\/","title":{"rendered":"Setting Up a Modern Node.js Project with TypeScript and Jest"},"content":{"rendered":"\n

In today\u2019s development world, it\u2019s 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.<\/p>\n\n\n\n

Step 1: Initialize Your Node.js Project<\/h2>\n\n\n\n

Begin by creating a new directory for your project, navigate to it in your terminal, and initialize a Node.js project:<\/p>\n\n\n

\nnpm init -y\n<\/pre><\/div>\n\n\n

This command will generate a package.json<\/code> file, which acts as the backbone for managing your project’s dependencies.<\/p>\n\n\n\n

Step 2: Install TypeScript and Essential Tools<\/h2>\n\n\n\n

To integrate TypeScript into your Node.js project, install TypeScript and some necessary tools:<\/p>\n\n\n

\nnpm install typescript ts-node @types\/node @tsconfig\/node20 --save-dev\n<\/pre><\/div>\n\n\n

These packages enable TypeScript in your Node.js environment, allowing you to write and compile TypeScript code seamlessly.<\/p>\n\n\n\n

\n

Unlock Your Backend Development Skills!<\/strong> 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<\/a><\/p>\n\n\n\n

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! ??<\/p>\n<\/blockquote>\n\n\n\n

Step 3: Configure TypeScript for Node.js<\/h2>\n\n\n\n

Now that TypeScript is installed, create a tsconfig.json<\/code> file to define your TypeScript configuration:<\/p>\n\n\n

\nnpx tsc --init\n<\/pre><\/div>\n\n\n

Here’s an example of a tsconfig.json<\/code> file tailored for Node.js projects. It uses Node.js 20 typescript base.<\/a><\/p>\n\n\n\n

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

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

Step 4: Set Up Jest for Testing with TypeScript<\/h2>\n\n\n\n

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:<\/p>\n\n\n

\nnpm install jest ts-jest @types\/jest --save-dev\n<\/pre><\/div>\n\n\n

Next, create a Jest configuration file to make sure it works smoothly with TypeScript:<\/p>\n\n\n\n

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

This setup allows Jest to transpile TypeScript files and ensures that your TypeScript code is properly tested.<\/p>\n\n\n\n

Step 5: Write and Test Your Code with TypeScript and Jest<\/h2>\n\n\n\n

Let\u2019s create a simple utility function in the src<\/code> directory:<\/p>\n\n\n\n

\/\/ src\/utils.ts\nexport function add(a: number, b: number): number {\n  return a + b;\n}<\/pre>\n\n\n\n

Now, let\u2019s write a Jest test for this function:<\/p>\n\n\n\n

\/\/ tests\/add.test.ts\nimport { add } from '..\/src\/utils';\n\ntest('adds 1 + 2 to equal 3', () => {\n  expect(add(1, 2)).toBe(3);\n});<\/pre>\n\n\n\n

To run your tests, simply add the script \"test\": \"jest\"<\/code> to your package.json<\/code> file and run:<\/p>\n\n\n

\nnpm run test\n<\/pre><\/div>\n\n\n

If everything is set up correctly, Jest will run the tests and confirm that your TypeScript code is functioning as expected.<\/p>\n\n\n\n

Conclusion<\/h4>\n\n\n\n

Setting up a Node.js project with TypeScript and Jest is essential for creating reliable, type-safe applications. By following these steps, you\u2019ll have a modern development environment where TypeScript ensures your code is robust, and Jest guarantees it\u2019s thoroughly tested. This powerful combination of TypeScript and Jest not only enhances your development process but also results in a codebase that\u2019s easier to maintain and scale.<\/p>\n","protected":false},"excerpt":{"rendered":"

In this guide, you’ll learn how to set up a Node.js project with TypeScript and Jest. The article takes you through each step, from initializing the project and configuring TypeScript to integrating Jest for testing. By following this tutorial, you’ll ensure your code is type-safe, well-tested, and ready for modern development.<\/p>\n","protected":false},"author":1,"featured_media":30826,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"footnotes":""},"categories":[15],"tags":[17,20,46,35],"class_list":["post-30822","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-javascript","tag-jest","tag-testing","tag-typescript"],"yoast_head":"\nSetting Up a Modern Node.js Project with TypeScript and Jest | Alex Rusin Blog<\/title>\n<meta name=\"description\" content=\"This article walks you through configuring TypeScript, integrating Jest for testing, and ensuring your code is type-safe and reliable\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.alexrusin.com\/setting-up-a-modern-node-js-project-with-typescript-and-jest\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Setting Up a Modern Node.js Project with TypeScript and Jest | Alex Rusin Blog\" \/>\n<meta property=\"og:description\" content=\"This article walks you through configuring TypeScript, integrating Jest for testing, and ensuring your code is type-safe and reliable\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.alexrusin.com\/setting-up-a-modern-node-js-project-with-typescript-and-jest\/\" \/>\n<meta property=\"og:site_name\" content=\"Alex Rusin Blog\" \/>\n<meta property=\"article:published_time\" content=\"2024-08-11T15:45:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-12T15:02:33+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/blog.alexrusin.com\/wp-content\/uploads\/2024\/08\/Node-Typescript-Jest-Cover-2-Cover-scaled.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"2560\" \/>\n\t<meta property=\"og:image:height\" content=\"1440\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"alexrusin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"alexrusin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/blog.alexrusin.com\/setting-up-a-modern-node-js-project-with-typescript-and-jest\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/blog.alexrusin.com\/setting-up-a-modern-node-js-project-with-typescript-and-jest\/\"},\"author\":{\"name\":\"alexrusin\",\"@id\":\"https:\/\/blog.alexrusin.com\/#\/schema\/person\/a9005ca622862109b2c514050fbaaf9a\"},\"headline\":\"Setting Up a Modern Node.js Project with TypeScript and Jest\",\"datePublished\":\"2024-08-11T15:45:09+00:00\",\"dateModified\":\"2024-10-12T15:02:33+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/blog.alexrusin.com\/setting-up-a-modern-node-js-project-with-typescript-and-jest\/\"},\"wordCount\":480,\"publisher\":{\"@id\":\"https:\/\/blog.alexrusin.com\/#\/schema\/person\/a9005ca622862109b2c514050fbaaf9a\"},\"image\":{\"@id\":\"https:\/\/blog.alexrusin.com\/setting-up-a-modern-node-js-project-with-typescript-and-jest\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/blog.alexrusin.com\/wp-content\/uploads\/2024\/08\/Node-Typescript-Jest-Cover-2-Cover-scaled.jpg\",\"keywords\":[\"javascript\",\"Jest\",\"testing\",\"typescript\"],\"articleSection\":[\"Javascript Development\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/blog.alexrusin.com\/setting-up-a-modern-node-js-project-with-typescript-and-jest\/\",\"url\":\"https:\/\/blog.alexrusin.com\/setting-up-a-modern-node-js-project-with-typescript-and-jest\/\",\"name\":\"Setting Up a Modern Node.js Project with TypeScript and Jest | Alex Rusin Blog\",\"isPartOf\":{\"@id\":\"https:\/\/blog.alexrusin.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/blog.alexrusin.com\/setting-up-a-modern-node-js-project-with-typescript-and-jest\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/blog.alexrusin.com\/setting-up-a-modern-node-js-project-with-typescript-and-jest\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/blog.alexrusin.com\/wp-content\/uploads\/2024\/08\/Node-Typescript-Jest-Cover-2-Cover-scaled.jpg\",\"datePublished\":\"2024-08-11T15:45:09+00:00\",\"dateModified\":\"2024-10-12T15:02:33+00:00\",\"description\":\"This article walks you through configuring TypeScript, integrating Jest for testing, and ensuring your code is type-safe and reliable\",\"breadcrumb\":{\"@id\":\"https:\/\/blog.alexrusin.com\/setting-up-a-modern-node-js-project-with-typescript-and-jest\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blog.alexrusin.com\/setting-up-a-modern-node-js-project-with-typescript-and-jest\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/blog.alexrusin.com\/setting-up-a-modern-node-js-project-with-typescript-and-jest\/#primaryimage\",\"url\":\"https:\/\/blog.alexrusin.com\/wp-content\/uploads\/2024\/08\/Node-Typescript-Jest-Cover-2-Cover-scaled.jpg\",\"contentUrl\":\"https:\/\/blog.alexrusin.com\/wp-content\/uploads\/2024\/08\/Node-Typescript-Jest-Cover-2-Cover-scaled.jpg\",\"width\":2560,\"height\":1440,\"caption\":\"Node.js Typescript and Jest\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blog.alexrusin.com\/setting-up-a-modern-node-js-project-with-typescript-and-jest\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/blog.alexrusin.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Setting Up a Modern Node.js Project with TypeScript and Jest\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blog.alexrusin.com\/#website\",\"url\":\"https:\/\/blog.alexrusin.com\/\",\"name\":\"Alex Rusin\",\"description\":\"Web Development Blog\",\"publisher\":{\"@id\":\"https:\/\/blog.alexrusin.com\/#\/schema\/person\/a9005ca622862109b2c514050fbaaf9a\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/blog.alexrusin.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/blog.alexrusin.com\/#\/schema\/person\/a9005ca622862109b2c514050fbaaf9a\",\"name\":\"alexrusin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/blog.alexrusin.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c36ef231f9e0b11371891eb84360f4bc?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c36ef231f9e0b11371891eb84360f4bc?s=96&d=mm&r=g\",\"caption\":\"alexrusin\"},\"logo\":{\"@id\":\"https:\/\/blog.alexrusin.com\/#\/schema\/person\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Setting Up a Modern Node.js Project with TypeScript and Jest | Alex Rusin Blog","description":"This article walks you through configuring TypeScript, integrating Jest for testing, and ensuring your code is type-safe and reliable","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.alexrusin.com\/setting-up-a-modern-node-js-project-with-typescript-and-jest\/","og_locale":"en_US","og_type":"article","og_title":"Setting Up a Modern Node.js Project with TypeScript and Jest | Alex Rusin Blog","og_description":"This article walks you through configuring TypeScript, integrating Jest for testing, and ensuring your code is type-safe and reliable","og_url":"https:\/\/blog.alexrusin.com\/setting-up-a-modern-node-js-project-with-typescript-and-jest\/","og_site_name":"Alex Rusin Blog","article_published_time":"2024-08-11T15:45:09+00:00","article_modified_time":"2024-10-12T15:02:33+00:00","og_image":[{"width":2560,"height":1440,"url":"https:\/\/blog.alexrusin.com\/wp-content\/uploads\/2024\/08\/Node-Typescript-Jest-Cover-2-Cover-scaled.jpg","type":"image\/jpeg"}],"author":"alexrusin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"alexrusin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.alexrusin.com\/setting-up-a-modern-node-js-project-with-typescript-and-jest\/#article","isPartOf":{"@id":"https:\/\/blog.alexrusin.com\/setting-up-a-modern-node-js-project-with-typescript-and-jest\/"},"author":{"name":"alexrusin","@id":"https:\/\/blog.alexrusin.com\/#\/schema\/person\/a9005ca622862109b2c514050fbaaf9a"},"headline":"Setting Up a Modern Node.js Project with TypeScript and Jest","datePublished":"2024-08-11T15:45:09+00:00","dateModified":"2024-10-12T15:02:33+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.alexrusin.com\/setting-up-a-modern-node-js-project-with-typescript-and-jest\/"},"wordCount":480,"publisher":{"@id":"https:\/\/blog.alexrusin.com\/#\/schema\/person\/a9005ca622862109b2c514050fbaaf9a"},"image":{"@id":"https:\/\/blog.alexrusin.com\/setting-up-a-modern-node-js-project-with-typescript-and-jest\/#primaryimage"},"thumbnailUrl":"https:\/\/blog.alexrusin.com\/wp-content\/uploads\/2024\/08\/Node-Typescript-Jest-Cover-2-Cover-scaled.jpg","keywords":["javascript","Jest","testing","typescript"],"articleSection":["Javascript Development"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/blog.alexrusin.com\/setting-up-a-modern-node-js-project-with-typescript-and-jest\/","url":"https:\/\/blog.alexrusin.com\/setting-up-a-modern-node-js-project-with-typescript-and-jest\/","name":"Setting Up a Modern Node.js Project with TypeScript and Jest | Alex Rusin Blog","isPartOf":{"@id":"https:\/\/blog.alexrusin.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/blog.alexrusin.com\/setting-up-a-modern-node-js-project-with-typescript-and-jest\/#primaryimage"},"image":{"@id":"https:\/\/blog.alexrusin.com\/setting-up-a-modern-node-js-project-with-typescript-and-jest\/#primaryimage"},"thumbnailUrl":"https:\/\/blog.alexrusin.com\/wp-content\/uploads\/2024\/08\/Node-Typescript-Jest-Cover-2-Cover-scaled.jpg","datePublished":"2024-08-11T15:45:09+00:00","dateModified":"2024-10-12T15:02:33+00:00","description":"This article walks you through configuring TypeScript, integrating Jest for testing, and ensuring your code is type-safe and reliable","breadcrumb":{"@id":"https:\/\/blog.alexrusin.com\/setting-up-a-modern-node-js-project-with-typescript-and-jest\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.alexrusin.com\/setting-up-a-modern-node-js-project-with-typescript-and-jest\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blog.alexrusin.com\/setting-up-a-modern-node-js-project-with-typescript-and-jest\/#primaryimage","url":"https:\/\/blog.alexrusin.com\/wp-content\/uploads\/2024\/08\/Node-Typescript-Jest-Cover-2-Cover-scaled.jpg","contentUrl":"https:\/\/blog.alexrusin.com\/wp-content\/uploads\/2024\/08\/Node-Typescript-Jest-Cover-2-Cover-scaled.jpg","width":2560,"height":1440,"caption":"Node.js Typescript and Jest"},{"@type":"BreadcrumbList","@id":"https:\/\/blog.alexrusin.com\/setting-up-a-modern-node-js-project-with-typescript-and-jest\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.alexrusin.com\/"},{"@type":"ListItem","position":2,"name":"Setting Up a Modern Node.js Project with TypeScript and Jest"}]},{"@type":"WebSite","@id":"https:\/\/blog.alexrusin.com\/#website","url":"https:\/\/blog.alexrusin.com\/","name":"Alex Rusin","description":"Web Development Blog","publisher":{"@id":"https:\/\/blog.alexrusin.com\/#\/schema\/person\/a9005ca622862109b2c514050fbaaf9a"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.alexrusin.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/blog.alexrusin.com\/#\/schema\/person\/a9005ca622862109b2c514050fbaaf9a","name":"alexrusin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blog.alexrusin.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c36ef231f9e0b11371891eb84360f4bc?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c36ef231f9e0b11371891eb84360f4bc?s=96&d=mm&r=g","caption":"alexrusin"},"logo":{"@id":"https:\/\/blog.alexrusin.com\/#\/schema\/person\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/blog.alexrusin.com\/wp-json\/wp\/v2\/posts\/30822","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.alexrusin.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.alexrusin.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.alexrusin.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.alexrusin.com\/wp-json\/wp\/v2\/comments?post=30822"}],"version-history":[{"count":6,"href":"https:\/\/blog.alexrusin.com\/wp-json\/wp\/v2\/posts\/30822\/revisions"}],"predecessor-version":[{"id":30963,"href":"https:\/\/blog.alexrusin.com\/wp-json\/wp\/v2\/posts\/30822\/revisions\/30963"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.alexrusin.com\/wp-json\/wp\/v2\/media\/30826"}],"wp:attachment":[{"href":"https:\/\/blog.alexrusin.com\/wp-json\/wp\/v2\/media?parent=30822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.alexrusin.com\/wp-json\/wp\/v2\/categories?post=30822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.alexrusin.com\/wp-json\/wp\/v2\/tags?post=30822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}